I've made this script which automates installing/uninstalling git. In my function for testing if git is installed, I use the git --version
command and test the return code.
I dislike the stderr output that occurs normally as I'm trying to make a nice custom output. Though I've figured out how to suppress stderr just for this function, I can't seem to reactivate it.
My read
prompt is now missing after calling this function.
function CheckGit() {
exec 3>&2 # link file desc 3 w/ stderr
exec 2> /dev/null
SILENT_MODE=$1
if [[ ! $(git --version) ]]; then
if [ SILENT_MODE ]; then
printf "${LT_RED} GIT IS NOT INSTALLED.\n"
fi
continue;
else
if [ SILENT_MODE ]; then
printf "${LT_BLUE} GIT IS CURRENTLY INSTALLED.\n"
fi
continue;
fi
GIT_INSTALLED=$?
#turn back on the stderr notifications
exec 2>&3 3>&- # Restore stdout and close file descriptor #3
}
while true; do
printf "${LT_BLUE} Menu\n"
printf " ***********************************************\n"
printf "${LT_GREEN} a) Check git.\n"
printf "${LT_GREEN} b) (More to be added)\n"
printf "${LT_GREEN} c) ...\n"
printf "${LT_GREEN} d) ...\n"
printf "${LT_GREEN} h) ...\n"
printf "${LT_RED} x) Exit.\n"
printf "\n${NC}"
read -p "Please make a selection: " eotuyx
case $eotuyx in
[Aa]* ) CheckGit true; continue;;
[Bb]* ) ...; continue;;
[Cc]* ) ...; continue;;
[Dd]* ) ...; continue;;
[Hh]* ) ...; continue;;
[XxQq]* ) break;;
* ) -e "\n${NC}" + "Please answer with a, b, c, d, x(or q).";;
esac
done
continue
inside theCheckGit
function. Those kind of "nonlocal gotos" are not only terribly confusing, but they also don't work the same in all shells: try this with dash, bash, zsh and ksh:sh -c 'foo(){ continue; }; for a in 1 2 3; do foo; echo $a; done'
– Nov 18 '18 at 00:27if [ SILENT_MODE ]; then ... fi
will be always true, becauseSILENT_MODE
is never an empty string ;-) – Nov 18 '18 at 00:34if [[ ... ]] then
toif ! git --version >/dev/null 2>&1; then
and get rid of all theexec
fd juggling. – Nov 18 '18 at 00:42