Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if [ -z "$STATE" ]; then
- echo "Need to set STATE"
- exit 1
- fi
- if [ -z "$DEST" ]; then
- echo "Need to set DEST"
- exit 1
- fi
- : ${STATE?"Need to set STATE"}
- : ${DEST:?"Need to set DEST non-empty"}
- : "${STATE?Need to set STATE}"
- : "${DEST:?Need to set DEST non-empty}"
- ${parameter:?word}
- In script.sh line 13:
- : ${FOO:?"The environment variable 'FOO' must be set and non-empty"}
- ^-- SC2086: Double quote to prevent globbing and word splitting.
- : "${STATE?Need to set STATE}"
- : "${DEST:?Need to set DEST non-empty}"
- $ mkdir junk
- $ cd junk
- $ > abc
- $ > def
- $ > ghi
- $
- $ x="*"
- $ printf "%sn" ${x:?You must set x} # Careless; not recommended
- abc
- def
- ghi
- $ unset x
- $ printf "%sn" ${x:?You must set x} # Careless; not recommended
- bash: x: You must set x
- $ printf "%sn" "${x:?You must set x}" # Careful: should be used
- bash: x: You must set x
- $ x="*"
- $ printf "%sn" "${x:?You must set x}" # Careful: should be used
- *
- $ printf "%sn" ${x:?"You must set x"} # Not quite careful enough
- abc
- def
- ghi
- $ x=
- $ printf "%sn" ${x:?"You must set x"} # Not quite careful enough
- bash: x: You must set x
- $ unset x
- $ printf "%sn" ${x:?"You must set x"} # Not quite careful enough
- bash: x: You must set x
- $
- [ -z "$STATE" ] && echo "Need to set STATE" && exit 1;
- ${MyVariable:=SomeDefault}
- ${MyVariable:-SomeDefault}
- ${MyVariable:=SomeDefault}
- MyVariable=${MyVariable:=SomeDefault}
- if [ "$MYVAR" = "" ]
- then
- echo "Does not exist"
- else
- echo "Exists"
- fi
- if [ "x$STATE" == "x" ]; then echo "Need to set State"; exit 1; fi
- $ unset a
- $ b=
- $ c=
- $ [[ -v a ]] && echo "a is set"
- $ [[ -v b ]] && echo "b is set"
- b is set
- $ [[ -v c ]] && echo "c is set"
- c is set
- mapfile -t arr < variables.txt
- EXITCODE=0
- for i in "${arr[@]}"
- do
- ISSET=$(env | grep ^${i}= | wc -l)
- if [ "${ISSET}" = "0" ];
- then
- EXITCODE=-1
- echo "ENV variable $i is required."
- fi
- done
- exit ${EXITCODE}
- #
- # assert if variables are set (to a non-empty string)
- # if any variable is not set, exit 1 (when -f option is set) or return 1 otherwise
- #
- # Usage: assert_not_null [-f] variable ...
- #
- function assert_not_null() {
- local fatal var num_null=0
- [[ "$1" = "-f" ]] && { shift; fatal=1; }
- for var in "$@"; do
- [[ -z "${!var}" ]] &&
- printf '%sn' "Variable '$var' not set" >&2 &&
- ((num_null++))
- done
- if ((num_null > 0)); then
- [[ "$fatal" ]] && exit 1
- return 1
- fi
- return 0
- }
- one=1 two=2
- assert_not_null one two
- echo test 1: return_code=$?
- assert_not_null one two three
- echo test 2: return_code=$?
- assert_not_null -f one two three
- echo test 3: return_code=$? # this code shouldn't execute
- test 1: return_code=0
- Variable 'three' not set
- test 2: return_code=1
- Variable 'three' not set
- #!/bin/bash
- declare -a vars=(NAME GITLAB_URL GITLAB_TOKEN)
- for var_name in "${vars[@]}"
- do
- if [ -z "$(eval "echo $$var_name")" ]; then
- echo "Missing environment variable $var_name"
- exit 1
- fi
- done
- if (set -u; : $HOME) 2> /dev/null
- ...
- ...
- is_this_an_env_variable ()
- local var="$1"
- if env |grep -q "^$var"; then
- return 0
- else
- return 1
- fi
- }
- if [ $?BLAH == 1 ]; then
- echo "Exists";
- else
- echo "Does not exist";
- fi
Add Comment
Please, Sign In to add comment