Created
November 18, 2019 11:05
-
-
Save madchap/e01927035d32e2b488c795a78c5a1781 to your computer and use it in GitHub Desktop.
bash vars checks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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