0

A bash script was provided to me by a sysadmin which requires three values to be input by me. The values are read using "read" rather than read directly from the command-line.

echo "Enter value:"
read myValue
echo "Enter value 2:"
read myOtherValue

The three values I'd be entering are predictable, and I need to run this script frequently, so I would like to automate it; however, simply executing the script with the params in the command-line does not work.

./script.sh myValue myOtherValue

I can supply the first value if I echo it and then pipe it to the script, but that only works for the first param. I don't know how to pass the rest of them in this fashion.

echo "myValue" | ./script.sh

I do not have access to modify the script to just read the arguments.

Any ideas?

Michael Moussa
  • 309
  • 1
  • 4
  • 10

4 Answers4

5

If the number of inputs is static you could use a here document:

cat <<FIN | ./script.sh
value1
value2
FIN
Gerald Combs
  • 6,441
  • 25
  • 35
2

If you are truely unable to copy this script to your home folder and make modifications to suit your needs, you should look into expect. Expect can read/write stdin/stdout to a program in an automated fashion.

This guide looks decent: http://www.ftlinuxcourse.com/FTLinuxCourse_Complete-2004/FTLinuxCourse/en/sysadm/chap5_3.html

Kyle Smith
  • 9,683
  • 1
  • 31
  • 32
2
echo -e "MyValue\nMyOtherValue" | ./script.sh
Bill Weiss
  • 10,979
  • 3
  • 38
  • 66
1

Similar to Gerald Combs' answer, you can use process substitution and redirection:

./script.sh < <(echo -e "myValue\nmyOtherValue")

or

./script.sh < <(echo "myValue
myOtherValue")

or

./script.sh < <(cat <<EOF
myValue
myOtherValue
EOF
)

or even:

./script.sh < <(echo "myValue"; echo "myOtherValue")

Where, instead of two "echoes", you might have two different commands that produce the output you need for input to the script.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151