HomeLinuxCross a Named Argument in a Bash Script

Cross a Named Argument in a Bash Script


The command-line argument values could be handed within the Bash script in two methods. A technique is to make use of positional arguments equivalent to $1, $2, $3, and so on. The opposite means is to make use of the named arguments. Utilizing the positional argument shouldn’t be so helpful for accessing the argument values as a result of it’s not clearly outlined which positional argument incorporates which kind of knowledge. However the significant possibility can be utilized with the named argument that helps to know which argument incorporates which kind of knowledge. The makes use of of the named arguments in Bash are proven on this tutorial.

Makes use of of Getopts and Getopt

Each getopts and getopt are utilized in Bash to learn the named argument values. However there’s a distinction between these instruments. Getopts is used to learn the brief possibility equivalent to -h, -d, and so on. However it could actually’t learn the lengthy choices equivalent to –model, –person, and so on. Getopt is used to learn the worth of the lengthy possibility. If you wish to know extra particulars in regards to the getopts, you may test right here.

Instance 1: Learn the Named Arguments Utilizing Getopts
Create a Bash file with the next script that reads the 2 named arguments utilizing getopts that assist the brief choices. The -i and -r choices are used to move the named argument values in the course of the execution of the script. Subsequent, the argument worth that’s learn by the -i possibility is checked with a specific worth to print the output primarily based on the matching worth.

#Learn the argument values primarily based on the choices
whereas getopts “i:r:” var
do
   case $var in
       i) ID=${OPTARG};;
       r) GPA=${OPTARG};;
   esac
completed

#Print message primarily based on the matching ID worth handed within the argument
if [[ $ID == “56” ]]; then
    echo “Mir Sabbir obtained $GPA
elif [[ $ID == “34” ]]; then
    echo “Nirob Ahsan obtained $GPA
else
    echo “ID is invalid.”
fi

The script is executed twice within the following output. The script is executed with none argument within the first execution. So, the error message is displayed. The script is executed with two legitimate named arguments within the second execution. The formatted values of the named argument values are printed within the output:

Instance 2: Learn the Named Arguments Utilizing Getopt
Create a Bash file with the next script that reads the 2 named arguments utilizing getopt that helps the lengthy choices. The –electronic mail and –move choices are used to move the named argument values in the course of the execution of the script. The argument values which might be learn by the named arguments are saved in two variables named $E-mail and $Password. Subsequent, the values of those variables are in contrast with the actual values and print the output primarily based on the output of the comparability.

#!/bin/bash

#Set the choices of the getopt command
format=$(getopt -n “$0” -l “electronic mail:,move:” “$@”)
if [ $# -lt 3 ]; then
   echo “Mistaken variety of arguments are handed.”
   exit
fi
eval set $format

#Learn the argument values
whereas [ $# -gt 0 ]
do
     case “$1” in
          –email) E-mail=“$2”; shift;;
          –pass) Password=“$2”; shift;;
          —) shift;;
     esac
     shift;
completed

#Evaluate the argument values with the actual worth
if [[ $Email == [email protected] && $Password == “secretpass” ]]; then
    echo “Legitimate person”
else
    echo “Invalid person”
fi

The script is executed thrice within the following output. The script is executed with none argument within the first execution. So, the error message “Mistaken variety of arguments are handed” is displayed. The script is executed with two argument values within the second execution however the worth of the second argument didn’t match the password worth. So, the message “Invalid person” is displayed. The script is executed with two legitimate named arguments within the third execution and each argument values matched with the values which might be supplied within the “if” situation. So, the message “Legitimate person” is displayed.

Instance 3: Learn the Named Argument with out Getopts and Getopt
Create a Bash file with the next script that reads the three named arguments utilizing the “whereas” loop. In keeping with the script, each brief and lengthy choices can be utilized to move the named arguments within the script. The -u or –person, -p or –move, and -h or –host choices are used to move the three named argument values in the course of the execution of the script. Subsequent, the argument values are learn and saved into three variables utilizing the “whereas” loop and the “shift” command.

#!/bin/bash
#Learn the argument values
whereas [[ “$#” -gt 0 ]]
  do
    case $1 in
      -u|–user) Person=“$2”; shift;;
      -p|–pass) Password=“$2”; shift;;
      -h|–host) Host=“$2”; shift;;
    esac
    shift
completed
#Print the argument values
printf “username: $Person npassword: $Password nhostname: $Hostn

The script is executed with three legitimate named arguments within the following output. So, the values of the three arguments are printed:

Conclusion

The other ways of utilizing the named arguments within the Bash script are proven on this tutorial to assist the Bash customers know how one can use the named arguments.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments