0

I am trying to automate something using shell script.

My question is when there is a system prompt, for e.g. "Do you want to continue (y/n):" And I need to input y using my shell script, how can it be done ? So at the end when the system prompt displays on the shell then my shell script should be automatically able to enter y.

Thanks in advance.

user3530656
  • 121
  • 4
  • 14
  • It's a duplicate: [How do I prompt for input in a Linux shell script?](http://stackoverflow.com/q/226703/1848245) – techno Feb 24 '16 at 06:41

2 Answers2

0

You can try an expect script

It is used to automate control of interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface.

KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
0

It is really quite easy using redirection, and there are several ways to do it. This uses a here string. First to generate that system prompt:

more one.sh
read -p "Do you want to continue (y/n): " ans
echo "Answer was $ans"

Now the script to run the program:

bash one.sh <<< 'y'

Output:

Answer was y

Warning! This works by redirecting the process's standard input stream. Some programs, particularly when getting passwords, access the terminal driver at a low level for security reasons.

cdarke
  • 42,728
  • 8
  • 80
  • 84