80 likes | 90 Views
Learn how to pass and handle command line parameters in shell scripts. Includes examples for passing values, handling strings and counting parameters.
E N D
Experiment No. 9 Presented by, Mr. SatishPise
Command Line Parameters ($1 $2 ..) # shaddem 10 30 • This example passes two command line parameters (10 and 30) to the script addem Example 1 #!/bin/bash # using one command line parameter value1=10 total=$[ $value1 * $1 ] echo $total
Example 2 #!/bin/bash # testing string parameters echo Hello $1, glad to meet you. # shtest3 satish
Example 3 #!/bin/bash # handling lots of parameters total=$[ ${10} * ${11} ] echo The tenth parameter is ${10} echo The eleventh parameter is ${11} echo The total is $total $ sh test4 1 2 3 4 5 6 7 8 9 10 11 12
Counting parameters ($#) Example 1 #!/bin/bash # getting the number of parameters echo There were $# parameters supplied. $ sh test8 There were 0 parameters supplied. $ sh test8 1 2 3 4 5
Being Shifty • Example 1 #!/bin/bash # demonstrating the shift command count=1 while [ -n "$1" ] do echo "Parameter #$count = $1" count=$[ $count + 1 ] shift done $ sh test13 rich barbarakatiejessica
Example 2 #!/bin/bash # demonstrating a multi-position shift echo "The original parameters: $*" shift 2 echo "Here’s the new first parameter: $1" $ sh test14 1 2 3 4 5
Processing simple options Example 1 #!/bin/bash # extracting command line options and values while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) param="$2" echo "Found the -b option, with parameter value $param" shift 2;; -c) echo "Found the -c option";; --) shift break;; *) echo "$1 is not an option";; esac shift done count=1 for param in "$@" do echo "Parameter #$count: $param" count=$[ $count + 1 ] done $ sh test17 -a -b test1 –d