How can I respond to a prompt within a shell script running in background?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I am scripting the installation of software on HP-UX server. Once the script starts it provides a prompt where I am to enter the install path. I need to pass the path to the script so it can continue to run. There is only 1 place in the script where this need exists.
The prompt from the script is:
Press ENTER for default path or enter path to install software:
I do not want to use the default path, so I must enter a new path. But this script will run in the background and I need to provide the path. I'm not sure of the exact response in script form.
shell-script background-process input
add a comment |Â
up vote
6
down vote
favorite
I am scripting the installation of software on HP-UX server. Once the script starts it provides a prompt where I am to enter the install path. I need to pass the path to the script so it can continue to run. There is only 1 place in the script where this need exists.
The prompt from the script is:
Press ENTER for default path or enter path to install software:
I do not want to use the default path, so I must enter a new path. But this script will run in the background and I need to provide the path. I'm not sure of the exact response in script form.
shell-script background-process input
Please could you be clearer about what "the script" is? Is it the installer, which you can't edit for some reason? Or another script that you are writing to run the installer? How are you starting the installer, so that it runs in the background?
â JigglyNaga
Jun 1 '16 at 18:57
Give a look to my answer, and test it!
â Jay jargot
Jun 5 '16 at 21:17
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I am scripting the installation of software on HP-UX server. Once the script starts it provides a prompt where I am to enter the install path. I need to pass the path to the script so it can continue to run. There is only 1 place in the script where this need exists.
The prompt from the script is:
Press ENTER for default path or enter path to install software:
I do not want to use the default path, so I must enter a new path. But this script will run in the background and I need to provide the path. I'm not sure of the exact response in script form.
shell-script background-process input
I am scripting the installation of software on HP-UX server. Once the script starts it provides a prompt where I am to enter the install path. I need to pass the path to the script so it can continue to run. There is only 1 place in the script where this need exists.
The prompt from the script is:
Press ENTER for default path or enter path to install software:
I do not want to use the default path, so I must enter a new path. But this script will run in the background and I need to provide the path. I'm not sure of the exact response in script form.
shell-script background-process input
shell-script background-process input
edited Jun 1 '16 at 21:55
Gilles
516k12210301557
516k12210301557
asked Jun 1 '16 at 15:09
37Scheper
31112
31112
Please could you be clearer about what "the script" is? Is it the installer, which you can't edit for some reason? Or another script that you are writing to run the installer? How are you starting the installer, so that it runs in the background?
â JigglyNaga
Jun 1 '16 at 18:57
Give a look to my answer, and test it!
â Jay jargot
Jun 5 '16 at 21:17
add a comment |Â
Please could you be clearer about what "the script" is? Is it the installer, which you can't edit for some reason? Or another script that you are writing to run the installer? How are you starting the installer, so that it runs in the background?
â JigglyNaga
Jun 1 '16 at 18:57
Give a look to my answer, and test it!
â Jay jargot
Jun 5 '16 at 21:17
Please could you be clearer about what "the script" is? Is it the installer, which you can't edit for some reason? Or another script that you are writing to run the installer? How are you starting the installer, so that it runs in the background?
â JigglyNaga
Jun 1 '16 at 18:57
Please could you be clearer about what "the script" is? Is it the installer, which you can't edit for some reason? Or another script that you are writing to run the installer? How are you starting the installer, so that it runs in the background?
â JigglyNaga
Jun 1 '16 at 18:57
Give a look to my answer, and test it!
â Jay jargot
Jun 5 '16 at 21:17
Give a look to my answer, and test it!
â Jay jargot
Jun 5 '16 at 21:17
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
If you can provide all the inputs when the script starts, then do so, by redirecting the program's input. That is, instead of running /path/to/installer
, run
echo '/the/path/where/to/install';
echo 'answer to the second prompt';
| /path/to/installer
or use a here document:
/path/to/installer <<'EOF'
/the/path/where/to/install
answer to the second prompt
EOF
If you want to interact with a program from time to time but use your terminal for other things in between, run the program in a terminal multiplexer such as Screen or tmux. With screen, start a session by running screen
, then start the program. To do something else, press Ctrl+A, c to create a second window, then Ctrl+A, n to navigate between the windows. To exit Screen but leave the program running, press Ctrl+A, d (âÂÂdetachâÂÂ). To come back to the existing Screen session, run screen -rd
(screen
with no option would start a new session).
add a comment |Â
up vote
1
down vote
You can echo your answer into standard input of script by using a pipe.
echo "My/Path/not/default"| yourscript.sh
So inside the same script, after the command to run the installer... ./INSTALL silent ... I should insert the following line ... echo "my/path/not/default/" | installscript.sh ?
â 37Scheper
Jun 1 '16 at 15:42
add a comment |Â
up vote
0
down vote
To provide an automatic answer, you could use one of the following:
insaller.sh < an_input_file
or
command-line | installer.sh
There is something to notice if the installer.sh
script is using read -p
, as in the example below:
read -p "Press ENTER for default path or enter path to install software:" answer
man bash
specifies that nothing is printed if the standard input is not a terminal.
If this is your situation, then you could try this odd thing:
( sleep 30 ; printf "/my/own/pathn" ) | insaller.sh
You should adapt the number of seconds (30
in above example) to your situation.
If it happens that read -p
is not used inside the install script, then you could give a try to this GNU
solution:
tempdir="$(mktemp -d)"
mkfifo "$tempdir"/input
touch "$tempdir"/output.log
./installer.sh <"$tempdir"/input >"$tempdir"/output.log 2>&1 &
installerpid=$!
tail --pid=$installerpid -fn 1 "$tempdir"/output.log | ( fgrep -q "Press ENTER for default path or enter path to install software:"; printf "/new/pathn" ) >> "$tempdir"/input &
# ... do stuff
# before ending the script, just wait that all background processes stop
wait
rm -f "$tempdir"/input "$tempdir"/output.log
The idea is to use 2 background command-lines, one for the install script, and one to wait the prompt and provide the answer.
A named pipe (input
) and a regular file (output.log
) are used for communication.
tail --pid=$installerpid -fn 1 "$tempdir"/output.log
prints lines as there are written in the output.log
file. It teminates when the installer script terminates.
( fgrep -q ... ; printf .. ) >> ...input
: blocks until the prompt is found, and provides the new path to the install script.
add a comment |Â
up vote
0
down vote
Gilles solution works! You can add 'sleep' to wait for another prompt if needed, before 'echo'. Not as clean as I expected, but it works that matter. I can used my python subprocess to wrap => to copy a shell.script in remote servers and execute it from there using shell ssh command. Then the shell script will do the installation and provide input for the prompt.
NICE.
Thanks Gilles,
New contributor
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you can provide all the inputs when the script starts, then do so, by redirecting the program's input. That is, instead of running /path/to/installer
, run
echo '/the/path/where/to/install';
echo 'answer to the second prompt';
| /path/to/installer
or use a here document:
/path/to/installer <<'EOF'
/the/path/where/to/install
answer to the second prompt
EOF
If you want to interact with a program from time to time but use your terminal for other things in between, run the program in a terminal multiplexer such as Screen or tmux. With screen, start a session by running screen
, then start the program. To do something else, press Ctrl+A, c to create a second window, then Ctrl+A, n to navigate between the windows. To exit Screen but leave the program running, press Ctrl+A, d (âÂÂdetachâÂÂ). To come back to the existing Screen session, run screen -rd
(screen
with no option would start a new session).
add a comment |Â
up vote
2
down vote
If you can provide all the inputs when the script starts, then do so, by redirecting the program's input. That is, instead of running /path/to/installer
, run
echo '/the/path/where/to/install';
echo 'answer to the second prompt';
| /path/to/installer
or use a here document:
/path/to/installer <<'EOF'
/the/path/where/to/install
answer to the second prompt
EOF
If you want to interact with a program from time to time but use your terminal for other things in between, run the program in a terminal multiplexer such as Screen or tmux. With screen, start a session by running screen
, then start the program. To do something else, press Ctrl+A, c to create a second window, then Ctrl+A, n to navigate between the windows. To exit Screen but leave the program running, press Ctrl+A, d (âÂÂdetachâÂÂ). To come back to the existing Screen session, run screen -rd
(screen
with no option would start a new session).
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you can provide all the inputs when the script starts, then do so, by redirecting the program's input. That is, instead of running /path/to/installer
, run
echo '/the/path/where/to/install';
echo 'answer to the second prompt';
| /path/to/installer
or use a here document:
/path/to/installer <<'EOF'
/the/path/where/to/install
answer to the second prompt
EOF
If you want to interact with a program from time to time but use your terminal for other things in between, run the program in a terminal multiplexer such as Screen or tmux. With screen, start a session by running screen
, then start the program. To do something else, press Ctrl+A, c to create a second window, then Ctrl+A, n to navigate between the windows. To exit Screen but leave the program running, press Ctrl+A, d (âÂÂdetachâÂÂ). To come back to the existing Screen session, run screen -rd
(screen
with no option would start a new session).
If you can provide all the inputs when the script starts, then do so, by redirecting the program's input. That is, instead of running /path/to/installer
, run
echo '/the/path/where/to/install';
echo 'answer to the second prompt';
| /path/to/installer
or use a here document:
/path/to/installer <<'EOF'
/the/path/where/to/install
answer to the second prompt
EOF
If you want to interact with a program from time to time but use your terminal for other things in between, run the program in a terminal multiplexer such as Screen or tmux. With screen, start a session by running screen
, then start the program. To do something else, press Ctrl+A, c to create a second window, then Ctrl+A, n to navigate between the windows. To exit Screen but leave the program running, press Ctrl+A, d (âÂÂdetachâÂÂ). To come back to the existing Screen session, run screen -rd
(screen
with no option would start a new session).
answered Jun 2 '16 at 0:13
Gilles
516k12210301557
516k12210301557
add a comment |Â
add a comment |Â
up vote
1
down vote
You can echo your answer into standard input of script by using a pipe.
echo "My/Path/not/default"| yourscript.sh
So inside the same script, after the command to run the installer... ./INSTALL silent ... I should insert the following line ... echo "my/path/not/default/" | installscript.sh ?
â 37Scheper
Jun 1 '16 at 15:42
add a comment |Â
up vote
1
down vote
You can echo your answer into standard input of script by using a pipe.
echo "My/Path/not/default"| yourscript.sh
So inside the same script, after the command to run the installer... ./INSTALL silent ... I should insert the following line ... echo "my/path/not/default/" | installscript.sh ?
â 37Scheper
Jun 1 '16 at 15:42
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can echo your answer into standard input of script by using a pipe.
echo "My/Path/not/default"| yourscript.sh
You can echo your answer into standard input of script by using a pipe.
echo "My/Path/not/default"| yourscript.sh
answered Jun 1 '16 at 15:39
X Tian
7,48111936
7,48111936
So inside the same script, after the command to run the installer... ./INSTALL silent ... I should insert the following line ... echo "my/path/not/default/" | installscript.sh ?
â 37Scheper
Jun 1 '16 at 15:42
add a comment |Â
So inside the same script, after the command to run the installer... ./INSTALL silent ... I should insert the following line ... echo "my/path/not/default/" | installscript.sh ?
â 37Scheper
Jun 1 '16 at 15:42
So inside the same script, after the command to run the installer... ./INSTALL silent ... I should insert the following line ... echo "my/path/not/default/" | installscript.sh ?
â 37Scheper
Jun 1 '16 at 15:42
So inside the same script, after the command to run the installer... ./INSTALL silent ... I should insert the following line ... echo "my/path/not/default/" | installscript.sh ?
â 37Scheper
Jun 1 '16 at 15:42
add a comment |Â
up vote
0
down vote
To provide an automatic answer, you could use one of the following:
insaller.sh < an_input_file
or
command-line | installer.sh
There is something to notice if the installer.sh
script is using read -p
, as in the example below:
read -p "Press ENTER for default path or enter path to install software:" answer
man bash
specifies that nothing is printed if the standard input is not a terminal.
If this is your situation, then you could try this odd thing:
( sleep 30 ; printf "/my/own/pathn" ) | insaller.sh
You should adapt the number of seconds (30
in above example) to your situation.
If it happens that read -p
is not used inside the install script, then you could give a try to this GNU
solution:
tempdir="$(mktemp -d)"
mkfifo "$tempdir"/input
touch "$tempdir"/output.log
./installer.sh <"$tempdir"/input >"$tempdir"/output.log 2>&1 &
installerpid=$!
tail --pid=$installerpid -fn 1 "$tempdir"/output.log | ( fgrep -q "Press ENTER for default path or enter path to install software:"; printf "/new/pathn" ) >> "$tempdir"/input &
# ... do stuff
# before ending the script, just wait that all background processes stop
wait
rm -f "$tempdir"/input "$tempdir"/output.log
The idea is to use 2 background command-lines, one for the install script, and one to wait the prompt and provide the answer.
A named pipe (input
) and a regular file (output.log
) are used for communication.
tail --pid=$installerpid -fn 1 "$tempdir"/output.log
prints lines as there are written in the output.log
file. It teminates when the installer script terminates.
( fgrep -q ... ; printf .. ) >> ...input
: blocks until the prompt is found, and provides the new path to the install script.
add a comment |Â
up vote
0
down vote
To provide an automatic answer, you could use one of the following:
insaller.sh < an_input_file
or
command-line | installer.sh
There is something to notice if the installer.sh
script is using read -p
, as in the example below:
read -p "Press ENTER for default path or enter path to install software:" answer
man bash
specifies that nothing is printed if the standard input is not a terminal.
If this is your situation, then you could try this odd thing:
( sleep 30 ; printf "/my/own/pathn" ) | insaller.sh
You should adapt the number of seconds (30
in above example) to your situation.
If it happens that read -p
is not used inside the install script, then you could give a try to this GNU
solution:
tempdir="$(mktemp -d)"
mkfifo "$tempdir"/input
touch "$tempdir"/output.log
./installer.sh <"$tempdir"/input >"$tempdir"/output.log 2>&1 &
installerpid=$!
tail --pid=$installerpid -fn 1 "$tempdir"/output.log | ( fgrep -q "Press ENTER for default path or enter path to install software:"; printf "/new/pathn" ) >> "$tempdir"/input &
# ... do stuff
# before ending the script, just wait that all background processes stop
wait
rm -f "$tempdir"/input "$tempdir"/output.log
The idea is to use 2 background command-lines, one for the install script, and one to wait the prompt and provide the answer.
A named pipe (input
) and a regular file (output.log
) are used for communication.
tail --pid=$installerpid -fn 1 "$tempdir"/output.log
prints lines as there are written in the output.log
file. It teminates when the installer script terminates.
( fgrep -q ... ; printf .. ) >> ...input
: blocks until the prompt is found, and provides the new path to the install script.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
To provide an automatic answer, you could use one of the following:
insaller.sh < an_input_file
or
command-line | installer.sh
There is something to notice if the installer.sh
script is using read -p
, as in the example below:
read -p "Press ENTER for default path or enter path to install software:" answer
man bash
specifies that nothing is printed if the standard input is not a terminal.
If this is your situation, then you could try this odd thing:
( sleep 30 ; printf "/my/own/pathn" ) | insaller.sh
You should adapt the number of seconds (30
in above example) to your situation.
If it happens that read -p
is not used inside the install script, then you could give a try to this GNU
solution:
tempdir="$(mktemp -d)"
mkfifo "$tempdir"/input
touch "$tempdir"/output.log
./installer.sh <"$tempdir"/input >"$tempdir"/output.log 2>&1 &
installerpid=$!
tail --pid=$installerpid -fn 1 "$tempdir"/output.log | ( fgrep -q "Press ENTER for default path or enter path to install software:"; printf "/new/pathn" ) >> "$tempdir"/input &
# ... do stuff
# before ending the script, just wait that all background processes stop
wait
rm -f "$tempdir"/input "$tempdir"/output.log
The idea is to use 2 background command-lines, one for the install script, and one to wait the prompt and provide the answer.
A named pipe (input
) and a regular file (output.log
) are used for communication.
tail --pid=$installerpid -fn 1 "$tempdir"/output.log
prints lines as there are written in the output.log
file. It teminates when the installer script terminates.
( fgrep -q ... ; printf .. ) >> ...input
: blocks until the prompt is found, and provides the new path to the install script.
To provide an automatic answer, you could use one of the following:
insaller.sh < an_input_file
or
command-line | installer.sh
There is something to notice if the installer.sh
script is using read -p
, as in the example below:
read -p "Press ENTER for default path or enter path to install software:" answer
man bash
specifies that nothing is printed if the standard input is not a terminal.
If this is your situation, then you could try this odd thing:
( sleep 30 ; printf "/my/own/pathn" ) | insaller.sh
You should adapt the number of seconds (30
in above example) to your situation.
If it happens that read -p
is not used inside the install script, then you could give a try to this GNU
solution:
tempdir="$(mktemp -d)"
mkfifo "$tempdir"/input
touch "$tempdir"/output.log
./installer.sh <"$tempdir"/input >"$tempdir"/output.log 2>&1 &
installerpid=$!
tail --pid=$installerpid -fn 1 "$tempdir"/output.log | ( fgrep -q "Press ENTER for default path or enter path to install software:"; printf "/new/pathn" ) >> "$tempdir"/input &
# ... do stuff
# before ending the script, just wait that all background processes stop
wait
rm -f "$tempdir"/input "$tempdir"/output.log
The idea is to use 2 background command-lines, one for the install script, and one to wait the prompt and provide the answer.
A named pipe (input
) and a regular file (output.log
) are used for communication.
tail --pid=$installerpid -fn 1 "$tempdir"/output.log
prints lines as there are written in the output.log
file. It teminates when the installer script terminates.
( fgrep -q ... ; printf .. ) >> ...input
: blocks until the prompt is found, and provides the new path to the install script.
edited Jun 5 '16 at 22:20
answered Jun 3 '16 at 13:08
Jay jargot
80125
80125
add a comment |Â
add a comment |Â
up vote
0
down vote
Gilles solution works! You can add 'sleep' to wait for another prompt if needed, before 'echo'. Not as clean as I expected, but it works that matter. I can used my python subprocess to wrap => to copy a shell.script in remote servers and execute it from there using shell ssh command. Then the shell script will do the installation and provide input for the prompt.
NICE.
Thanks Gilles,
New contributor
add a comment |Â
up vote
0
down vote
Gilles solution works! You can add 'sleep' to wait for another prompt if needed, before 'echo'. Not as clean as I expected, but it works that matter. I can used my python subprocess to wrap => to copy a shell.script in remote servers and execute it from there using shell ssh command. Then the shell script will do the installation and provide input for the prompt.
NICE.
Thanks Gilles,
New contributor
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Gilles solution works! You can add 'sleep' to wait for another prompt if needed, before 'echo'. Not as clean as I expected, but it works that matter. I can used my python subprocess to wrap => to copy a shell.script in remote servers and execute it from there using shell ssh command. Then the shell script will do the installation and provide input for the prompt.
NICE.
Thanks Gilles,
New contributor
Gilles solution works! You can add 'sleep' to wait for another prompt if needed, before 'echo'. Not as clean as I expected, but it works that matter. I can used my python subprocess to wrap => to copy a shell.script in remote servers and execute it from there using shell ssh command. Then the shell script will do the installation and provide input for the prompt.
NICE.
Thanks Gilles,
New contributor
New contributor
answered 6 mins ago
Jeffrey A
1
1
New contributor
New contributor
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f286917%2fhow-can-i-respond-to-a-prompt-within-a-shell-script-running-in-background%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Please could you be clearer about what "the script" is? Is it the installer, which you can't edit for some reason? Or another script that you are writing to run the installer? How are you starting the installer, so that it runs in the background?
â JigglyNaga
Jun 1 '16 at 18:57
Give a look to my answer, and test it!
â Jay jargot
Jun 5 '16 at 21:17