user6862251
user6862251

Reputation:

Why command "read" doesn't work?

#!/bin/bash
while read user;
do
echo "Do you want to send the message to $user?"
read response
case $response in
Y|y) echo The message was sent;;
N|n) echo The message was not sent;;
Q|q) exit;;
esac
done < users

Why doesn't this code work on Ubuntu? If I run next code, it'll be done;

#!/bin/bash
while read user;
do
echo "Do you want to send the message to $user?"
done < users

Upvotes: 1

Views: 2202

Answers (1)

P....
P....

Reputation: 18411

You need to read response from terminal so use </dev/tty for user response. Or to be more specific, /proc/$$/fd/0 can be used for taking input from stdin.

#!/bin/bash
while read user;
do
echo "Do you want to send the message to $user?"
IFS="" read -r response  </dev/tty # OR < /proc/$$/fd/0
case $response in
Y|y) echo The message was sent;;
N|n) echo The message was not sent;;
Q|q) exit;;
esac
done < users

Upvotes: 3

Related Questions