BASH scripting; su to user, commands after dont execute
ProgrammingThis 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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.