Monday, June 10, 2013

Calling an interactive bash script from another script

Lots of time we need to call existing scripts from new scripts. There are scripts which are interactive and have no option to avoid interactive mode by passing values inline as parameters. Some of them may just need a Yes or No while some may need important inputs in interactive mode.

Let us take a example (a bad one though). Suppose we need to call a script - stopMyServer.sh.

$./stopMyServer.sh
### Do you really want to stop Server? [Yes | No]
Yes
### Please enter the reason code to stop the server
### 1. Scheduled Downtime
### 2. Critical Patch
### 3. Performance degradation
1
### Thanks. Shutting down the server. ###
#!/bin/bash

echo Calling stopMyServer.sh

# EOF is meaningless and can be substituted by any word. Any 
# strings passed between the set of EOF (or any word used 
# instead of EOF) will be passed to interactive script. One 
# line for every interactive pause

./stopMyServer.sh <<EOF
Yes
1
EOF

echo stopMyServer.sh was called

No comments:

Post a Comment