'Trying to loop through lines on stdin
I would like to make a short script that will eventually run a quiz.
I'm trying to read stdin
, which contains a new-line delimited list of words, for example:
vestirse
lavarse
lavarse los dientes
ducharse
bañarse
My program so far doesn't seem to be converting input into any type of iterable array:
read inputlines
IFS=$'\n' read -rd '' -a y <<<"$inputlines"
for line in "${lines[@]}"
do
echo "$line"
done
Now if I run pbpaste | bash wordlearner.bash
nothing is printed at all, even though I am expecting (at this point) basically a perfect echo
of what was input.
How do I store stdin
to be looped through at a later date?
Solution 1:[1]
Use a while loop, and then ask for input to read though /dev/tty
?
while read -r line; do
echo "$line"
echo "What do you think?"
read -r answer < /dev/tty
done
Alternative use mapfile
to slurp all lines into an array?
mapfile -t lines
for line in "${lines[@]}"; do
echo "$line"
done
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |