Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BASH scripts
# [] source bash_mucha.sh
#
# see in /etc/profile function "pathmunge"
check_var () {
varname=$1
varvalue=${!1}
echo "# check_var function #"
echo "varname="$varname
echo "varvalue="$varvalue
if [ -z $varvalue ]; then
echo $varname "not set and will be set to value" \'$2\'
declare -g $varname=$2
elif [[ ${!1} != *$2* ]]; then
echo $varname "value" \'$varvalue\' "without" \'$2\'
echo "TERBA DODAT"
declare -g $varname=$varvalue"-AND-"$2
else
echo $varname "value" \'$varvalue\' "already contain" \'$2\'
echo "UZ OBSAHUJE"
fi
echo
}
unset TEST_VAR1
unset TEST_VAR2
unset TEST_VAR3
TEST_VAR1="test1"
TEST_VAR2="test2"
set | grep TEST_VAR1
set | grep TEST_VAR2
set | grep TEST_VAR3
echo
check_var TEST_VAR1 test1
check_var TEST_VAR2 more2
check_var TEST_VAR3 test3
set | grep TEST_VAR1
set | grep TEST_VAR2
set | grep TEST_VAR3
echo;echo;echo
check_var TEST_VAR1 most1
check_var TEST_VAR2 most2
check_var TEST_VAR3 test3
set | grep TEST_VAR1
set | grep TEST_VAR2
set | grep TEST_VAR3
# before ! first ! run this script
# [] set > set_default
# run script
# [] source bash_set.sh
test_function() {
echo "test function"
TEST_VARF="testf"
}
TEST_VAR1="test1"
TEST_VAR2="test2"
set | grep TEST_VAR1
set | grep TEST_VAR2
set | grep test_function
set | grep TEST_VARF
echo
unset TEST_VAR2
unset test_function
set | grep TEST_VAR1
set | grep TEST_VAR2
set | grep test_function
set | grep TEST_VARF
echo
unset COMPREPLY
_kernel_versions
echo $COMPREPLY
# array without an index only gives the first element
unset 'COMPREPLY[0]'
_ip_addresses
echo ${COMPREPLY[0]}
if [ -z "$TEST_VAR1" ]; then
echo "TEST_VAR1 not set"
else
echo "TEST_VAR1 is set"
fi
if [ -z "$TEST_VAR2" ]; then
echo "TEST_VAR2 not set"
else
echo "TEST_VAR2 is set"
fi
echo
unset TEST_VAR2
# TEST_VAR2 will set to the value "" only if it is unset
: ${TEST_VAR2=""}
set | grep TEST_VAR2
: ${TEST_VAR2="empty string"}
set | grep TEST_VAR2
# TEST_VAR2 set to "" if TEST_VAR2 is unset or the empty string
: ${TEST_VAR2:="test2"}
set | grep TEST_VAR2
: ${TEST_VAR2:="already exists"}
set | grep TEST_VAR2
echo; echo "### diff ###"
set > set_after
diff set_default set_after
# [] source bash_var.sh
check_var () {
varname=$1
varvalue=${!1}
echo "# check_var function #"
echo "varname="$varname
echo "varvalue="$varvalue
if [ -z $varvalue ]; then
echo $varname "not set and will be set to value" \'$2\'
declare -g $varname=$2
# must give '-g' or the variable will be local
# identical # declare -g $1=$2
# eval is dangerous # eval $1='$2'
else
echo $varname "is set to value" \'$varvalue\'
fi
echo
}
unset TEST_VAR1
unset TEST_VAR2
unset TEST_VAR3
TEST_VAR1="test1"
TEST_VAR2=" "
set | grep TEST_VAR1
set | grep TEST_VAR2
set | grep TEST_VAR3
echo
check_var TEST_VAR1 already
check_var TEST_VAR2 test2
check_var TEST_VAR3 test3
set | grep TEST_VAR1
set | grep TEST_VAR2
set | grep TEST_VAR3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.