Positional Parameters
A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell's arguments when it is invoked
The shell maintains a variable called $# that contains the number of items on the command line in addition to the name of the command
For eg: If i write a simple program
#!/bin/bash
echo $#
and try to execute it
[root@localhost bash]# . test.sh # Since no variable is passed so output is 0
0
[root@localhost bash]# . test.sh 1 # Since 1 variable is passed so output is 1
1
[root@localhost bash]# . test.sh 2 # Since 1 variable is passed so output is 1
1
[root@localhost bash]# . test.sh 3 # Since 1 variable is passed so output is 1(independent of value if i passed 3 here the o/p is same 1)
1
[root@localhost bash]# . test.sh 2 5 #Sincei passed 2 variables(2and5)hence o/p is 2
2
The example script to determine positional parameters
#!/bin/bash
BADPARAM=165 # if no value is passed the output status is 165 (echo $?)
if [ $# != 2 ] # No of variables need to pass to script
then
echo "This script require at least 2 arguments"
echo "You have entered $# arguments"
exit $BADPARAM
fi
seq $1 $2
[root@localhost bash]# . posparam.sh 1 4
1
2
3
4
Thursday, July 24, 2008
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment