kensuke1984 kensuke1984 - 1 year ago 94
Bash Question

How to create a shell script (/bin/sh) with option -s and read input inside

I want to run a shell script (POSIX) like this.

# this is
echo your age?
read age
echo "You are $age."

If this is thrown to /bin/sh through standard input as

cat | /bin/sh -s

then the question is skipped.

How can I ask the question?

Answer Source

The problem is that /bin/sh is reading the script, and your script is reading input, from the same input file: the pipe fed by cat

One solution is to modify your script to read directly from the terminal, not standard input, although that may not be exactly what you want.

echo "your age?"
read age < /dev/tty
echo "Your age $age"

The better solution is to not read the script from standard input; just pass it as an argument to /bin/sh.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download