0

I am often in the need of running a bash script that needs inputs from me and im trying to improve my workflow by automating this.

In my case my bash script is in need of 3 inputs from me:

What interface should i use?
1

Enter the password:
mypass

Please restart the program:
sudo bash restart

How can i make my bash script file auto input theses values? I have tried figuring this out but all the answer are about inputing yes or no.

Peterssoen
  • 157
  • 3
  • 12
  • Expect is a best tool designed to handle these kind of tasks instead of rolling out your custom scripts. – apatniv Jun 21 '18 at 01:46
  • Possible duplicate of [Have bash script answer interactive prompts](https://stackoverflow.com/q/3804577/608639), [How can I respond to prompts in a Linux Bash script automatically?](https://stackoverflow.com/q/40791622/608639), [How do I script a “yes” response for installing programs?](https://stackoverflow.com/q/7642674/608639), etc. – jww May 01 '19 at 18:09

3 Answers3

3

If that is all the input your program needs, then you can simply put the inputs, one per line, in a text file, then run it like this:

$> ./yourscript.sh < yourtextfile.txt

For your example, the text file would contain

1
mypass
sudo bash restart
Kenneth
  • 416
  • 4
  • 13
1

If you have such a script.sh:

#!/bin/bash
read -p "What intergace should i use?"$'\n' interfacenum
echo 
read -p "Enter the password:"$'\n' pass
echo 
read -p "Please restaart the program:"$'\n' prog
echo

echo "Values:"
for i in interfacenum pass prog; do
        echo $'\t'"$i=\"${!i}\""
done

You can 'input' the values into the script using echo or printf:

 echo -ne "1\nmypass\nsudo bash restart\n" | ./script.sh
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    I wouldn't recommend using `echo` for anything complex like this; some versions will just print `-ne` as part of their output, and this has even been known to change between OS releases (I had a bunch of my scripts break suddenly because of this). I'd use `printf` instead, something like: `printf '%s\n' "1" "mypass" "sudo bash restart"` – Gordon Davisson Jun 20 '18 at 22:50
0

You can use expect and autoexpect to achieve your task.

Let's say this is your file:

cat hello.sh 
#!/usr/local/bin/bash

read -p "Select the interface: " interface
echo "interface selected: $interface"

read -p "Enter the username: " username
echo "username: $username"

You don't have to even write the scripts. You can you autoexpect to generate the script for you.

autoexpect ./hello.sh 
autoexpect started, file is script.exp
Select the interface: 1
interface selected: 1
...(more input and output to generate the script)

Examine the generated script.

tail -n7 script.exp 
expect -exact "Select the interface: "
send -- "1\r"
expect -exact "1\r
interface selected: 1\r
Enter the username: "
send -- "vivek\r"
expect eof

Now sit back and run the generated script

./script.exp 
spawn ./hello.sh
Select the interface: 1
interface selected: 1
Enter the username: vivek
username: vivek
apatniv
  • 1,771
  • 10
  • 13