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?

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

  • Leave it to me to try to over-engineer a solution when the path of least resistance is right in front of me. Thanks! – Michael Moussa Oct 23 '09 at 15:07
  • Are you referring to making a copy, or using expect? :) – Kyle Smith Oct 23 '09 at 15:12
5

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

cat <<FIN | ./script.sh
value1
value2
FIN
2
echo -e "MyValue\nMyOtherValue" | ./script.sh
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.

  • Shouldn't the last one read ./script.sh < <(echo "myValue"; echo "myOtherValue") ? – Kyle Smith Oct 23 '09 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.