JP Vossen on 28 Sep 2009 12:26:42 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] correct way to do this in bash [sudo cp]


> Date: Sun, 27 Sep 2009 09:15:30 -0700 (PDT)
> From: Bob Schwier <schwepes2002@yahoo.com>
> 
> I swiped this thread because I have a BASH question.  I'm using Ubantu
> 8.02 and I have a modest problem with BASH under SUDO.  I'm trying to
> extract personal files from a hard drive that ran SUSE 7.0.  I do not
> seem to be able to use wild cards to speed up the process.  I seem to
> have to extract each file individually.  

Short answer:
	sudo -s
	cp /your/files/here/* .


Long answer:

You will run into problems using sudo when doing certain things, for 
example:
	sudo cp /some/path/*
	sudo cat foo >> /something/your/user/can't/write/to

It's a shell issue, though I doubt it's restricted to bash.  The issue 
is due to the order of command-line processing (Appendix C of _bash 
Cookbook_ which is taken from page 180+ in _Learning the bash Shell_). 
Basically, tokenization (including redirectors) then lots of expansions 
(including wildcards) happen *before* the command actually runs.  IOW, 
before sudo runs.  Remember that it's the *shell* that is doing 
wild-card expansion, not the command.  So in your case it sounds like 
you are trying to wild-card expand stuff your regular user doesn't have 
access to.  (That same thing trips me up trying to 'sudo vi 
/etc/logcheck/{tab for filename completion, which fails} all the time.)

There are three ways around this, depending on what you are doing.

1) sudo -s
2) sudo -c 'bash -c your_command_here'
3) command1 | sudo command2

#1 is easiest, but the effect is to give you a root prompt.  Thus you 
lose sudo's command logging, and you need to remember you are now root, 
so be careful and exit when you are finished!  (A decent $PS1 is very 
useful here [1].)

#2 can be a real pain to get quoted right.  Basically, you are running a 
bash shell, which is running a command, all of which is sudo'd.  Get it? 
  Good.

#3 only works if you are piping to another command you can sudo.  It 
won't help for wildcards, filename completion, redirection, etc.

Honestly, if #3 won't obviously work I just do a 'sudo -s' then exit as 
soon as I did whatever.

Later,
JP

[1] This is the $PS1 I use.  You will either love it or hate it.  To 
test, run a bash sub-shell (just type 'bash') and paste this in.  If you 
hate it, just type exit to kill the sub-shell.
PS1='\n[\u@\h:T\l:L$SHLVL:C\!:J\j:\D{%Y-%m-%d_%H:%M:%S_%Z}]\n$PWD\$ '
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|      http://bashcookbook.com/
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug