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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-07-2004, 05:50 AM
|
#1
|
Member
Registered: Sep 2002
Location: Texas
Distribution: SlackWare - Current LFS - CVS
Posts: 267
Rep:
|
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.
|
|
|
06-07-2004, 07:42 AM
|
#2
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
06-07-2004, 09:02 AM
|
#3
|
Member
Registered: Sep 2002
Location: Texas
Distribution: SlackWare - Current LFS - CVS
Posts: 267
Original Poster
Rep:
|
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.
|
|
|
06-07-2004, 09:15 AM
|
#4
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
06-07-2004, 12:09 PM
|
#5
|
Member
Registered: May 2002
Posts: 964
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 12:52 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|