I’ve used the following alias’ on my old arch system using bash and can’t see why they wouldn’t work on zsh, yet it doesn’t trigger the warning in the function. I’ve tried it with a number of different commands so it doesn’t fail due to pamac specifically. Any ideas?
Aliases pamac so if it is run with a UID of 0 (root) it prints a warning, then just runs it anyway since pamac almost always includes a prompt you can terminate from (in hindsight this probably isn’t the best but doesn’t explain why it doesn’t work).
alias pamac='[ "$(id -u)" -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2; pamac'
This fails because $(id -u) is expanded before being passed to sudo
You can test it.
alias pamac='echo UID = $(id -u)'
sudo only applies to the first command, so it wouldn’t work anyway.
You could create a script in /usr/local/bin, assuming it’s in your PATH before /usr/bin which I think it is by default (but not % sure, I may have added it to mine).
#!/usr/bin/bash
[ $(id -u) -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2; pamac
The only thing getting passed to sudo is “pamac” - otherwise I’d be getting an error, but I just get the sudo password prompt as though nothing happened.
I found another way and used a function but I’m still perplexed the first method isn’t working when I think it should:
sudo() {
if [ "$1" = "pamac" ]; then
printf "\033[1;31mSTOP:\033[0m pamac manages its own privile>
fi
command sudo "$@"
}
pamac shouldn’t be passed to sudo as it’s a separate command, only the first command is run with sudo. You’re not getting an error code because pamac runs successfully after the rest fails because you used ;.
Try these:
# will print your warning, return code 0
alias pamac='[ ! "$(id -u)" -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2'
# won't print, return code 1
alias pamac='[ "$(id -u)" -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2'
# won't print, returncode 1
alias pamac='[ "$(id -u)" -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2 && echo $(id -u)'
# will print "not root" and the UID but not your warning, returncode 0
alias pamac='[ "$(id -u)" -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2 && echo root $(id -u) || echo not root $(id -u)'
# will print your warning and the UID, returncode 0
alias pamac='[ ! "$(id -u)" -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2 && echo $(id -u)'
# can't run pacman as root
alias pamac='[ ! "$(id -u)" -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2 && pacman -S mpv
alias pamac='[ ! "$(id -u)" -eq 0 ] && printf "\033[1;33mWarning:\033[0m pamac handles its own elevation. Using sudo is discouraged.\n" >&2; pacman -S mpv