Brain Drippings

bash function, FTW

by Kreme on Dec.10, 2008, under Computer

Thanks to Leo Laporte’s twitter post of http://codeulate.com/?p=22 I found this wonderful function buried in the comments.

function s { 
  if [ $# -gt 0 ]  
  then  
   sudo $@  
  else  
   sudo $(fc -ln | tail -n1)  
  fi  
}

What it does is nothing short of spectacular. Type a command, like say:

 ~ $ portupgrade postfix
The pkgdb must be updated. Please run 'pkgdb -u' as root.

Oh, darn, forgot to sudo it. Oh well.

 ~ $ s
Password:
[Updating the pkgdb  in /var/db/pkg ... - 431 packages found (-0 +2) .. done]
[package update begins]

That is, if I type a command that requires sudo, I get an error. I could up-arrow, ctrl-a, and type ‘sudo ‘ and hit return. Or I could, as the original post said, type ‘sudo !!’

–Man is born free, but is everywhere in chains.

Posted via email

No comments for this entry yet...

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...