Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a bunch of several bash scripts that will perform some actions that require user input, therefore I'm using expect with it.

However, I need one to keep running. But if I use expect, the expect command will wait while the script is running and my main script will not continue.

IE: script1 spawns a script as root:

#!/bin/bash
expect << EOF
spawn su root -c "/bin/bash script2.sh"
expect "Password:"
send "test\n"
set timeout -1
expect eof;
EOF

script2.sh will spawn a screen:

#!/bin/bash
screen -dm nastyscript.sh

nastyscript.sh will spawn a binary that must keep running:

#!/bin/bash
expect << EOF
spawn mybinary
expect "*Enter password for my binary"
send "test\r"
expect "*I am running"
set timeout -1
expect -re . { exp_continue } eof { exit }
expect eof;
EOF

But, since one script is waiting for expect to finish, my main script won't continue.

Any way to do that?

share|improve this question
    
Can you change script1.sh? E.g., sudo script2.sh –  Mark Wagner Jun 15 '11 at 16:14
add comment

1 Answer

First, you absolutely don't want to send a password with expect. Besides being very bad style it totally undermines the security of your system because you have to store a user password in cleartext. And everybody in this day and age knows by heart that you should never store any passwords in cleartext, ever.

I would recommend reading up on the manpage for sudoers. Then you can use sudo from within the script without having to store passwords in the clear. A more solid solution would probably be an executable with suid root. Shell scripts can't have suid for security reasons, so this would require compiling a small program.

On the other hand, if you are sure that you need multiple processes running in parallel communicating with each other, then bash version 4 offers a solution in form of the coproc keyword. Basically you put coproc in front of a command line. For simple commands it has roughly the same effect as an & at the end of the command line; the process will run in parallel with your script. The difference is that with coproc you have the stdout and stdin file descriptors of the other process available in the array COPROC, so your script can communicate with the other process. See http://www.gnu.org/software/bash/manual/html_node/Coprocesses.html

Then you also might want to wait on the other process before your script exits.

share|improve this answer
add comment

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.