positional parameters | if

$0 is the name of the command itself
$1 first parameter, $2 second, etc

$# variable tells how many parameters it was called with

$ cat n3params
echo "my name is $0"
echo "I was called as $0 with $# parameters"
if [ "$#" -eq "2" ]; then
echo "my first parameter is $1"
echo "my second parameter is $2"
else
echo "you provided $# par'ameters but 2 required"
fi
---
$ bash n3params f1 r2
my name is n3params
I was called as n3params with 2 parameters
my first parameter is f1
my second parameter is r2

$ bash n3params f2
my name is n3params
I was called as n3params with 1 parameters
you provided 1 par'ameters but 2 required
-----------------------
 cat n4manaparams
echo "I was called with $# params"
count=1
while [ "$#" -ge "1" ]; do
echo "parameter $count is $1"
let count=$count+1
shift
done
--
bash n4manaparams qwe rty uio
I was called with 3 params
parameter 1 is qwe
parameter 2 is rty
parameter 3 is uio

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all