![]() |
|
Guides All Guides and HOWTO's. |
![]() |
|
Thread Tools | Display Modes |
|
|||
![]()
When passing arguments to a shell script it good to know the differences between the behaviour of the '$@' and '$*' special variables. Both of them allow you to retrieve the arguments or parameters passed to your script, but there are subtle differences when you surround them with quotes or not.
A shell script that illustrates these quirks: Code:
#!/bin/sh # J65nko - daemonforums.org echo; echo 'Parameters in "$@" -------' nr=0 for THIS in "$@" ; do nr=$((nr+1)) echo "Parameter $nr :\t$THIS" done echo; echo 'Parameters in $@ -------' nr=0 for THIS in $@ ; do nr=$((nr+1)) echo "Parameter $nr :\t$THIS" done echo; echo 'Parameters in "$*" -------' nr=0 for THIS in "$*" ; do nr=$((nr+1)) echo "Parameter $nr :\t$THIS" done echo; echo 'Parameters in $* -------' nr=0 for THIS in $* ; do nr=$((nr+1)) echo "Parameter $nr :\t$THIS" done Code:
$ ./paramtst.sh "King Alfred the Great" 1 2 Parameters in "$@" ------- Parameter 1 : King Alfred the Great Parameter 2 : 1 Parameter 3 : 2 Parameters in $@ ------- Parameter 1 : King Parameter 2 : Alfred Parameter 3 : the Parameter 4 : Great Parameter 5 : 1 Parameter 6 : 2 Parameters in "$*" ------- Parameter 1 : King Alfred the Great 1 2 Parameters in $* ------- Parameter 1 : King Parameter 2 : Alfred Parameter 3 : the Parameter 4 : Great Parameter 5 : 1 Parameter 6 : 2
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
![]()
Another approach to process the passed arguments is to use "shift". Using the above example:
$1 will contain "King Alfred the Great" $2 holds the number "1" $3 holds the number "2" The special variable "$#" contains the number of arguments. The situation after a shift: $1 holds the number "1" $2 holds the number "2" and $# will be decreased by one to reflect the number of the remaining parameters. A script to demonstrate that we can keep printing the first parameter in "$1" when we use shift: Code:
#!/bin/sh # J65nko - daemonforums.org while [ $# -ne 0 ]; do nr=$((nr+1)) echo "Parameter $nr :\t$1" shift done Code:
./paramshift.sh "King Alfred the Great" 1 2 Parameter 1 : King Alfred the Great Parameter 2 : 1 Parameter 3 : 2
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
![]()
Hi
I think it can be interesting to add this 2 lines at the start of the script and try it with and without double quotes around the string "King Alfred the Great" Code:
echo; echo "Number of Parameters :\t$#" echo "Original Parameter 1 :\t$1" ![]() |
![]() |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Solved] How to make 2 separate arguments in 1 bash script? | guitarscn | Programming | 1 | 31st August 2010 09:12 PM |
ask for a shell script | Simon | Programming | 5 | 27th April 2010 01:07 AM |
incrementing within a shell script? | spiderpig | Programming | 5 | 29th September 2008 08:12 PM |
Shell Script. | bsdnewbie999 | Programming | 21 | 15th July 2008 07:54 AM |
shell script with input | c0mrade | Programming | 5 | 13th July 2008 04:33 AM |