Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash how to detect missing mandatory arguments passed to a script

Tags:

bash

I have a Bash script and I need to pass a certain number of arguments to make it work.

./upload.sh $ARG1 $ARG2 $ARG3

Let's say that the 2 mandatory fields are ARG1 and ARG2, and
ARG1 and 3 are not empty.

I think the script will run and think that it has the 2 mandatory arguments, Is there a way to detect that ARG2 is missing/empty? I need to return exit 1 and not exit 0.

Here's a bit of the script

RESOURCE=$1
CONTAINER=$2
APP_NAME=$3

if [[ -z $RESOURCE || -z $CONTAINER ]];
then
    echo `date`" - Missing mandatory arguments: resource and container. "
    echo `date`" - Usage: ./upload.sh  [resource] [container] [appname] . "
    exit 1
fi

Thanks in advance,

Alain

like image 984
Alain Avatar asked Nov 07 '13 17:11

Alain


People also ask

How do you know if an argument is passed in Bash?

Use for loop in Bash and traverse through all passed arguments using $@ argument. Inside for loop check, the provides argument is the directory or not using the if statement using the -d option for the first argument using $1 parameter. If it is true then print the message that the provided argument is the directory.

What does $@ do in Bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do you access these arguments from within the script?

Using arguments Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2 , and so on. The $0 will contain the script name.

What does $f mean in Bash?

The “[ -f ~/. bashrc]” is known as a test command in bash. This command, which includes the “-f” expression, will check if the ~/. bashrc file exists (i.e. the file .


4 Answers

I like:

VARNAME=${1:?"missing arg 1 for VARNAME"}
like image 21
Evan Closson Avatar answered Nov 15 '22 15:11

Evan Closson


I always use a little function to check all the arguments:

process_arguments() {
    while [ -n "$1" ]
    do
        case $1 in
            -h|--help) echo "some usage details"; exit 1;;
            -x) do_something; shift; break;;
            -y) do_something_else; shift; break;;
            *) echo "some usage details"; exit 1;;
        esac
        echo $1; shift
    done
}

This way if you miss anything, the proper usage details will be displayed the script will exit. Otherwise, you can enter the args in any order. When your script starts, just call

process_arguments "$@"

and you should be all set.

like image 180
user3270760 Avatar answered Nov 15 '22 16:11

user3270760


Is there a way to detect that ARG2 is missing/empty?

NO. The way you're passing the arguments, those would be interpreted as arguments 1 and 2.

You could instead say:

./upload.sh "$ARG1" "$ARG2" "$ARG3"

in order to make bash interpret the arguments correctly whether those are empty or not.


Example:

$ cat file
[ -z $1 ] && echo "Arg 1 missing"
[ -z $2 ] && echo "Arg 2 missing"
[ -z $3 ] && echo "Arg 3 missing"
$ bash file "$HOME" "$FOOBAR" "$USER"
Arg 2 missing

(The variable FOOBAR was undefined in the above example.)

like image 41
devnull Avatar answered Nov 15 '22 14:11

devnull


Args as you are using them are positional so is you only provided 1 and 3, but 1 and 2 are require, you just accidentally provided a value for arg 3 as arg 2.

You could require that your optional arguments start with a dash -, and if you find either $1 or $2 start with a dash you know you are missing a required. You of course would have to remove the dash from $3.

A better way to handle args is to use getopts. Here are a couple of resources to get you started:

  • http://wiki.bash-hackers.org/howto/getopts_tutorial
  • http://rsalveti.wordpress.com/2007/04/03/bash-parsing-arguments-with-getopts/

This will allow you to handle args in a way similar to standard commandline utils. If you write commandline utils in C you have probably used the C library version of getops before.

like image 45
voidlogic Avatar answered Nov 15 '22 15:11

voidlogic