Re: Bash scripting -- Usage of arrays

From: John Baldwin (jhb_at_freebsd.org)
Date: 11/29/05

  • Next message: Freddie Cash: "Re: Bash scripting -- Usage of arrays"
    To: freebsd-hackers@freebsd.org
    Date: Tue, 29 Nov 2005 16:17:15 -0500
    
    

    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
    _______________________________________________
    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: Freddie Cash: "Re: Bash scripting -- Usage of arrays"

    Relevant Pages

    • Re: bullet proof for loop over $(find) in bash, how?
      ... : In ksh, perhaps. ... so shouldn't be used in a script. ... Bash is not standard. ... OP is using bash and arrays, ...
      (comp.unix.shell)
    • Re: Bash scripting -- Usage of arrays
      ... > Below is a sample script which I used. ... > Below is the out put from the FreeBSD server using the same code. ... > Please guide me on how to use arrays on freebsd too. ... Bash is in /usr/local/bin. ...
      (freebsd-questions)
    • Re: Bash scripting -- Usage of arrays
      ... > Below is a sample script which I used. ... > Below is the out put from the FreeBSD server using the same code. ... > Please guide me on how to use arrays on freebsd too. ... You can either install bash from ports, or you can write your scripts in sh ...
      (freebsd-questions)
    • RE: Shell script error
      ... The reason that works on Linux and not on FreeBSD is that on Linux "sh" is ... On FreeBSD, sh is the bourne shell and bash is bash. ... That script is a bash script, ...
      (freebsd-questions)
    • Re: Bash scripting -- Usage of arrays
      ... On November 29, 2005 01:09 pm, Jayesh Jayan wrote: ... > Today I was trying to script using arrays in FreeBSD 5.4 but it doesn't ... execute the script. ...
      (freebsd-hackers)