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!!!

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

command arg1 arg2 arg3 arg4 .


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.

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

http://wooledge.org:8000/BashFAQ/005

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.

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. :frowning:

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.

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


array="one two three four five"
n=0 ;for i in ${array}; do newarray[$n]=$i; let n++; done ;echo ${newarray[li]}[/li]```


Nope no nice formatting, now you would need to change a var to $1 I'll let you work it out.

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[li].
[/li]I’ll try that syntax, tell you later…

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[li]}[/li]or
${myarray[@]}

You can see that with:

myarray[0]=“first one”
myarray[1]=“second”
echo ${myarray[li]}[/li]
I used that example to show you that if you were to
call a script:

examplescript.sh ${myarray[li]}[/li]
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

${newarray*]} 's idea did not work…

Thanks cjcox!!!What you wrote will work then,I believe.
Tell you later, tks again!!!

IT WORKS!!! I suppose “$@” is equivalent to $1, “$@” refers to the whole array.
So, I should make reference to each element like this:

“$@”[0]

or better:

bestarray@]="$@"
?
Thanks!

On Tue, 2009-03-31 at 23:16 +0000, instaler wrote:
> IT WORKS!!! I suppose “$@” is equivalent to $1, “$@” refers to the
> whole array.
> So, I should make reference to each element like this:
>
> “$@”[0]

Nope. It’s really just a way of handling the arguments.
You’ll have to iterate.

Now … you could do something like:

for arg in “$@”; do
myarray$count]="$arg"
let count++
done

Then myarray would have the values from the command line.

Just for some added detail. The first 9 command line
parameters can be refereced by using $1 … $9.

That’s somewhat limiting though. You can also pull out items
from the command line though and the command shift can
be very useful.

Consider the following code snippet:
(this is written in portable Bourne shell
script… apologies to bash purists)

f=0
while $# -gt 0 ]; do
case “$1” in
-o?*)

handles things like -oValue

o=expr "$1" : '..\(.*\)'
;;
-o)

handles things like -o Value

o="$2"
shift
;;
-f)

Just a flag (on/off)

f=1
;;
*)
break
;;
esac
shift
done

After the above runs, the only thing left as arguments

are the non switch items (this code assumes switches

come first). The flag f might be set and the value for

“o” might be set.

echo “o is $o”
echo “f is $f”

Loop through remaining arguments (arguments without a hyphen)

for arg in “$@”; do
echo “args: $arg”
done

Thabks again!!:slight_smile:

And the reason for that is clear, when one realises how the shell interacts with the OS. In usual programming languages, you don’t leave the language environment when you do a function call. However in shell, when you invoke another program, this goes through the OS. The Unix/Linux execution model simply says that the program is given an array of strings as arguments from the caller. What goes through are the string values since the caller is in a different address space.