You are not logged in.
Pages: 1
How I can use sudo with my aliases? For example I have an alias ka='killall -9' but when I run it with sudo, "ka" is not recognized as a command.
Last edited by anonymous_user (2009-12-28 19:19:47)
Offline
Export your aliases globally, or put them in /etc/profile, or in both root and non-root's .bashrc....
Offline
Export your aliases globally
How do I do that?
or in both root and non-root's .bashrc....
I already did that.
Offline
You could also try:
alias ka='sudo killall -9'
Offline
I don't need to sudo the command every time though.
Oh well, if there no solution I will just create a separate alias.
Offline
This may be a silly question, but have you either sourced the changed files or logged out and in since you made the changes?
Offline
You could create a function to handle the privilege requirements for you. Perhaps something like:
justkill () {
pusers=$(ps -eo "%u%c" | grep -e " $1\$" | cut -d\ -f1)
altuser=$(echo "$pusers" | grep -v "$USER")
if [ ! -n "$pusers" ]; then
echo "$1: no process found"
exit 1
fi
if [ -n "$altuser" -a "$UID" -ne 0 ]; then
sudo killall -9 "$1"
else
killall -9 "$1"
fi
}
alias ka='justkill'
Last edited by chpln (2009-12-28 05:44:10)
Offline
set the follow alias (works on zsh, I think that works in bash too)
alias sudo='sudo '
And you can use you alias with sudo
Last edited by kazuo (2009-12-28 01:26:28)
Offline
But why does that solve it?
Offline
But why does that solve it?
If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.
The fact that I come across nuances like this so regularly make me think it might be worth-while to sit and read the entire manual.
Clever solution, kazuo.
Last edited by chpln (2009-12-28 05:55:01)
Offline
Thank you kazuo.
Offline
Pages: 1