Shell script - exit the script if the variable is empty or empty

I expect to use the following variable in my bash script, but in case it is empty or null, which would be the best way to handle it and exit the script.

tag=$1 

I see answers with 'set -u'. I know this will work, but is it good for a production environment?

+12
source share
7 answers

There is a built-in statement requiring a variable to be set. This will lead to the output of the script if it is not.

 tag=${1?Need a value} 

This is usually used with : no-op next to the start of the script.

 : ${1?Need a value} 

The merge of "unset or empty" is slightly different. There is no similar construction to exit an empty but set value, but you can easily use the associated ${var:-default} , which expands to $var if it is installed and not empty, and default otherwise. There is also ${var-default} , which produces only default if the variable is not set correctly.

This can be especially useful if you want to use set -u , but you need to deal with a possible undefined variable:

 case ${var-} in '') echo "$0: Need a value in var" >&2; exit 1;; esac 

I somewhat prefer case over if [ "${var-}" = '' ] , mainly because it bothers me to wrap double quotes around ${var-} , and the sluggish case of the value is in $var , which is interpreted as an option for [ and gives an error message when you least expect it. (In Bash, [[ does not have these problems, but I prefer to stick with the POSIX shell when I can.)

+22
source

If you want to check that the variable is not empty, you can do this:

 if [ -z "$tag" ]; then exit 1 fi 

From the manual for test :

-z STRING

length STRING is zero

Given that you are using positional arguments in a script, you can also check the number of arguments you received by looking at $# .

+12
source

No one suggested :? option.

If you want to make sure that the variable is set and not equal to zero:

 SEARCH_PATH=${DAYS_TO_KEEP:?Must provide DAYS_TO_KEEP.} 

It will immediately exit with code 2 and print a message:

 /entrypoint.sh: line 17: SEARCH_PATH: Must provide DAYS_TO_KEEP. 
+5
source

I'm not sure if you want to determine if the variable is unset or empty . These are two different things. In particular, a variable can be set, but it will be empty:

 $ var="" $ if [ -z "$var" ]; then echo empty; fi $ empty 

The same thing happens here:

 #!/usr/bin/env bash set -u echo $1 

Test:

 $ ./test.sh ./test.sh: line 4: $1: unbound variable $ ./test.sh "" $ 

Or here:

 #!/usr/bin/env bash tag=${1?Need a value} echo $tag 

Test:

 $ ./se.sh ./se.sh: line 3: 1: Need a value $ ./se.sh "" $ 

Other posters presented the correct ways to detect an unrelated and empty variable. Personally, I like this way of detecting empty and undefined variables:

 #!/usr/bin/env bash if [ "$1"A = A ] then echo variable is empty or unset fi 

Test:

 $ ./empty.sh "" variable is empty or unset $ ./empty.sh variable is empty or unset $ ./empty.sh 1 $ 
+2
source

I rather liked the way Perl uses "die", and it's easy to do something like this in a shell.

 # Print (optional) error message and exit # Usage: die [[msg] exit_status] die() { [[ -n "$1" ]] && echo "$1" [[ -n "$2" ]] && exit $2 || exit 1 } [[ -n "$tag" ]] || die "Need a tag argument. Use $0 --help for details" 

This assumes a bash or Korn shell, but you can convert it to the classic Bourne shell by changing [[]] to [] .

+1
source

Use pattern matching to determine if a value contains only a space:

 pattern=$'*( |\t)' if [[ $1 = $pattern ]]; then echo "First parameter has no non-whitespace characters" exit 1 fi 

The quote $'...' makes it easy to add a tab to a string. An extended pattern *(...) matches 0 or more patterns inside parentheses (similar to the regular expression ( |\t)* ). A template is assigned to a variable because = performs exact string matching if any part of its right operand is quoted, so we do the quoting in advance to simplify setting the value.

0
source

The following test ensures that the variable is Null or the value assigned to it. Double quotes are very important and should be used!

 VAL= # Creates a NULL variable if [[ -z "$VAL" && "$VAL" = '' ]] then echo "The VAL variable is NULL" fi or VAL=25 if [[ ! -z "$VAL" && "$VAL" != '' ]] then echo "The VAL variable is NOT NULL" fi 
0
source

All Articles