Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Rep:
Bash Scripts - need help with variables
Hello all,
I want to create a script that processes parameters which a user might specify when they call my script.
example of how I want my script to be able to be used:
Code:
sh myscript red green orange
I'd imagine I want to be able to do something like:
Code:
p=1
for ARG in "$@"
do
I want to make a variable(p) contain whatever the first parameter is
let p=p+1
and then variable(p) contain whatever the second parameter is
and so on until there are no more parameters.
done
In usage, the user calling my script could specify any quantity of parameters.
I am just not sure how to go about getting variables like this to work. Can anybody point me in the correct direction?
Well... it sounds good. However I think there is a problem with the fact that Ubuntu uses /bin/dash as the default shell and for some reason the sha-bang #!/bin/bash is skipped. In other words it looks like the script is executed by the dash shell and the array assigment is buggy in some way. Just a guess, but I found confirmation on the Ubuntu forums, here (see in particular post #5). By the way, how did you run the script? Does
Indeed when the shell is invoked as interactive (for example when the standard input is connected to a terminal and you don't use the -c option) the sha-bang is treated as a comment. The question now is: how does /bin/dash manage arrays? What are the differences between the /bin/bash syntax and /bin/dash? I cannot find any exhaustive documentation on the net. Maybe someone here can shed some light.
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Original Poster
Rep:
Almost there.
My script is:
Code:
#!/bin/bash
array=("$@")
len=${#array[*]}
echo "The array has $len members. They are:"
i=0
while [ $i -lt $len ]; do
parameter$i=${array[$i]}
echo "${parameter[$i]}"
let i++
done
When I invoke my script using:
Quote:
./myscript.sh alpha bravo charlie
I get the following output:
Code:
The array has 3 members. They are:
./myscript.sh: line 8: parameter0=alpha: command not found
./myscript.sh: line 8: parameter1=bravo: command not found
./myscript.sh: line 8: parameter2=charlie: command not found
What I really want is echo "${parameter[$i]}" to deliver output as:
Code:
The array has 3 members. They are:
alpha
bravo
charlie
Where:
parameter1 = alpha
parameter2 = bravo
parameter3 = charlie
I know I am close to the solution but my knowledge of manipulating variables is weak.
It's not really clear what you're trying to do here. This code tries to assign scalar variables parameter0, parameter1, parameter2 then it tries to print them as array elements. In any case waht you need is indirect reference where the name of a variable is referenced by another variable. Here is a working example using eval:
Code:
i=1
while [ $i -lt $len ]; do
eval parameter$i=${array[$i]}
eval echo \$parameter$i
let i++
done
So I will start with, I am not really sure what the objective is here?
The reason i said this is because unless you need the separate storage, ie array, for another reason, why not just use the positional parameters as they lay?
$# will give you the number of parameters passed in and the same as using say array[1] you can just use $2 (as arrays start at 0 unless otherwise defined).
So to use your current example:
Code:
#!/bin/bash
echo "$# members were passed in. They are:"
for ARG
do
echo $ARG
done
Output:
Code:
$ ./myscript.sh alpha bravo charlie
3 members were passed in. They are:
alpha
bravo
charlie
The question now is: how does /bin/dash manage arrays? What are the differences between the /bin/bash syntax and /bin/dash? I cannot find any exhaustive documentation on the net. Maybe someone here can shed some light.
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Original Poster
Rep:
OK, I'm starting to see what you mean about an array perhaps being the most appropriate method for what I want to do because while the array is storing the parameters that were specified when my script is invoked, I can step through the array, locate a parameter and act upon it using while and if loops.
OK, I'm starting to see what you mean about an array perhaps being the most appropriate method for what I want to do because while the array is storing the parameters that were specified when my script is invoked, I can step through the array, locate a parameter and act upon it using while and if loops.
That and the fact that eval is often pronounced 'evil' as it can cause some serious headaches ... especially if it gets out of control.
maybe a case where you can deal with any or all parameters?
Code:
#!/bin/bash
while test $# -gt 0; do
case "$1" in
red)
echo "$1"
# code for red param
shift;;
green)
echo "$1"
# code for green param
shift;;
blue)
echo "$1"
#code for blue param
shift;;
*)
echo "Not in parameter list"
# or code to handle case of no or non-listed param
exit 1;;
esac
done
as is, the case will exit as soon as a non-listed parameter is found.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.