1 / 8

Command Line Parameters for Script Execution

Learn how to pass and handle command line parameters in shell scripts. Includes examples for passing values, handling strings and counting parameters.

aaronmiller
Télécharger la présentation

Command Line Parameters for Script Execution

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Experiment No. 9 Presented by, Mr. SatishPise

  2. 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

  3. Example 2 #!/bin/bash # testing string parameters echo Hello $1, glad to meet you. # shtest3 satish

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

More Related