I have written a shell script which in turn calls another script, but the second one prompts for password. I want to supply the password and pass the prompt without user begin notified.

Here is my script:

su - s3 -c "su.sh"

I trided :

su - s3 -c "echo pass | su.sh"

But I got an error:

standard in must be a tty


The screenshot of commands.

enter image description here

I also tired :

 su - s3 -c "echo mypass|sudo -S su.sh"

but another error comes up:

sudo: sorry, you must have a tty to run sudo

su.sh commands:

if [ ! -n "$IS_WINDOWS" ]
   /bin/su root -c "...."
  • First locate what part of the script is issuing the error message. Or paste your script in the question. Chances are you really want to use sudo instead of su - hard coding passwords in scripts is never a good idea. – peterph Mar 2 '15 at 15:25
  • just edited the question. actually the command inside double quote throws the error. – Babak Fakhriloo Mar 2 '15 at 15:34

It seems that you need to open a pty. You can try socat:

echo pass | socat -  exec:su.sh,pty,stderr,su=s3,ctty

This way the password is not in the command line arguments, which would be a security issue.

The better option would be to modify su.sh in the way that it does not need a password anymore.

| improve this answer | |
  • I saw the su.sh file and it has a line what I mentioned in edited question. How could I modify it so it does not require password any more ? – Babak Fakhriloo Mar 2 '15 at 15:49
  • If the command in the su root line can be called before the script, just delete the line and call it by hand before the script. Then you can avoid the double su issue which requires passwords. – jofel Mar 2 '15 at 16:15

You may use the expect program to supply a response when the password is asked. Note this also has the security problem of hard-coding passwords and is usually better to set a sudo rule for the passwordless execution of the command.

| improve this answer | |
  • Is it only possible with expect tool ? – Babak Fakhriloo Mar 2 '15 at 16:13
  • It's not the only solution but is the easiest one if you want to supply it through the script. Better solutions require changing configuration of system permissions and usually are more secure. The correct solution instead of script is to use sudo. – miguelbernadi Mar 2 '15 at 16:17

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