0

I have a shell-script main_script.sh, which will in term call 3 other scripts (a1.sh,a2.sh,a3.sh). Now on execution of a1.sh/a2.sh/a3.sh I need to answer some verbose with Y/N.

I know that each of a1.sh/a2.sh/a3.sh will need 2 Y/N.

How can I implement main_script.sh,so I don't have to answer Y/N requests while execution?

1
  • Note a1.sh,a2.sh,a3.sh are inside main_script.sh. main_script.sh will execute a1.sh,a2.sh,a3.sh and for all the verbose I have to enter Y only. Commented Nov 19, 2014 at 7:33

1 Answer 1

2

It depends on how the scripts are written. You mentioned each script needing two Y. I presume that each Y will need to be followed by an enter key (newline). In this case, it could be as simple as using the following for main_script.sh:

#!/bin/bash
echo $'Y\nY\n' | bash a1.sh
echo $'Y\nY\n' | bash a2.sh
echo $'Y\nY\n' | bash a3.sh

Above, the echo command sends two Y and newline characters to each of the scripts. You can adjust this as needed.

Some scripts will insist that interactive input come from the terminal, not stdin. Such scripts are harder, but not impossible, to fool. For them, use expect/pexpect.

More details

Let's look in more detail at one of those commands:

echo $'Y\nY\n' | bash a1.sh

The | is the pipe symbol. It connects the standard out of the echo command to the standard input of the a1.sh command. If a1.sh is amenable, this may allow us to pre-answer any and all questions that a1.sh asks.

In this case, the output from the echo command is $'Y\nY\n'. This is a bash shell syntax meaning Y, followed by newline character, denoted by \n, followed by Y, followed by newline, \n. A newline character is the same thing that the enter or return key generates.

Using expect

If your script does not accept input on stdin, then expect can be used to automate the interaction with script. As an example:

#!/bin/sh

expect <<EOF1
spawn a1.sh
expect "?"
send "Y\r"
expect "?"
send "Y\r"
sleep 1
EOF1

expect <<EOF2
spawn a2.sh
expect "?"
send "Y\r"
expect "?"
send "Y\r"
sleep 1
EOF2
Sign up to request clarification or add additional context in comments.

2 Comments

#!/bin/sh echo $'Y\nY\n' | pkgrm pkg1 echo $'Y\nY\n' | pkgrm pkg2 echo $'Y\nY\n' | pkgrm pkg3 echo $'Y\nY\n' | pkgadd -d . new_pkg1 echo $'Y\nY\n' | pkgadd -d . new_pkg2 echo $'Y\nY\n' | pkgadd -d . new_pkg3 I was using like this. But I think something is going wrong. "Do you want to remove this package? [y,n,?,q] ERROR: Please enter yes or no. "
For cases when redirecting stdin is not adequate, I have added to the answer information on using expect to automate scripts. If there continue to be problems, I will need more exact information on the scripts' interactions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.