Ben
Ben

Reputation: 139

Using shell script to automate input

How can I write a .sh file to automate input? For example I have a simple program that asks for a name and a few other things. I have so far

#!/bin/bash

echo alice
echo 5

I try to use it like ./program < ./file.sh, but it seems to take #!/bin/bash as input. I am wanting the first input the program takes to be alice and then directly after 5 which should terminate the program

Upvotes: 0

Views: 2038

Answers (2)

Abhishek Jaisingh
Abhishek Jaisingh

Reputation: 1732

You can use the command redirection syntax:

./program < <(./file.sh)

Basically, it first executes the script file.sh then it puts the output it gets in a temporary file, then the temporary file is passed as stdin to the first program with usual file-redirection syntax.

The problem with your incantation is that the bash syntax you are using is for file redirection, where the contents of the file are being passed as input to the first program. What you want to do is pass the output of the file.sh script instead of the actual text in it.

Upvotes: 0

Kashan Baig
Kashan Baig

Reputation: 61

why are you using file.sh as input stream ? just create a simple plain text file and keep each value on separate row

myscript.sh

#!/bin/bash

read "Enter your name : " a
read "Enter your age : " b  

echo $a 
echo $b

input.txt

Kashan
21

output

~$ ./myscript < input.txt

kashan
21


 

Upvotes: 2

Related Questions