Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: passing arrays to a script

  1. #1

    Default passing arrays to a script

    HI, me again!!

    Is there any way you can "pass" a script an array or list of variables?

    Or you can only pass variables? (and , for instance,if you need to give 100 variables to a script , you can only do it one by one):

    whatever.sh var1 var2 ....var100

    Thanks!!!

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: passing arrays to a script

    Of course you can pass more than one argument to a script, just put them on the command line:

    command arg1 arg2 arg3 arg4 ...

  3. #3

    Default Re: passing arrays to a script

    I did not make myself clear: I know you can pass many arguments, the thing is if I can pass an array or a list, like this:

    script.sh array1

    being array1=array1[0]
    array1[1]
    ....
    ..
    array1[n]

    or

    script.sh list1

    being list1= var1, var2, ...., varn or sth of that kind.

  4. #4

    Default Re: passing arrays to a script

    Try here I suspect the answer is here, guessing its a read line $1 in your script.

    http://wooledge.org:8000/BashFAQ/005
    Man first, have a try at Info, have a look at Wiki, if all that fails Scroogle!!!!!
    If I've helped click on the Rep button I don't know what it does but it sounds cool.

  5. #5
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: passing arrays to a script

    Quote Originally Posted by instaler View Post
    I did not make myself clear: I know you can pass many arguments, the thing is if I can pass an array or a list, like this:

    script.sh array1

    being array1=array1[0]
    array1[1]
    ....
    ..
    array1[n]

    or

    script.sh list1

    being list1= var1, var2, ...., varn or sth of that kind.
    The short answer is yes. The long answer is you have to read the bash man page on how to use arrays in bash to do it. Note that the array type is not in classic Bourne shell, but is a bash feature.

  6. #6

    Default Re: passing arrays to a script

    I've read man pages but I get Tcl man page for array!!
    And it does not mention how to pass an array to a file/script.

  7. #7

    Default Re: passing arrays to a script

    The thing is: I still don't know how to pass an array to a file as a unique argument!!!

    I tried this:

    examplescript.sh $examplearray

    but it does not work.

  8. #8

    Default Re: passing arrays to a script

    follow my link, never done it but by using my very useful page..

    Code:
    array="one two three four five"
    n=0 ;for i in ${array}; do newarray[$n]=$i; let n++; done ;echo ${newarray[*]}
    Nope no nice formatting, now you would need to change a var to $1 I'll let you work it out.
    Man first, have a try at Info, have a look at Wiki, if all that fails Scroogle!!!!!
    If I've helped click on the Rep button I don't know what it does but it sounds cool.

  9. #9

    Default Re: passing arrays to a script

    I looked at your site, tks!!But I did not find anything related to passing an array as a parameter to another file, that is, when you want to "pass" a variable to a file it's very easy:

    examplescript.sh $var

    being "var" a variable with content (string, integer, whatever).
    What if my variable is an array?:

    examplescript.sh $array


    Maybe the right syntax is the one you used in your example, ${newarray[*].
    I'll try that syntax, tell you later...

  10. #10
    Join Date
    Jun 2008
    Location
    Frisco, TX
    Posts
    1,233

    Default Re: passing arrays to a script

    On Tue, 2009-03-31 at 19:36 +0000, instaler wrote:
    > I looked at your site, tks!!But I did not find anything related to
    > passing an array as a parameter to another file, that is, when you want
    > to "pass" a variable to a file it's very easy:
    >
    > examplescript.sh $var
    >
    > being "var" a variable with content (string, integer, whatever).
    > What if my variable is an array?:
    >
    > examplescript.sh $array
    >
    >
    > Maybe the right syntax is the one you used in your example,
    > ${newarray .
    > I'll try that syntax, tell you later...
    >
    >


    You can't pass arrays as arguments. You can pass the values though.
    With that said, there is a command line limit on number of parameters
    usually (you'll get a command line too long message if you exceed that).

    The way to explode the array is like:

    ${myarray[*]}
    or
    ${myarray[@]}

    You can see that with:

    myarray[0]="first one"
    myarray[1]="second"
    echo ${myarray[*]}

    I used that example to show you that if you were to
    call a script:

    examplescript.sh ${myarray[*]}

    That you'd actually be passing THREE arguments...
    first
    one
    second

    To fix this, you'll put quotes around the
    array expansion and use @ instead of *.

    examplescript.sh "${myarray[@]}"

    is the right way to do this.. but you need to be
    careful inside of examplescript.sh in how you handle
    the command line arguments.. you'll want something like:
    (notice the "$@" to loop through the args from the command line)

    for arg in "$@"; do
    echo "$arg"
    done





Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •