Re: Bash scripting -- Usage of arrays

From: Jayesh Jayan (jayesh.freebsdlist_at_gmail.com)
Date: 11/29/05

  • Next message: Jayesh Jayan: "Re: Bash scripting -- Usage of arrays"
    Date: Wed, 30 Nov 2005 03:20:42 +0530
    To: John Baldwin <jhb@freebsd.org>
    
    

    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=( 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 != 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="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" = 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
    _______________________________________________
    freebsd-hackers@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
    To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
    

  • Next message: Jayesh Jayan: "Re: Bash scripting -- Usage of arrays"

    Relevant Pages

    • Re: Bash scripting -- Usage of arrays
      ... >> Below is a sample script which I used. ... > You can either install bash from ports, or you can write your scripts in ... > for x in $array; ...
      (freebsd-questions)
    • Re: array problem: space between words
      ... last_arr_member# FUNCTION to echo the amount of members an array contains ... can be used to append an element to the array. ... Removing my 'echo', and your 'eval', this is the quickest way for bash I can think of by now. ... Yes, I use command substitution in the calling functions, to do things like reverse iterating and array. ...
      (comp.unix.shell)
    • Re: safely iterating through potentially empty arrays in bash
      ... > My version of bash is the standard debian woody one: ... > # initialise foo to the empty array ... > The number of elements in foo is 0. ...
      (comp.unix.shell)
    • Re: Field splitting: Is the default IFS treated differently?
      ... I've run across another oddity of this sort in bash. ... While fiddling with a script to read a file into an array with a single ... According to the bash man page, command substitution removes trailing ...
      (comp.unix.shell)
    • Re: Faster alternative
      ... While it can be done in bash, ... I never knew it was possible with newlines ... set -u # Stop with error message on usage of unset veriables to catch errors ... IFS=$'\n' # array will contain each line of output as an array ellement ...
      (comp.unix.shell)