Check That There Are at Least Two Arguments Given in a Bash Script

Check that there are at least two arguments given in a bash script

Use the $# special variable. Its value is the number of arguments. So if you have a script that contains only:

echo $#

and execute it like this:

thatscript foo bar baz quux

It'll print 4.

In your case you may want to do something like:

if [ $# -lt 2 ]; then
# TODO: print usage
exit 1
fi

Check existence of input argument in a Bash shell script

It is:

if [ $# -eq 0 ]
then
echo "No arguments supplied"
fi

The $# variable will tell you the number of input arguments the script was passed.

Or you can check if an argument is an empty string or not like:

if [ -z "$1" ]
then
echo "No argument supplied"
fi

The -z switch will test if the expansion of "$1" is a null string or not. If it is a null string then the body is executed.

How to check for the existence of a second argument

You could create a small script to look into how $# changes when you call the function with different numbers of arguments. For instance:

[Contents of "push.sh":]

push() {
echo $#
}

echo "First call, no arguments:"
push
echo "Second call, one argument:"
push "First argument"
echo "Third call, two arguments:"
push "First argument" "And another one"

If you put this in a script and run it, you'll see something like:

-> % ./push.sh
First call, no arguments:
0
Second call, one argument:
1
Third call, two arguments:
2

This tells you that the value of $# contains the number of arguments given to the function.

The if [ $# -eq 0 ] part you can add to the script, and change the 0 to some other numbers to see what happens. Also, an internet search for "bash if" will reveal the meaning of the -eq part, and show that you could also use -lt or -gt, for instance, testing whether a number is less than or greater than another.

In the end, you'll likely want to use something like the following:

a=$1
b=$2

if [ $# -lt 1 ]
then
a=$(timestamp)
fi

if [ $# -lt 2 ]
then
b=$(second thing)
fi

Check number of arguments passed to a Bash script

Just like any other simple command, [ ... ] or test requires spaces between its arguments.

if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
fi

Or

if test "$#" -ne 1; then
echo "Illegal number of parameters"
fi

Suggestions

When in Bash, prefer using [[ ]] instead as it doesn't do word splitting and pathname expansion to its variables that quoting may not be necessary unless it's part of an expression.

[[ $# -ne 1 ]]

It also has some other features like unquoted condition grouping, pattern matching (extended pattern matching with extglob) and regex matching.

The following example checks if arguments are valid. It allows a single argument or two.

[[ ($# -eq 1 || ($# -eq 2 && $2 == <glob pattern>)) && $1 =~ <regex pattern> ]]

For pure arithmetic expressions, using (( )) to some may still be better, but they are still possible in [[ ]] with its arithmetic operators like -eq, -ne, -lt, -le, -gt, or -ge by placing the expression as a single string argument:

A=1
[[ 'A + 1' -eq 2 ]] && echo true ## Prints true.

That should be helpful if you would need to combine it with other features of [[ ]] as well.

Take note that [[ ]] and (( )) are keywords which have same level of parsing as if, case, while, and for.

Also as Dave suggested, error messages are better sent to stderr so they don't get included when stdout is redirected:

echo "Illegal number of parameters" >&2

Exiting the script

It's also logical to make the script exit when invalid parameters are passed to it. This has already been suggested in the comments by ekangas but someone edited this answer to have it with -1 as the returned value, so I might as well do it right.

-1 though accepted by Bash as an argument to exit is not explicitly documented and is not right to be used as a common suggestion. 64 is also the most formal value since it's defined in sysexits.h with #define EX_USAGE 64 /* command line usage error */. Most tools like ls also return 2 on invalid arguments. I also used to return 2 in my scripts but lately I no longer really cared, and simply used 1 in all errors. But let's just place 2 here since it's most common and probably not OS-specific.

if [[ $# -ne 1 ]]; then
echo "Illegal number of parameters" >&2
exit 2
fi

References

  • Bash Conditional Expressions
  • Conditional Constructs
  • Pattern Matching
  • Word Splitting
  • Filename Expansion (prev. Pathname Expansion)
  • Simple Commands

How to check that a parameter was supplied to a bash script

Use $# which is equal to the number of arguments supplied, e.g.:

if [ "$#" -ne 1 ]
then
echo "Usage: ..."
exit 1
fi

Word of caution: Note that inside a function this will equal the number of arguments supplied to the function rather than the script.

EDIT: As pointed out by SiegeX in bash you can also use arithmetic expressions in (( ... )). This can be used like this:

if (( $# != 1 ))
then
echo "Usage: ..."
exit 1
fi

Simple bash shell script to ensure that provides two arguments

You should use:

#!/bin/sh
if [ $# -ge 2 ]
then
sleep $1
shift
banner $*
else
echo "Usage: Greeing seconds word(s)"
fi

You were missing:

  • a space after if
  • a space around [ and ]


Related Topics



Leave a reply



Submit