Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: making sudo work with functions/builtins



Sweth Chandramouli writes:
> i just had a little brainstorm about how to get around this:
> 
> % alias mv='nocorrect mv'
> % ls
> cmd foo
> % alias sudo='sudo cmd '
> % cat cmd
> #!/bin/sh
> eval $SHELL -c \"$@\"
> % sudo mv foo bar
> % ls
> bar cmd
> %

While this cures the error of sudo trying to run "nocorrect", it
does not propagate the "nocorrect" forward soon enough to turn off
spelling corrections on the command.  This delayed application is
more of a problem with a command that has the "noglob" modifier:

% alias e='noglob echo'
% e f*
f*
% sudo e f*
foo

I know of no way around this problem.

Note that your solution also some quoting problems:

% sudo echo 'f*'
foo
% sudo echo 'one    two'   'three    four'
one two three four
% touch 'one two'
% sudo cat 'one two'
cat: cannot open one
cat: cannot open two

One way to fix these is by changing your "cmd" shell script to be:

#!/local/bin/zsh
noglob $@

The way I worked around the sudo problem was to use the following
alias and function.  In addition to avoiding the errors, it also
allows me to type just "sudo" to get the effect of "sudo zsh":

alias sudo='my_sudo '

function my_sudo {
	while [[ $# > 0 ]]; do
		case "$1" in
		command) shift ; break ;;
		nocorrect|noglob) shift ;;
		*) break ;;
		esac
	done
	if [[ $# = 0 ]]; then
		command sudo zsh
	else
		noglob command sudo $@
	fi
}

It doesn't allow me to run any shell builtins or functions, though.

..wayne..



Messages sorted by: Reverse Date, Date, Thread, Author