Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I bet intruder3 is the password you've entered. You cannot use su in this way. Look at the man page: the -p option is not meant to input password. Furthermore, I think using su in a script is not possible at all, unless you're root and want to run a command as a regular user, for example
Code:
su username -c "echo Hello world!"
in this case you're not prompted for password, since root can switch to any other user without entering his password.
Edit: I'm a bit slow. So here is another solution:
Do you just want to run the script as another user?
If thats the case I only found a workaround for this:
Code:
#!/bin/bash
# First call? Switch user.
if [ "$1" != "-nouserswitch" ] ; then
echo "Username:"
read username
exec su $username -c "/home/dom/Temp/test.sh -nouserswitch"
fi
whoami
So you basically run your script twice. The first time to execute the script as another user. The second time (with the "-nouserswitch" argument) to actually run your script.
This solution does only work if you need the user switch at the very beginning of the script.
hi..predeep.sree
i tried ur script but its giving hell lot of errors...still thanx yaar..can u plzz check it urself y so many errors r comin.....
@dom83....
i tried ur script but i think ur not gettin wat m tryin to say..
cause ur script is switcin user but its asking for password as normaly it is asked wen we do su <username>
but i want a script in which once a user has provided username and password then the script shd itself switch the user ....n not ask for nethin else...
There is nothing wrong with placing an su or sudo in a script. Onoe just has to understand the consequences, and how it require STDIN to be attached to the terminal.
Code:
$ cat /tmp/su-test.sh
#!/bin/bash
echo Hello
su -c 'echo Hello Root' root
echo All done
$ /tmp/su-test.sh
Hello
Password:
Hello Root
All done
av.dubey - my versions of su do not support a -p password option. The -p option is used to preseve the environment. Furthermore, the "pwd" command that follows you call to su won't be executed until the su-invoked shell exits.
Can you clarify better what you are trying to accomplish that is alread done with:
i don understand y m not able to make you clear abt wat i m tryin to do ...
see i want to make a shell program ..which will be run by normal user...
it will prompt for username and password ..
once the normal user will enter username as root and password as the root's password...then he will automatically be logged in as superuser and a switch case will be there that will enable him to run many root commands just by entering his choice as 1,2 or 3 ,,for example adduser,neat and all..
my main problem is that m not able to login as superuser using shell script..
i know we can do it manually like typine su - and den entering password when prompted...but cant it be automased using shell script..
I suggest you use sudo instead of su for the following reason:
-S The -S (stdin) option causes sudo to read the password from the
standard input instead of the terminal device.
(so you can echo it or something)
Also, expect is a wise solution as well.
I have found a few other people with the same problem, check them out: http://www.dbforums.com/archive/index.php/t-319516.html
Here's an expect guide: http://www.unix.com/shell-programmin...ll-script.html
actually im trying to make an application using cgi which will provide users a web interface software type of thing that will help them to do all the root activities using graphical interface...
but leave that right now..
the thing is that if iam able to login as root using shellscript ...my work will be done...so plzz don ask me to change my mind regarding that..
can neone please give me a simple example of using execute or ne other thing like that using which i can get as superuser using shell script..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.