How to by-pass user interactions in a script?

10,741

Solution 1

If the interaction is simple, i.e. only reads from the standard input, you can just send the particular lines to the program's input:

( echo yes ; echo 1024; echo yes ) | install.sh

Solution 2

If you want to do anything that doesn't just read from stdin (e.g. ftp login) you'll want to use expect.

Solution 3

From another thread:

If your command doesn't care how fast you give it input, and you don't really need to interact with it, then you can use a heredoc.

Example:

#!/bin/bash
command_you_want_to_execute <<EOD
interaction_1
intereaction_2
EOD

If you need branching based on the output of the program, or if your program is at all sensitive to the timing of your commands, then Expect is what you want.

Share:
10,741

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I am trying to automate the installation of a program through a shell script. There are a few steps which require user interaction from the command line (confirmation, yes/no, etc.). What is the best way to by-pass it? I know that one possibility is to use an expect script. Just wanted to know if there is a better/cleaner way to do this.