Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read from user within while-loop read line?

I had a bash file which prompted the user for some parameters and used defaults if nothing was given. The script then went on to perform some other commands with the parameters. This worked great - no problems until most recent addition.

In an attempt to read the NAMES parameter from a txt file, I've added a while-loop to take in the names in the file, but I would still like the remaining parameters prompted for.

But once I added the while loop, the output shows the printed prompt in get_ans() and never pauses for a read, thus all the defaults are selected.

I would like to read the first parameter from a file, then all subsequent files from prompting the user.

What did I break by adding the while-loop?

cat list.txt | 
while read line
do 
  get_ans "Name" "$line"
  read NAME < $tmp_file

  get_ans "Name" "$line"
  read NAME < $tmp_file
done

function get_ans
{
  if [ -f $tmp_file ]; then
    rm $tmp_file

  PROMPT=$1
  DEFAULT=$2

  echo -n "$PROMPT [$DEFAULT]: "
  read ans
  if [ -z "$ans" ]; then
    ans="$DEFAULT"
  fi
  echo "$ans" > $tmp_file
}

(NOTE: Code is not copy&paste so please excuse typos. Actual code has function defined before the main())

like image 286
c card Avatar asked Mar 13 '12 16:03

c card


People also ask

What is while read line in shell script?

The while read loop. Here, cat reads each line from standard input and writes the line to standard output. It continues until the end-of-file condition is reached on file1, then it stops. In shell programming, we can read a line from standard input using the read command.


2 Answers

You pipe data into your the while loops STDIN. So the read in get_ans is also taking data from that STDIN stream.

You can pipe data into while on a different file descriptor to avoid the issue and stop bothering with temp files:

while read -u 9 line; do
   NAME=$(get_ans Name "$line")
done 9< list.txt

get_ans() {
    local PROMPT=$1 DEFAULT=$2 ans
    read -p "$PROMPT [$DEFAULT]: " ans
    echo "${ans:-$DEFAULT}"
}
like image 107
glenn jackman Avatar answered Oct 13 '22 02:10

glenn jackman


To read directly from the terminal, not from stdin (assuming you're on a *NIX machine, not a Windows machine):

while read foo</some/file; do
    read bar</dev/tty
    echo "got <$bar>"
done
like image 30
aqn Avatar answered Oct 13 '22 01:10

aqn