Monday, January 31, 2011

BASH : Positional parameters

Positional parameters are separated by spaces
. forloop.sh 2 7
touch posparam.sh
CONTENT OF FILE
#!/bin/bash
#Author
#Date
#Purpose
echo $#  -> the '#' pound sign is the name of the variables and '$#' are the number of positionals parameters
#END
. posparam.sh 1 -> returns 1
. posparam.sh 3 -> returns 1
. posparam.sh 4 7 -> returns 2
. posparam.sh 4 78 -> returns 2


pico posparam.sh
CONTENT OF FILE
#!/bin/bash
echo $#  -> the '#' pound sign is the name of the variables and '$#' are the number of positionals parameters
seq $1 $2
#END
. posparam.sh 1 100 -> counts from 1 to 100 ; You have to give 2 parameters to the script
CONTENT OF FILE
#!/bin/bash
BADPARAM=165
echo $#  -> the '#' pound sign is the name of the variables and '$#' are the number of positionals parameters
if [ $# != 2] #you must have 2 argument given to this file
then
echo This script requires 2 arguments
exit $BADPARAM
fi
#Begin Sequence from param 1 to param 2
seq $1 $2
#END
. posparam.sh
chmod u+X posparam.sh
./posparam.sh -> close the shell window and he return code is 165 and message 'This script requires 2 arguments'
./posparam.sh 1 10 -> counts from 1 to 0
CONTENT OF FILE
#!/bin/bash
BADPARAM=165
echo $#  -> the '#' pound sign is the name of the variables and '$#' are the number of positionals parameters
if [ $# != 2] #you must have 2 argument given to this file
then
echo This script requires 2 arguments
echo "You entered $# parameters"
exit $BADPARAM
fi
#Begin Sequence from param 1 to param 2
seq $1 $2
#END
./posparam.sh -> close the shell window and he return code is 165 and message 'You  have entered 0 parameters'
echo ${10} -> access the 10th parameter from the script

No comments:

Post a Comment