I have a script called operation.sh and while i am running the script, will be getting output like below and will wait for user input.

 1. Start
 2. Restart
 3. Stop
 4. Status
 Enter Option: 

Once I pressed "1", it will perform some logic and again it will come to above view.

I need to call the operation.sh from other sh (i.e., other.sh) like,

 sh other.sh

It has to perform the same logic as when I pressed 1 in operation.sh.

other.sh is having below content.

sh operation.sh 1

I could understand, it doesn't make sense (but I tried since I don't have any idea).

How can I call a menu script(which is performing action based on user selected menu) from another script.

  • Look at expect. – Henrik Jul 15 '16 at 14:50
  • @Henrik, i have't tried with expect. and I have idea. But, Is there any other way without using expect.? – Spike Jul 15 '16 at 14:54
  • Related: Prepare answers for questions of a command – steeldriver Jul 15 '16 at 14:59
  • depends on how the other script reads its input. for instance, for some ways that use standard input, echo 1 | sh other.sh might work – infixed Jul 15 '16 at 15:39

OPC1() {
echo "Opcion 1"

OPC2() {
echo "Opcion 2"

if ( test $opc -eq 1 )

#### MAIN ##########


if [ $1 ]
    echo "1. start "
    echo "2. stop "
    echo "0. salir "
    read opc
  • i can't modify target script. can't i achieve my scenario without modifying target script? – Spike Jul 18 '16 at 4:55

Try using echo -e "1\n2" piped to the script. Just put the menu responses separated by newlines (\n) in the string after -e.

echo -e "1\n2" | operation.sh

Another option is to put the responses in order one per line in a text file. You can then cat that file and pipe it to the script.

cat responses.txt | operation.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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