LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-07-2004, 04:50 AM   #1
Zero-0-Effect
Member
 
Registered: Sep 2002
Location: Texas
Distribution: SlackWare - Current LFS - CVS
Posts: 267

Rep: Reputation: 31
BASH scripting; su to user, commands after dont execute


My question is how to, in a script, su to a user.
For simple example:

Ran as root., so I dont have to worry bout entering a password, for now. Want to figure this out first.

#!/bin/bash
su zero
cd /home/zero
ls
exit 0

It does su to user zero but then does not execute anything after that point. Why? Is there a special way of doing this that im missing.
 
Old 06-07-2004, 06:42 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

When you do su, another instance of bash is launched, so the script never gets to the next line (until the su'ed instance of bash returns). There is a -c option for su which allows you to pass a single command to the su'ed instance of bash, which might be useful (I don't know if that single command actually does have to be one command, or whether you could string a load of commands onto one line. If not, you could just create a seperate script and put the commands you need to execute after the su into that). See 'man su' for enlightenment.

Dave
 
Old 06-07-2004, 08:02 AM   #3
Zero-0-Effect
Member
 
Registered: Sep 2002
Location: Texas
Distribution: SlackWare - Current LFS - CVS
Posts: 267

Original Poster
Rep: Reputation: 31
Thx, I checked the man page for su and there was no listing for what args could be but I did find that -c option on the net. It is possible to do more than one command like:
su user -c "
command
command
etc..
"
But if you do anything with qoutes it end's it. But executing the second script is a good idea. Funny, the simplist things I never think of!
Thanks alot Dave!

Maybe I should of wrote this in c++... ... oh well, least im learning something new.
 
Old 06-07-2004, 08:15 AM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
You can escape any characters that you want to pass directly to the -c option by putting \ in front of them:

e.g.
su -c "echo "who" > /home/dave/out; grep who /home/dave/out" doesn't work, but
su -c "echo \"who\" > /home/dave/out; grep who /home/dave/out" does.

Dave
 
Old 06-07-2004, 11:09 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
sudo does what you want but itakes just one command.
If you need a bunch of things done, use that command to invoke a shell script that does the stuff you want.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - execute commands on a remote machine capture results, etc dkrysak Programming 1 11-18-2005 01:50 PM
[Samba] Execute commands depending on user billiejoex Linux - Software 1 10-25-2005 12:01 PM
Execute bash commands from web page solomage Linux - Software 1 10-07-2004 10:54 AM
cannot execute some commands as an plain user! minike Slackware 5 09-03-2004 06:34 PM
how can execute some commands when a user logs out rddreamz Programming 2 05-25-2004 03:00 PM


All times are GMT -5. The time now is 12:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration