"$PRINTF"'%s\n'"Requesting permission to invoke '$2' as privileged user (y/n)"
read -r privilege_granted
case"$privilege_granted" in
"Y"|"y"|"YES"|"yes")
edebug "User granted permission to invoke '$2' as privileged user"
unset privilege_granted
break
;;
"N"|"n"|"NO"|"no")
die 3"Unable to execute '$2' as privileged user"
;;
*)"$PRINTF"'%s\n'"Input '$privilege_granted' is not recognized, try again.."
esac
done
# Check what we can use for executing command as privileged user
unset privilege_commands
# FIXME-QA: Fix duplicate code
## Check for sudo
ifcommand -v "$SUDO" 1>/dev/null;then
privilege_commands="$privilege_commands sudo"
elif ! command -v "$SUDO" 1>/dev/null;then
edebug "Command '$SUDO' is not executable in $funcname, unable to use it"
else
die bug "checking wether command sudo is executable in $funcname"
fi
## Check for su
ifcommand -v "$SU" 1>/dev/null;then
privilege_commands="$privilege_commands su"
elif ! command -v "$SU" 1>/dev/null;then
edebug "Command '$SU' is not executable in $funcname, unable to use it"
else
die bug "checking wether command su is executable in $funcname"
fi
case"$("$PRINTF"'%s\n'"$privilege_commands"| tr ' ''\n'| wc -l)" in
0) die 3"Neither of supported commands used to invoke command as privileged user '$privilege_commands' are available on this system, unable to invoke '$2'";;
1)
cmd_count="$("$PRINTF"'%s\n'"$privilege_commands"| sed "s/ //gm")"
case"$cmd_count" in
"sudo")
while true;do
printf'%s\n'"Requesting permission to use '${SUDO:-sudo}' for invokation of '$2' (y/n)"
read -r allowed_to_use_sudo
case"$allowed_to_use_sudo" in
"Y"|"y"|"YES"|"yes")
sudo "$2"| die 3"Unable to execute '$2' with privileged permission"
break
;;
"N"|"n"|"NO"|"no")
die 3"Unable to execute '$2' with privileged permission using sudo, because we were not allowed to proceed"
;;
*)
"$PRINTF"'%s\n'"Input '$allowed_to_use_sudo' is not recognized, retrying.."
unset allowed_to_use_sudo
esac
done
;;
"su")
while true;do
printf'%s\n'"Requesting permission to use '${SUDO:-sudo}' for invokation of '$2' (y/n)"
read -r allowed_to_use_sudo
case"$allowed_to_use_sudo" in
"Y"|"y"|"YES"|"yes")
su root -c "$2"| die 3"Unable to execute '$2' with privileged permission"
break
;;
"N"|"n"|"NO"|"no")
die 3"Unable to execute '$2' with privileged permission using sudo, because we were not allowed to proceed"
;;
*)
"$PRINTF"'%s\n'"Input '$allowed_to_use_sudo' is not recognized, retrying.."
unset allowed_to_use_sudo
esac
done
;;
esac;;
2)
# NOTICE: This is adapted to allow more commands in the future
while true;do
printf'%s\n\n'\
"We found following commands that we can use to execute the command as privileged user:"
# FIXME: seq might not be available on the system
# - yes | head -n 3| nl | cut -f1 | while read i; do echo $i; done
# - awk 'BEGIN{for(i=0;i<10;i++)print i}
for num in $(seq 1"$cmd_count"| tr '\n'' ');do
for cmd in $privilege_commands;do
printf"%s\n""$num. $cmd"
done
printf'%s\n'""
printf'%s\n'"Which command do you want to use?"
done
read -r privilege_choice
case"$privilege_choice" in
[1-2])
die fixme "Choose the appropriate choice, invoke this script as privileged user as a workaround"