Skip to content

Instantly share code, notes, and snippets.

@madchap
Created November 18, 2019 11:05
Show Gist options
  • Select an option

  • Save madchap/e01927035d32e2b488c795a78c5a1781 to your computer and use it in GitHub Desktop.

Select an option

Save madchap/e01927035d32e2b488c795a78c5a1781 to your computer and use it in GitHub Desktop.
bash vars checks
function check_var_empty() {
# make sure the variables hold some value
for var in "${vars_to_check[@]}"; do
[[ "x${!var}" == "x" ]] && echo "The variable $var is empty, and should not. Will abort." && VAR_EMPTY=1
done
[[ ! -z $VAR_EMPTY ]] && exit -1
}
function sanitize() {
# only allow the characters we want/need
for var in "${vars_to_check[@]}"; do
eval $var=${!var//[^a-zA-Z0-9_\/\.-]/}
done
}
vars_to_check=( "TEST1" "TEST2" )
check_var_empty $vars_to_check
sanitize $vars_to_check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment