Search code examples
bashshellexpectspawn

Spawn New Process and Return to Expect Script


I am using an Expect script to automate the installation of a program. Because of an issue with one of the install dependencies, I need to pause the installation process at a specific point to edit the db.properties file. Once that file is changed, I can resume the installation process. I can spawn a new process in the middle of the installation to do this, but I get the "spawn id exp5 not open" error after closing that process.

db_edit.sh edits the appropriate file:

#!/usr/bin/sh
filename=db.properties
sed -i "s/<some_regex>/<new_db_info>/g" $filename

My automated installation script spawns the above script in the middle of its execution:

#!/usr/bin/expect

# Run the installer and log the output
spawn ./install.bin
log_file install_output.log

# Answer installer questions
# for simplicity, let's pretend there is only one
expect "PRESS <ENTER> TO CONTINUE:"
send "\r"

# Now I need to pause the installation and edit that file
spawn ./db_edit.sh
set db_edit_ID $spawn_id
close -i $db_edit_ID
send_log "DONE"

# Database Connection - the following must happen AFTER the db_edit script runs
expect "Hostname (Default: ):"
send "my_host.com\r"
# more connection info ...

expect eof

The output log install_output.log shows the following error:

PRESS <ENTER> TO CONTINUE: spawn ./db_edit.sh^M
DONEexpect: spawn id exp5 not open
    while executing
"expect "Hostname (Default: ):""^M

The database info has been modified correctly, so I know the script works and it is indeed spawned. However, when I close that process, the spawn id of the installation process is also apparently closed, which causes the spawn id exp5 not open error.

Also curious is that the spawn appears to happen before it should. The response to "PRESS <ENTER>" should be "\r" or ^M to indicate that ENTER was sent.

How can I fix this to resume the installation script after closing db_edit.sh?


Solution

  • There is no need to automate any interactivity with that script, so don't use spawn

    exec db_edit.sh
    

    This way, you're not interfering with the spawn_id of the currently spawned process.