Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 2 down vote accepted

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%5FComplete-2004/FTLinuxCourse/en/sysadm/chap5%5F3.html

share|improve this answer
    
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
echo -e "MyValue\nMyOtherValue" | ./script.sh
share|improve this answer

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.

share|improve this answer
    
Shouldn't the last one read ./script.sh < <(echo "myValue"; echo "myOtherValue") ? –  Kyle Smith Oct 23 '09 at 18:39
    
Thanks for the correction. –  Dennis Williamson Oct 23 '09 at 19:13

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

cat <<FIN | ./script.sh
value1
value2
FIN
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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