Passing arguments in shell script

By | 23rd August 2020

Command line arguments are essential for any programming or scripting language to have a control of the the input parameters. Passing arguments to the shell script can be done in multiple ways. In this blog, we will see the two ways of passing arguments to a shell script

Option 1 – Using getops

This method is the most flexible and standard way of passing the arguments. In this method, you can pass the arguments in any order provided you use the corresponding character. Also you can change the number of arguments dynamically.

usage="$(basename $0) -d <Database> -t <TableName> -i <Id> -u <User> -r <RunDate>"
while getopts "d:t:r:i:u:s:" arg; do
  case ${arg} in
  d)
    database="${OPTARG}"
    ;;
  t)
    tablename="${OPTARG}"
    ;;
  i)
    id="${OPTARG}"
    ;;
  u)
    dbuser="${OPTARG}"
    ;;
  r)
    rundate="${OPTARG}"
    ;;
  *)
    echo "ERROR: Invalid input ${arg}"
    echo "${usage}"
    exit 1
    ;;
  esac
done
databaseuser=${dbuser:-defaultuser}

echo ""
echo "Arguments List"
echo "--------------"
echo "Database	: $database"
echo "Tablename	: $tablename"
echo "Id	: $id"
echo "Dbuser	: $databaseuser"
echo "Rundate	: $rundate"
echo ""

exit 0

The echo statements are self-explanatory. Let us see the purpose of other commands in the script.

usage=”$(basename $0) -d -t -i -u -r “ => This variable will hold the warning message that needs to be displayed if a wrong argument is passed.

while getopts “d:t:r:i:u:s:” arg; => This will show the list of arguments that can be passed to the program.

case ${arg} in – This is like the regular switch case statement. Whichever character matches, it will take control to the corresponding case.

databaseuser=${dbuser:-defaultuser} => If the dbuser argument is not passed, this statement will assign the default user. This is a kind of error handling technique too.

Example 1 – Passing all arguments

$ sh args.sh -d testdb -t testtable -i 10 -r 20200810 -u readuser

The result of the above script execution is shown below.

Arguments List
--------------
Database	: testdb
Tablename	: testtable
Id		: 10
Dbuser		: readuser
Rundate		: 20200810

Example 2 – Without one argument

sh args.sh -d testdb -t testtable -i 10 -r 20200810

As you can see, we purposely missed the “-u readuser” argument in this case. But, you can also see that the program smartly setting the default value “defaultuser”

Arguments List
--------------
Database	: testdb
Tablename	: testtable
Id		: 10
Dbuser		: defaultuser
Rundate		: 20200810

Option 2 – Directly passing args with space

In this option, you simply pass all the arguments with space in between them. Inside the program, you can assign the values to the local variables. This will not have the flexibility of sending arguments in a different order and you have to pass all the arguments as well.

main() {
usage="$(basename $0) database tablename username id rundate"
echo "Number of args: $#"
if [[ $# -lt 5 ]];then
   echo "ERROR: Pass all the args properly."
   echo "Example - ${usage}"
   exit 1
fi
echo "Received all the arguments"

echo "Arguements list"
echo "---------------"
echo "Database	: ${1}"
echo "Tablename	: ${2}"
echo "Username	: ${3}"
echo "Id	: ${4}"
echo "Rundate	: ${5}"
echo ""
exit 0
}

main "$@"

if [[ $# -lt 5 ]];then – This will check if the number of arguments is less than the expectation. This is a predefined number so do not have the flexibility to change it during the run time.

main “$@” – This is the entry of the execution. This will receive all the arguments passed and send it to the main function.

Example 1 – Passing all arguments

$ sh argsbasic.sh testdb testtable adminuser 50 20200806

Here we have passed all the arguments in the expected order. Remember if we change the order of arguments, the program will never know. It can only check for a number of arguments. The result is shown below.

Number of args: 5
Received all the arguments
Arguements list
---------------
Database	: testdb
Tablename	: testtable
Username	: adminuser
Id	        : 50
Rundate	        : 20200806

Example 2 – Without one argument

$ sh argsbasic.sh testdb testtable adminuser 50

In this case, we purposely left the last argument to see the behavior of the program. Since the program is expecting 5 arguments, it will throw an error in this case.

Number of args: 4
ERROR: Pass all the args properly.
Example - argsbasic.sh database tablename username id rundate

Also, check “How to iterate through the list using for loop”, “How to read delimited file in shell script

Leave a Reply

Your email address will not be published. Required fields are marked *