Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using su in ksh script

Status
Not open for further replies.

new2unix

Programmer
Feb 5, 2001
143
US
Hi,

I am using the su - <username> in my script to change to a different user, but the password prompt comes up after the script executed the su command. How do I pass the user password to the prompt from the same script?

Thanks

Mike
 
You can do things like this:

su -c &quot;cat /etc/shadow&quot;
su - -c &quot;some_prog_that_needs_roots_env&quot;

in your script.

Another way to do more complex interaction is to use Expect ( ) to manage the session to some point. That's rarely necessary.

The man page for su on your system may give you other options of interest.

If you'd rather avoid the prompt entirely, you can use a suid program instead, or the &quot;sudo&quot; program can be used. The functionality of sudo may be called something different on your platform- on SCO it's &quot;asroot&quot;, Unixware has the same concept administered through &quot;tfadmin&quot; and &quot;adminuser&quot;.

Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
BTWE, I assumed that you wanted to su to root, in part of my answer above, but the rest of it applies to any username.
Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
check out expect or, have your admin setup a sudoers file to allow you to execute a command as another user
 
Thanks!

Actually, I need to &quot;su - <username>&quot; to different userid and execute the .profile in order to execute a script that, normally, would run ok when log in manually as that user but failed to run as a crontab job.

I will ask our administrator to see if the sudoer can be set up.

Mike
 
???

You can do

su - username -c &quot;some command, script or whatever&quot;

I'm sorry you didn't understand that.
Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
are you saying you can su -username -c in a script?? I don't think you can do that withouth the help of a language like expect because you'll be prompted for a password , however sudo can execute a command or a script from within a script by doing it like this:

/usr/local/bin/sudo -H -u usertorunas command
 
Yes, you can use that in a script.

Unless you mean an unattended script- you can't program the password into this, no. For that you need expect or sudo.

Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
thanks, thats what I thought the only way I know to &quot;automate&quot; su in a script is with using expect thought I might have missed the boat or something
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top