Reading the sudo man page, I see that the -v flag can be used to check if the user has sudo privileges in his workstation. I have a piece of script that needs to test it. If the user has not sudo privileges, it prints on screen:
Sorry, user tester may not run sudo on debian.
How can I suppress this message and just execute the rest of the code?
Try to append >/dev/null
in your command. In case the message is printed in stderr then use 2>/dev/null
or as advised in comments use &>/dev/null
to redirect both stdout and stderr to null.
If can_auto_sudo=$(sudo -l -n sudo &>/dev/null; echo $?)
is 0, you can sudo as much as you want.
[ $EUID -eq 0 ] || exit 1
[ $EUID -eq 0 ] || exit 1
# Exit if not effectively root
sudo -l /usr/bin/program &>/dev/null || exit 2
sudo -l /usr/bin/program &>/dev/null || exit 2
# Exit if it can't run this as sudo
sudo -l -n /usr/bin/program &>/dev/null || exit 3
sudo -l -n /usr/bin/program &>/dev/null || exit 3
# Exit if requires interaction
`sudo -ll -U $USER # tells you which commands can be runs with sudo by user (have to parse yourself)
[[ "$(printenv SUDO_USER)" = "" ]] || echo "$SUDO_USER is sudoing!" && exit 5
Using sudo -l or --list As per the man page, sudo can be used with -l or --list to get the list of allowed and forbidden commands for any particular user.
The syntax would be: sudo -l [-AknS] [-a type] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
If we use sudo --list without any arguments, then it will print the list of allowed and forbidden commands for the user who is executing the sudo command
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.