Wednesday, October 2, 2013

Using 'su - user' in a Script

www.unixbabuforum.inI am trying to write a shell script, which executes list of commands that I mentioned in the script. 

For example, 
I am going to execute/write the following script as a root. 

echo " test " 
su - user2 

ps -ef | grep user2 


am not able to execute the above commands after " su " as a user. 

Can anyone please tell me how to su to user and execute some commands as a "user"? 

www.unixbabuforum.inYou need to somehow attach the commands to the su command: One way is with a "here" document as follows: 

su - user2 << EoI 
ps -ef | grep user2 

I believe you can also do it by re-directing commands from a file like this: 

su - user2 < my_script www.unixbabuforum.inSome commands like login, passwd, su can not execute simply from script. 

Try to use "expect" command. 

See man page of expect.


Post a Comment

Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM