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

Re: using SUDO in bash script



Rick Weinbender wrote:
Adam Aube wrote:


On Wednesday 04 February 2004 03:23 pm, Rick Weinbender wrote:

Can I use SUDO within a bash script?

Absolutely, though be aware that if sudo is set to require a password, you
won't be able to run it in the background.

By putting NOPASSWD before the command in /etc/sudoers, sudo will not
require a password to run that command.



*****

Thanks.
I'm new to SUDO, but I found a web clip that seems to be
what I'm going for.  Just can't make it work yet.
Can I avoid typing sudo before myprogram at the commandline?

Clip Below:
*****************************************
Clipped from article:
"The one disadvantage of using sudo is that your users have to
 remember to run "sudo ..." as part of their command.  However,
 that's easy to work around by simply creating a wrapper shell
 script.  This is a normal (non-SUID) shell script that simply does
 something like:"
``
#!/bin/sh
exec /usr/bin/sudo /some/path/to/our/target/prog "$@"
''
 (execute sudo, on the target program and pass our argument,
 preserving any quoting as we specified it).
*****************************************

Thanks,
-Rick

instead of writing a shellscript you could also use aliases in the bash
shell (don't know if it works in other shells though). to have your
aliases available all the time, you can but them in your .bashrc file in
your $HOME directory (you will probably already find some aliases
defined in there).

example:
$ alias ls='cd'

doesn't make much sense, but shows you how it works: ls is now defined
as an alias for cd. so if you type 'ls /home' bash will actually invoke
'cd /home'

similar you could use:
$ alias myprogram='sudo myprogram'

to make bash invoke myprogram with SUDO

hth
#!mike





Reply to: