Monday, December 23, 2024
HomeNetworkingVerifying bash script arguments | Community World

Verifying bash script arguments | Community World


Many bash scripts use arguments to regulate the instructions that they’ll run and the knowledge that will likely be supplied to the individuals working them. This submit examines plenty of methods that you would be able to confirm arguments once you put together a script and need to be sure that it is going to just do what you propose it to do – even when somebody working it makes a mistake.

Displaying the script identify, and many others.

To show the identify of a script when it’s run, use a command like echo $0. Whereas anybody working a script will undoubtedly know what script they only invoked, utilizing the script identify in a utilization command might help remind them what command and arguments they need to be offering.

Script names could be displayed together with the anticipated arguments like this:

echo Utilization: $0 month yr

The $0 argument represents the identify of the script.

Utilization statements are sometimes displayed when a person doesn’t enter the correct arguments to run a script. A utilization assertion will remind individuals what is predicted – particularly after they run the script very sometimes.

Checking the variety of arguments supplied

To show the variety of arguments supplied by the person, you should utilize a command like echo $#. That $# represents the variety of arguments supplied by the person. Your script can confirm that it’s what the script requires. Right here’s an instance:

if [ $# != 2 ]; then
  echo $0 username date (e.g., 12/01/2023)

The code above asks if the variety of arguments supplied is not equal to 2. You may also use instructions like these;

if [ $# == 2]        # if variety of arguments equals 2
if [ $# -lt 3 ]      # if variety of arguments is lower than 3
if [ $# -gt 4 ]      # if variety of arguments is greater than 4

The == (equals), lt (lower than) and gt (higher than) comparisons are sometimes used to confirm that the right variety of arguments have been supplied by the individual working the script.

Verifying the arguments

If a script goes to work with a file, you may need to verify that the required file exists earlier than the script runs a command that’s meant to course of or study it. For instance:

filename=$1     # first argument needs to be filename
if [ ! -e $filename ]; then
  echo “No such file: $filename”
fi

The ! -e a part of the command above exams “if there isn’t any file with the identify”. You may also have a script confirm file permissions. For instance, to verify whether or not the individual working the script can learn, write or execute a file, you should utilize instructions similar to these:

if [ ! -r $1 ]; then
  echo can't learn $1
fi
if [ ! -x $1 ]; then
  echo can't run $1
fi
if [ ! -w $1 ]; then
echo can't write to $1
fi

The individual working the script may see one thing like this:

$ countlines thatfile
can't learn thatfile

Verifying argument sorts

There are a selection of how that you would be able to confirm the kind of arguments supplied to a script. For instance, you’ll be able to verify whether or not an argument is a quantity or a string of letters. You possibly can even verify if the arguments embody a month and a yr. Listed here are some examples of find out how to do this stuff:

Test if numeric

The code under ensures the argument supplied is a single string of digits. The $re variable units up the sample (all digits).

# verify if argument is numeric
re="^[0-9]+$"
if ! [[ $2 =~ $re ]] ; then
   echo "error: Not a quantity"
   exit 2
fi

Test if a string of characters

The code under ensures that the argument is a string of characters. You possibly can elect to verify if it accommodates solely lowercase or uppercase letters.

To verify if an argument accommodates solely lowercase letters:

# verify argument is characters
re="^[a-z]+$"
if ! [[ $1 =~ $re ]] ; then
   echo "error: Not alphabetic"
   exit 3
fi

To verify if an argument accommodates solely uppercase letters:

# verify argument is characters
re="^[A-Z]+$"
if ! [[ $1 =~ $re ]] ; then
   echo "error: Not alphabetic"
   exit 3
fi

To verify if an argument accommodates solely letters (uppercase or lowercase):

# verify argument is characters
re="^[a-zA-Z]+$"
if ! [[ $1 =~ $re ]] ; then
   echo "error: Not alphabetic"
   exit 3
fi

NOTE: The order of the letters within the argument supplied is unimportant for these exams. The “re” (common expression) exams merely make sure that the argument offers solely characters within the vary specified.

Looping by way of script arguments

The command under will loop by way of all the arguments which can be supplied when a script is run.

# show listing of arguments
for arg in "$@"
do
    echo "$arg"
performed

The $@ string represents the listing of arguments supplied. The for command then loops by way of them, displaying them one after the other.

Checking command-specific syntax

The command proven under will confirm that the syntax for a particular command that the script is predicted to run – on this case, the cal (calendar) command – is right. It does this in a really attention-grabbing means. It runs the command and sends its output to /dev/null (the “bit bucket”). If the error code returned signifies that the command failed in a roundabout way (i.e., the return code is bigger than zero), the script tells the person the command failed. In any other case, it runs the command once more, displaying the output anticipated.

# verify if yr/month are legitimate
cal $1 $2 >/dev/null
if [ $? != 0 ]; then # error encountered
echo Invalid month and/or yr exit 4 else cal $1 $2 fi

Wrap-up

There are various methods to confirm the arguments supplied to a script when it’s run. That is particularly helpful if you find yourself getting ready scripts that different individuals – particularly much less script-savvy individuals than your self – will find yourself working.

Copyright © 2023 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments