Command Line Arguments
In many instances, bash scripts require argument values to supply enter choices to the script. You can maintain command-line arguments in a bash script in two tactics. One is via the usage of argument variables, and any other is via the usage of the getopts serve as. How you’ll be able to maintain command-line arguments is proven on this instructional.
Using argument variables:
The argument variable begins from $0. The major script document identify is saved in $0, which receives argument values from command line arguments. If two arguments are handed within the command-line, then sequentially, the argument values shall be gained in $1 and $2 variables.
Example -1: Sending 3 numeric values within the command line arguments
Create a bash document with the next script. The script will obtain three-argument values and retailer them in $1, $2, and $3 variables. It will rely the entire collection of arguments and print argument values the usage of a loop with out a loop. The sum of all argument values shall be revealed later.
# Counting overall collection of arguments
echo “Total number of arguments: $#”
# Reading argument values in my opinion
echo “First argument value : $1”
echo “Second argument value : $2”
echo “Third argument value : $3”
# Reading argument values the usage of loop
for argval in “[email protected]”
do
echo -n “$argval “
completed
# Adding argument values
sum=$(($1+$2+$3))
# print the end result
echo -e “nResult of sum = $sum“
The following output will seem after executing the script document with 3 numeric argument values, 50, 35, and 15.
$ bash cmdline1.sh 50 35 15
Example -2: Taking filename as an issue
Create a bash document with the next script to rely the entire collection of characters of any document. Here, the filename shall be handed as a command-line argument.
#!/bin/bash
filename=$1
totalchar=`wc -c $filename`
echo “Total number of characters are $totalchar“
Run the bash script with the filename as a unmarried argument price and run any other command to test that document’s overall collection of characters. Here, the weekday.txt document is used as an issue price. The overall collection of characters within the weekday.txt document is 57.
$ bash cmdline2.sh weekday.txt
$ wc -c weekday.txt
Using getopts serve as:
If you need to retailer knowledge within the database or any document or create a document in a selected structure according to command line arguments values, then the getopts serve as is the most suitable choice to do the duty. It is a integrated Linux serve as. So, you’ll be able to simply use this serve as for your script to learn formatted knowledge from the command line.
Example -3: Reading arguments via getopts serve as
Create a bash document with the next script to grasp using the getopts serve as. ‘getopts’ serve as is used with some time loop to learn command-line argument choices and argument values. Here, 4 choices are used that are ‘i’, ‘n’, ‘m’ and ‘e’. the case commentary is used to check the precise choice and retailer the argument price in a variable. Finally, print the values of the variable.
#!/bin/bash
whilst getopts “:i:n:m:e:” arg; do
case $arg in
i) ID=$OPTARG;;
n) Name=$OPTARG;;
m) Manufacturing_date=$OPTARG;;
e) Expire_date=$OPTARG;;
esac
completed
echo -e “n$ID $Name $Manufacturing_date $Expire_daten“
Run the document with the next choices and argument values. Here, p100 is the worth of -i choice, ‘Hot Cake‘ is the worth of -n choice, ‘01-01-2021‘ is the worth of -m choice and ‘06-01-2021‘ is the worth of -e choice.
$ bash cmdline3.sh -i p001 -n ‘Hot Cake’ -m ’01-01-2021′ -e ’06-01-2021′
When you wish to have to ship easy values in a script, then it’s higher to make use of argument variables. But if you wish to ship knowledge in a formatted method, it’s higher to make use of the getopts serve as to retrieve argument values. The makes use of of each argument variables and getopts choices have proven within the subsequent instance.
Example-4: Reading customary arguments and arguments with getopts choices
The tactics to learn command-line arguments the usage of argument variables and getopts choices had been proven one at a time in earlier examples of this instructional. But those two tactics can be utilized in one script to learn command-line argument values. Create a bash script with the next code to learn the command line argument values handed via getopts choices and argument parameters. Here, 3 getopts choices had been used to learn the command line’s hostname, username, and password. Next, shift command has been used to take away all getopts choices from the command for studying the command line values the usage of argument variables. This script will learn and print a most of 3 values of the argument variables. If no argument price with out choice shall be given after executing the code, then a message shall be revealed; differently, the values of the argument variable shall be revealed.
# Reading arguments with getopts choices
whilst getopts ‘h:u:p:’ OPTION; do
case “$OPTION“ in
h)
# Print hostname
echo “The host name is $OPTARG“ ;;
u)
# Print username
echo “The username is $OPTARG“ ;;
p)
# Print password
echo “The password is $OPTARG“ ;;
*)
# Print serving to message for offering unsuitable choices
echo “Usage: $0 [-h value] [-u value] [-p value]” >&2
# Terminate from the script
go out 1 ;;
esac
completed
# Remove all choices handed via getopts choices
shift “$(($OPTIND -1))”
# Reading first customary arguments
if [ ! -z $1 ]; then
echo “The first table name is $1”
else
echo “No normal argument is given.”
go out 1
fi
# Reading 2nd customary arguments
if [ ! -z $2 ]; then
echo “The second table name is $2”
fi
# Reading 3rd customary arguments
if [ ! -z $3 ]; then
echo “The third table name is $3”
fi
The following output will seem if the unsuitable choice is given on the time of executing the code. Here, choice -a does no longer exist within the script.
The following output will seem if the legitimate choices with the values are given within the command line on the time of executing the code.
The following output will seem if the legitimate choices and customary argument values are used within the command line on the time of executing the code. Here, the traditional arguments are buyer and worker.
Using ‘[email protected]’ for studying command-line arguments:
The command-line arguments may also be learn with out the usage of argument variables or getopts choices. Using ‘[email protected]‘ with the primary bracket is differently to learn all command-line argument values.
Example-5: Reading command line argument values with out variable
Create a bash document with the next script to learn the argument values with none argument variable and calculate the sum of 3 command line argument values. “[email protected]” has been used with the primary brackets right here to learn all argument values into an array. Next, the sum of the primary 3 array values shall be revealed.
# Read all arguments values
argvals=(“[email protected]”)
# Check the entire collection of arguments
if [ $# -gt 2 ]
then
# Calculate the sum of 3 command line arguments
sum=$((${argvals[0]}+${argvals[1]}+${argvals[2]}))
echo “The sum of 3 command line arguments is $sum“
fi
The following output will seem after executing the above script for the argument values 12, 20, and 90. The sum of those numbers is 122.
Conclusion:
The tactics to supply command-line argument values with none choice and with choices have proven right here. The approach to learn command-line argument values with out the usage of variables has proven right here additionally. I’m hoping this instructional will lend a hand the readers to make use of command-line argument values correctly of their bash script.