How can I respond to a prompt within a shell script running in background?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












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.










share|improve this question























  • 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














up vote
6
down vote

favorite
1












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.










share|improve this question























  • 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












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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










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).






share|improve this answer



























    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





    share|improve this answer




















    • 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


















    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.






    share|improve this answer





























      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,





      share








      New contributor




      Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        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






























        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).






        share|improve this answer
























          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).






          share|improve this answer






















            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).






            share|improve this answer












            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).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 2 '16 at 0:13









            Gilles

            516k12210301557




            516k12210301557






















                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





                share|improve this answer




















                • 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















                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





                share|improve this answer




















                • 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













                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





                share|improve this answer












                You can echo your answer into standard input of script by using a pipe.



                echo "My/Path/not/default"| yourscript.sh






                share|improve this answer












                share|improve this answer



                share|improve this answer










                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

















                • 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











                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.






                share|improve this answer


























                  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.






                  share|improve this answer
























                    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.






                    share|improve this answer














                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 5 '16 at 22:20

























                    answered Jun 3 '16 at 13:08









                    Jay jargot

                    80125




                    80125




















                        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,





                        share








                        New contributor




                        Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.





















                          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,





                          share








                          New contributor




                          Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.



















                            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,





                            share








                            New contributor




                            Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            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,






                            share








                            New contributor




                            Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.








                            share


                            share






                            New contributor




                            Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered 6 mins ago









                            Jeffrey A

                            1




                            1




                            New contributor




                            Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            Jeffrey A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                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













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay