Last active
April 30, 2024 03:34
-
-
Save joshgachnang/2306795 to your computer and use it in GitHub Desktop.
Bash Check Username of User Running Script
This file contains 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
if [ "$(whoami)" != "username" ]; then | |
echo "Script must be run as user: username" | |
exit 255 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOte, one of the sublt things with sudo and looking at environment variables in one line.
(root) $ sudo -u someuser echo ${USER}
root
Will always return 'root' since it ${USER} is value substituted before the call to sudo.
However, if you escape the $ symbol and pass the statement instead of value.
(root) $ sudo -u someuser bash -c "echo \${USER}"
someuser
Will more likely return what you intended.