Validating command line arguments in shell script

posted by | Leave a comment

$ ps -ef | grep 4406 work 4406 4402 0 pts/0 bash #### confirmation of pidls -ltr total 156 drwxr-xr-x. H : history expand – enable to use history command and reuse commands from history.

So far you’ve seen how to write Linux bash scripts that do the job without user inputs.

This is useful when the script expects certain number of arguments and you need to validate before proceeding.

for example let’s create & execute the test script with some parameters : Our first file just have a ls command and echo of exist status: #! on execution , it gives 0 as exist status: ./letters osmodule rpms testfile txt1 Exit status is : 0 Our Second file just have a misspelled ls command and echo of exist status: #!

In many cases, bash scripts require argument values to provide input options to the script.

You can handle command line arguments in a bash script by two ways.

These bash parameters are used to process command line arguments in a bash shell script, to get process status, exit status and options flag./bin/bash while [ -n "

These bash parameters are used to process command line arguments in a bash shell script, to get process status, exit status and options flag.

/bin/bash while [ -n "$1" ]; do # while loop starts case "$1" in -a) echo "-a option passed" ;; # Message for -a option -b) echo "-b option passed" ;; # Message for -b option -c) echo "-c option passed" ;; # Message for -c option --) shift # The double dash which separates options from parameters break ;; # Exit the loop using break command *) echo "Option $1 not recognized" ;; esac shift done total=1 for param in [email protected]; do echo "#$total: $param" total=$(($total 1)) done As you can see from the result, all the parameters after the double dash treated as parameters and not options.

When you dig deep into Linux bash scripting, sometimes you need options with additional parameter values like this: #!

/bin/sh las ## non existing command echo “Exit status is : ” $?

on execution , it gives a non 0 exist status, indicating failure to execute: ././test.sh: line 2: las: command not found Exit status is : 127Lets create a test file to get the process id #!

||

These bash parameters are used to process command line arguments in a bash shell script, to get process status, exit status and options flag./bin/bash while [ -n "$1" ]; do # while loop starts case "$1" in -a) echo "-a option passed" ;; # Message for -a option -b) echo "-b option passed" ;; # Message for -b option -c) echo "-c option passed" ;; # Message for -c option --) shift # The double dash which separates options from parameters break ;; # Exit the loop using break command *) echo "Option $1 not recognized" ;; esac shift done total=1 for param in [email protected]; do echo "#$total: $param" total=$(($total 1)) done As you can see from the result, all the parameters after the double dash treated as parameters and not options.When you dig deep into Linux bash scripting, sometimes you need options with additional parameter values like this: #!/bin/sh las ## non existing command echo “Exit status is : ” $?on execution , it gives a non 0 exist status, indicating failure to execute: ././test.sh: line 2: las: command not found Exit status is : 127Lets create a test file to get the process id #!

" ]; do # while loop starts case "

These bash parameters are used to process command line arguments in a bash shell script, to get process status, exit status and options flag.

/bin/bash while [ -n "$1" ]; do # while loop starts case "$1" in -a) echo "-a option passed" ;; # Message for -a option -b) echo "-b option passed" ;; # Message for -b option -c) echo "-c option passed" ;; # Message for -c option --) shift # The double dash which separates options from parameters break ;; # Exit the loop using break command *) echo "Option $1 not recognized" ;; esac shift done total=1 for param in [email protected]; do echo "#$total: $param" total=$(($total 1)) done As you can see from the result, all the parameters after the double dash treated as parameters and not options.

When you dig deep into Linux bash scripting, sometimes you need options with additional parameter values like this: #!

/bin/sh las ## non existing command echo “Exit status is : ” $?

on execution , it gives a non 0 exist status, indicating failure to execute: ././test.sh: line 2: las: command not found Exit status is : 127Lets create a test file to get the process id #!

||

These bash parameters are used to process command line arguments in a bash shell script, to get process status, exit status and options flag./bin/bash while [ -n "$1" ]; do # while loop starts case "$1" in -a) echo "-a option passed" ;; # Message for -a option -b) echo "-b option passed" ;; # Message for -b option -c) echo "-c option passed" ;; # Message for -c option --) shift # The double dash which separates options from parameters break ;; # Exit the loop using break command *) echo "Option $1 not recognized" ;; esac shift done total=1 for param in [email protected]; do echo "#$total: $param" total=$(($total 1)) done As you can see from the result, all the parameters after the double dash treated as parameters and not options.When you dig deep into Linux bash scripting, sometimes you need options with additional parameter values like this: #!/bin/sh las ## non existing command echo “Exit status is : ” $?on execution , it gives a non 0 exist status, indicating failure to execute: ././test.sh: line 2: las: command not found Exit status is : 127Lets create a test file to get the process id #!

" in -a) echo "-a option passed" ;; # Message for -a option -b) echo "-b option passed" ;; # Message for -b option -c) echo "-c option passed" ;; # Message for -c option --) shift # The double dash which separates options from parameters break ;; # Exit the loop using break command *) echo "Option

These bash parameters are used to process command line arguments in a bash shell script, to get process status, exit status and options flag.

/bin/bash while [ -n "$1" ]; do # while loop starts case "$1" in -a) echo "-a option passed" ;; # Message for -a option -b) echo "-b option passed" ;; # Message for -b option -c) echo "-c option passed" ;; # Message for -c option --) shift # The double dash which separates options from parameters break ;; # Exit the loop using break command *) echo "Option $1 not recognized" ;; esac shift done total=1 for param in [email protected]; do echo "#$total: $param" total=$(($total 1)) done As you can see from the result, all the parameters after the double dash treated as parameters and not options.

When you dig deep into Linux bash scripting, sometimes you need options with additional parameter values like this: #!

/bin/sh las ## non existing command echo “Exit status is : ” $?

on execution , it gives a non 0 exist status, indicating failure to execute: ././test.sh: line 2: las: command not found Exit status is : 127Lets create a test file to get the process id #!

||

These bash parameters are used to process command line arguments in a bash shell script, to get process status, exit status and options flag./bin/bash while [ -n "$1" ]; do # while loop starts case "$1" in -a) echo "-a option passed" ;; # Message for -a option -b) echo "-b option passed" ;; # Message for -b option -c) echo "-c option passed" ;; # Message for -c option --) shift # The double dash which separates options from parameters break ;; # Exit the loop using break command *) echo "Option $1 not recognized" ;; esac shift done total=1 for param in [email protected]; do echo "#$total: $param" total=$(($total 1)) done As you can see from the result, all the parameters after the double dash treated as parameters and not options.When you dig deep into Linux bash scripting, sometimes you need options with additional parameter values like this: #!/bin/sh las ## non existing command echo “Exit status is : ” $?on execution , it gives a non 0 exist status, indicating failure to execute: ././test.sh: line 2: las: command not found Exit status is : 127Lets create a test file to get the process id #!

not recognized" ;; esac shift done total=1 for param in [email protected]; do echo "#$total: $param" total=$(($total 1)) done As you can see from the result, all the parameters after the double dash treated as parameters and not options.When you dig deep into Linux bash scripting, sometimes you need options with additional parameter values like this: #!/bin/sh las ## non existing command echo “Exit status is : ” $?on execution , it gives a non 0 exist status, indicating failure to execute: ././test.sh: line 2: las: command not found Exit status is : 127Lets create a test file to get the process id #!

Leave a Reply

bruno mars skyler grey dating