From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25510 invoked from network); 16 Feb 1999 00:17:07 -0000 Received: from sunsite.auc.dk (130.225.51.30) by ns1.primenet.com.au with SMTP; 16 Feb 1999 00:17:07 -0000 Received: (qmail 5474 invoked by alias); 16 Feb 1999 00:16:02 -0000 Mailing-List: contact zsh-users-help@sunsite.auc.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 2160 Received: (qmail 5465 invoked from network); 16 Feb 1999 00:15:59 -0000 Date: Mon, 15 Feb 1999 19:15:09 -0500 From: Sweth Chandramouli To: ZSH Users Subject: making sudo work with functions/builtins Message-ID: <19990215191509.A28737@astaroth.nit.gwu.edu> Mail-Followup-To: ZSH Users Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.95i a while ago, someone asked about how to deal with the following sort of situation: % alias mv='nocorrect mv' % alias sudo='sudo ' % sudo mv foo bar sudo: nocorrect: command not found from what i can tell, this happens because sudo requires that its first argument be an actual command, and not a builtin or a function; adding the space after sudo in the alias makes expansion take place on cp, so that what sudo sees as the first arg is 'nocorrect'. 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 % my question is twofold--is there a more efficient way to do this, and can anyone see a situation where this would break something that would otherwise work? -- sweth. -- Sweth Chandramouli IS Coordinator, The George Washington University / (202) 994 - 8521 (V) / (202) 994 - 0458 (F) * From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4508 invoked from network); 16 Feb 1999 22:43:54 -0000 Received: from sunsite.auc.dk (130.225.51.30) by ns1.primenet.com.au with SMTP; 16 Feb 1999 22:43:54 -0000 Received: (qmail 1105 invoked by alias); 16 Feb 1999 22:41:59 -0000 Mailing-List: contact zsh-users-help@sunsite.auc.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 2162 Received: (qmail 1087 invoked from network); 16 Feb 1999 22:41:57 -0000 Message-Id: <199902162241.OAA11943@bebop.clari.net> To: Sweth Chandramouli Cc: ZSH Users In-reply-to: sweth's message of Mon, 15 Feb 1999 19:15:09 -0500. <19990215191509.A28737@astaroth.nit.gwu.edu> Subject: Re: making sudo work with functions/builtins Date: Tue, 16 Feb 1999 14:41:41 -0800 From: Wayne Davison 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.. From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5249 invoked from network); 17 Feb 1999 17:32:18 -0000 Received: from sunsite.auc.dk (130.225.51.30) by ns1.primenet.com.au with SMTP; 17 Feb 1999 17:32:17 -0000 Received: (qmail 22758 invoked by alias); 17 Feb 1999 17:30:59 -0000 Mailing-List: contact zsh-users-help@sunsite.auc.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 2165 Received: (qmail 22750 invoked from network); 17 Feb 1999 17:30:56 -0000 From: Bart Schaefer MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <14026.21467.871243.164811@tiny.zanshin.com> Date: Tue, 16 Feb 1999 21:30:03 -0800 (PST) To: Wayne Davison Cc: Sweth Chandramouli , ZSH Users Subject: Re: making sudo work with functions/builtins In-Reply-To: <199902162241.OAA11943@bebop.clari.net> References: <19990215191509.A28737@astaroth.nit.gwu.edu> <199902162241.OAA11943@bebop.clari.net> X-Mailer: VM 6.65a under Emacs 20.3.5.1 Reply-To: Bart Schaefer Wayne Davison writes: > Sweth Chandramouli writes: > > i just had a little brainstorm about how to get around this: > > > > % alias sudo='sudo cmd ' > > % cat cmd > > #!/bin/sh > > eval $SHELL -c \"$@\" > > 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. Aw, sure you do. You almost got it ... all you have to do is reverse the order of handling noglob. do_sudo() { integer glob=1 while (($#)) do case $1 in command|exec|-) shift; break;; nocorrect) shift; continue;; noglob) glob=0; shift; continue;; *) break;; esac done (($# == 0)) && 1=zsh if ((glob)) then command sudo $~==* else command sudo $==* fi } alias sudo='noglob do_sudo ' However, I don't know of any equivalent way to handle nocorrect. > It doesn't allow me to run any shell builtins or functions, though. I don't have 3.1.5 on this laptop, so I forget the new "whence" option for this, but I think you could test whether something was a builtin or function, and if so cause the do_sudo or my_sudo helper function to invoke zsh -c in such a way as to run it. I'm not sure what builtins it would be useful to run that way, but ...