Example script:
sudo su admin
ls /usr/bin
exit
When I run it with:
biske@comp1:~$ ./script
Then prompt is changed to:
admin@comp1:/home/biske$
When I type exit it actually prints those lines:
exit bin games include lib lib32 local sbin share src
I wanted output without requiring user to enter anything.
I suppose problem could be solved by feeding this sudo su
commmand with password.
Is there a way to do it?
sudo su user
will fork into a new shell. That's the last thing you want to do when in a non-interactive script. Sorry, I mean that's the last thing you want to do in a script after hard-coding a password into it! Please avoid that.You would be much better off skipping
su
and using sudo on its own:Or failing that, just making sure your script is run as root with this block:
And then there's no need for interacting inside the script at all. Your script can
sudo -u... command
anything without needing to change password.