Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
    
See this: stackoverflow.com/questions/20616022/… –  Ketan Feb 18 at 17:46
    
when it asks you for a password, then give it your own password (not root password). If you run it again, it can cache the password and not ask again (depends on config). and remove <password> line –  X Tian Feb 18 at 18:10
    
no...I don't want use caching...you are right..but for first time I want enter password and do stuff –  MortezaLSC Feb 18 at 18:13

3 Answers 3

up vote 3 down vote accepted

You can pipe the password and send it in the command inside the script.

echo "password" | sudo -S 

But it is not a good idea to send the password in the command line. If you need more information on how to login as root from the script, you can look at the answer provided here.

However, if it is for experimental purposes, we can use the expect to enter the password from command line. The script needs be modified like below.

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

The last line is needed since we need to press the Enter after inputting the password. As Tian suggested, it is not a good idea to send the root password in the shell script.

share|improve this answer
    
I update question –  MortezaLSC Feb 18 at 17:52
    
Is the option that I specified working? –  Ramesh Feb 18 at 17:54
    
No really, the error is:sudo: root: command not found –  MortezaLSC Feb 18 at 17:55
1  
Take a look at this link. Apparently, you need the -s flag only. stackoverflow.com/questions/11636840/… –  Ramesh Feb 18 at 17:56
    
good..question: doesn't need to enter password? –  MortezaLSC Feb 18 at 17:58

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:

sudo -kSs << EOF
password
whoami
echo "Not a good idea to have a password encoded in plain text"
EOF
share|improve this answer
2  
Hmm, this answer looks familiar... –  Graeme Feb 18 at 19:36
    
yes..I remove it.. and put it here and in superuser..ok? –  MortezaLSC Feb 18 at 19:37
    
Thank you for your answer,,,It works good... –  MortezaLSC Feb 18 at 19:38
1  
No prob, maybe a link to the original would be good though. SE won't allow duplicate answers on the same question anyway, even if one is deleted. –  Graeme Feb 18 at 19:41
1  
Daresay this is a good answer, might as well upvote it :) –  Graeme Feb 18 at 19:57

Embedding the root password in the script isn't a good idea, from a security point of view, this is probably why su attempts to get it initially from a terminal.

Using sudo is a better route to take, it's more flexible, you can configure it to allow only particular commands, or even certain users to run a program/script with or without using a password (man sudo).

@Ketan's reference is also worth reading.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.