Monday, October 14, 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. 
================= 
#!/bin/ksh 

date 
echo " test " 
su - user2 

id 
pwd 
/home/user2/test.sh 
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 
id 
pwd 
/hom/user2/test.sh 
ps -ef | grep user2 
EoI 


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



0 comments:

Post a Comment

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