Input from within shell script

前端 未结 4 1024
[愿得一人]
[愿得一人] 2020-12-01 18:44

I have a script that calls an application that requires user input, e.g. run app that requires user to type in \'Y\' or \'N\'.
How can I get the shell script not to ask

相关标签:
4条回答
  • 2020-12-01 19:26

    You can pipe in whatever text you'd like on stdin and it will be just the same as having the user type it themselves. For example to simulating typing "Y" just use:

    echo "Y" | myapp
    

    or using a shell variable:

    echo $ANSWER | myapp
    

    There is also a unix command called "yes" that outputs a continuous stream of "y" for apps that ask lots of questions that you just want to answer in the affirmative.

    0 讨论(0)
  • 2020-12-01 19:32

    If the app reads from stdin (as opposed to from /dev/tty, as e.g. the passwd program does), then multiline input is the perfect candidate for a here-document.

    #!/bin/sh
    
    the_app [app options here] <<EOF
    Yes
    No
    Maybe
    Do it with $SHELL
    Quit
    EOF
    

    As you can see, here-documents even allow parameter substitution. If you don't want this, use <<'EOF'.

    0 讨论(0)
  • 2020-12-01 19:44

    I prefer this way: If You want multiple inputs... you put in multiple echo statements as so:

     { echo Y; Y; } | sh install.sh >> install.out
    

    In the example above... I am feeding two inputs into the install.sh script. Then... at the end, I am piping the script output to a log file to be archived and viewed for later.

    0 讨论(0)
  • 2020-12-01 19:51

    the expect command for more complicated situations, you system should have it. Haven't used it much myself, but I suspect its what you're looking for.

    $ man expect
    

    http://oreilly.com/catalog/expect/chapter/ch03.html

    0 讨论(0)
提交回复
热议问题