Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 30 Nov 2005 03:20:42 +0530
From:      Jayesh Jayan <jayesh.freebsdlist@gmail.com>
To:        John Baldwin <jhb@freebsd.org>
Cc:        freebsd-hackers@freebsd.org, freebsd-questions@freebsd.org
Subject:   Re: Bash scripting -- Usage of arrays
Message-ID:  <e8ecf3c00511291350w4208320kcbc34f50040c63b3@mail.gmail.com>
In-Reply-To: <200511291617.16666.jhb@freebsd.org>
References:  <e8ecf3c00511291309yb9caeb9uebdf92c4ad7af4f8@mail.gmail.com> <200511291617.16666.jhb@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi John,

Thank you.

It seems to work like a charm.


On 11/30/05, John Baldwin <jhb@freebsd.org> wrote:
>
> On Tuesday 29 November 2005 04:09 pm, Jayesh Jayan wrote:
> > Hi,
> >
> > Today I was trying to script using arrays in FreeBSD 5.4 but it doesn't
> > work.
> >
> > Below is a sample script which I used.
> >
> > ******************************************************
> >
> > #!/bin/bash
> >
> > array=3D( zero one two three four);
> > echo "Elements in array0:  ${array[@]}"
> >
> > ******************************************************
> >
> > It works fine on RedHat server.
> >
> > Below is the output.
> >
> > # sh array.sh
> > Elements in array0:  zero one two three four
> >
> > Below is the out put from the FreeBSD server using the same code.
> >
> > -bash-2.05b# sh aa.sh
> > aa.sh: 3: Syntax error: word unexpected (expecting ")")
> >
> > Please guide me on how to use arrays on freebsd too.
>
> sh !=3D bash
>
> You can either install bash from ports, or you can write your scripts in
> sh
> without using bash extensions.  For example, with sh you can do things
> like:
>
> array=3D"zero one to three four"
> for x in $array; do
>         echo $x
> done
>
> However, you can't easily get the count of items.  You could maybe do
> something like:
>
> set $array
> echo "$# items"
>
> but that's somewhat hackish.
>
> --
> John Baldwin <jhb@FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
> "Power Users Use the Power to Serve"  =3D  http://www.FreeBSD.org
>



--
Jayesh Jayan

"The box said "Requires Windows 95, NT, or better", so I installed Linux."

Visit my homepage @ http://www.jayeshjayan.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?e8ecf3c00511291350w4208320kcbc34f50040c63b3>