Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is my simple bash:

cat test.sh

#!/bin/bash 
echo "hello"
su - root -c /path/to/script.sh <<EOF
password                              
EOF
whoami
echo "good bye"

But I get this error:

./test.sh
hello
su: must be run from a terminal
<current-user>
good bye

(OR)

cat test2.sh
#!/bin/bash 
echo "hello"
sudo su <<EOF
password                              
EOF
whoami
echo "good bye"

Again another error

(OR)

cat test3.sh
#!/bin/bash 
echo "hello"
su root <<EOF
password                              
EOF
whoami
echo "good bye"

again error...

when I try:

#!/bin/bash
echo "hello"
sudo -s <<EOF
<password>
echo Now I am root
id                                                                      
echo "yes!"
EOF
whoami
echo "good bye"

Then the output is:

./script.sh
hello
[sudo] password for <user>:

I also changed my script to:

#!/usr/bin/expect -f
spawn sudo -s <<EOF
expect "assword for user:"
send -- "password\r"
expect eof

and output is:

spawn sudo -s <<EOF
[sudo] password for user:
/bin/bash: <<EOF: command not found

Also which sh output is /bin/sh

How can I resolve the error in these three scripts?

share|improve this question
up vote 6 down vote accepted

There are many ways to do this... here are some:

  1. login as that user: for real,

    su user2

or just get the same environment,

  su -l user2
  1. execute a single command as that user:

    su user2 -c cat myfile.txt

  2. use here docs:

    sudo su user <<EOF

    command1

    command2

    .....

    EOF

Your commands may fail because /tmp/script.sh is not executable, or because the last echo in one.sh does not have the matching apex (").

share|improve this answer
    
I didn't got su user < new syntax? – MLSC Feb 18 '14 at 17:25
    
see my update...the error occurs – MLSC Feb 18 '14 at 17:28
    
don't work..I'm afraid – MLSC Feb 18 '14 at 17:44
    
@MortezaLSC And yet it's funny, because it does work on my systems Debian, Kubuntu, Arch Linux. You must have copied something wrong. – MariusMatutiae Feb 18 '14 at 18:14
    
you mean I use your heredoc solution? – MLSC Feb 18 '14 at 18:15

Use sudo and grant your (original) user the permission to run /tmp/script.sh as the desired user without password:

original_user ALL=(script_user) NOPASSWD: /tmp/script.sh

Invoke like so:

sudo -u script_user /tmp/script.sh

Still, it's a terrible idea to do so with anything relating to a world-writable directory. You're just asking for it by doing that.

share|improve this answer
    
Don't use this. Not voting down because they mentioned it's a "terrible idea". – nerdwaller Feb 18 '14 at 17:41
    
I don't see what's generally wrong with that. Apart from the script's location. – Daniel B Feb 18 '14 at 18:37
    
Hence why I didn't vote you down, in and of itself, you are right, isn't fully evil. – nerdwaller Feb 18 '14 at 18:41

I GOT ANSWER FROM here

Doing this kind of stuff is not safe or standard practice (in fact many consider it disasterous), it is really not a good idea to put a password in a script. A more standard approach would be simply to expect the whole script to be executed with root privileges, or just to have the script prompt for a password. You can also allow various commands to be run via sudo without a password by particular users by using the NOPASSWD option in /etc/suoders.

However, now that you are aware of the risks, it is possible to use sudo -kS to have sudo read the password from stdin and have bash read commands from stdin with -. Eg:

sudo -kS bash - << EOF
password
whoami
echo "Not a good idea to have a password encoded in plain text"
EOF
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .