Bash run docker command only if user has privileges (Bash if or statement)
I’m trying to only allow a script to be run if the user is allowed docker privileges. So this means the user either needs to be in the
docker group or be root.
I’ve tried to do this but it doesn’t work.
if [ $EUID -ne 0 ] || [ $(groups | grep -c docker) -ne 1 ]; then echo "You must have docker privileges to use this command." exit 1 fi echo "Do docker stuff"
Oddly, just the
[ $EUID -ne 0 ] or the
[ $(groups | grep -c docker) -ne 1 ] work fine by themselves but the OR condition doesn’t seem to work and I can’t figure out why.
One Solution collect form web for “Bash run docker command only if user has privileges (Bash if or statement)”
You should be combining them with
Suppose the user is in the docker group, but not root.
[ $EUID -ne 0 ] will be true, while
[ $(groups | grep -c docker) -ne 1 ] will be false.
|| is true if either of its operands is true, so this will make the whole condition true, and the error message will be printed.
&& only succeeds if both its operands are true, so you get the message when the user is not root and they’re not in the docker group.