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 Code:
p=1 I am just not sure how to go about getting variables like this to work. Can anybody point me in the correct direction? Cheers, Greenie |
If I understand well you want to assign the positional parameters to the array elements. If this is the case you can simply do
Code:
array=("$@") |
Thanks, I just did a quick read on Arrays and that method might be easiest if I can just get the basics working.
The following: Code:
#!/bin/bash Quote:
Code:
#!/bin/bash Quote:
What am I doing wrong with my syntax when using the array() command? |
Quote:
Code:
readlink -f /bin/bash |
Output of readlink -f /bin/bash
Code:
/bin/bash Output of bash --version Code:
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) |
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
Code:
bash scriptname.sh |
Yes you're right.
The sha-bang was being skipped when I invoke my script using sh myscript.sh So instead I tried: Code:
./myscript.sh |
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. :jawa:
Edit: I found some information on Greg's Wiki: http://mywiki.wooledge.org/Bashism which simply states that Quote:
|
Almost there.
My script is: Code:
#!/bin/bash Quote:
Code:
The array has 3 members. They are: Code:
The array has 3 members. They are: parameter1 = alpha parameter2 = bravo parameter3 = charlie I know I am close to the solution but my knowledge of manipulating variables is weak. |
Code:
parameter$i=${array[$i]} Code:
i=1 Anyway, if you need to assign the positional parameters to scalar variables, the array step is superfluous. Here is two alternatives: Code:
#!/bin/bash Code:
#!/bin/bash |
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 Code:
$ ./myscript.sh alpha bravo charlie |
Quote:
|
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.
|
Quote:
|
maybe a case where you can deal with any or all parameters?
Code:
#!/bin/bash |
All times are GMT -5. The time now is 07:31 PM. |