How to automatically pass 'multiple' user inputs

Hi Everyone,

1) I really cannot figure out how to pass multiple user inputs in a script. really need your help re this. below is the script.
-----------

#!/bin/sh
# script name: ask.sh

echo "Enter name: \c"
read NAME
echo "Your name is $NAME\n"

echo "Enter age: \c"
read AGE
echo "Your are is $AGE\n"

-----------

Now I want to automatically pass, say for example "Mickey" and "30" to the two prompts respectively but I can't figure out how. Been searching in Google but i can't find any.

So far, i can only pass 1 user input automatically such that to run ask.sh in this way:
echo "Mickey" | ask.sh

Would really appreciate all your help.

Thanks a lot!

you can pass it as arguments and read the value using $1 and $2 inside the script

 
./ask.sh "Mickey" "23"

otherwise, use it like this

 
$ cat d.sh
#!/bin/sh
read a b
echo "a value is : $a"
echo "b value is : $b"

 
$ echo "YES NO" | d.sh
a value is : YES
b value is : NO

1 Like

You can also do something like this:

./myscript.sh <<EOF
name
age
EOF

Or if you can rewrite the script, just pass them in as parameters:

#!/bin/sh

NAME="$1"
AGE="$2"
./myscript name age
1 Like

Thanks for the replies! :slight_smile: but I can't change the script. The script is intended to ask and receive user inputs, not as as parameters. I just need to automatically pass these user inputs. :smiley:

Then you need to use a tool like Expect

Then you need to use a tool like Expect