1

I had a similar problem to this with Python using readlines() but I'm not sure if it's the same here.

The read command is hanging my bash script.

generate_email()
{
    # --- Arguments
    oldrev=$(git rev-parse $1)
    newrev=$(git rev-parse $2)
    refname="$3"

    # ... some code ...
}

# ... more code ...

while read oldrev newrev refname
do
    generate_email $oldrev $newrev $refname
done

Any ideas on how to fix this?

Bo A
  • 3,144
  • 2
  • 33
  • 49
  • Seems like you're expecting input (with 3 delimited arguments) to be parsed. You need to pipe it in. Either pass in that input to the script you've included above, or explicitly to the while loop (`[some-command] | while read oldrev newrev refname...`). – Gingi Aug 03 '12 at 19:15

2 Answers2

2

You're not telling read to read from anything. So it's just waiting for input from stdin.

If you're wanting to read from a file, you need to use read like so:

while read -r oldrev newrev refname; do
  generate_email "$oldrev" "$newrev" "$refname"
done < /path/to/file

Note the < /path/to/file. That's where you're actually telling read to read from the file.

If you're wanting to read from an input stream, you can use while read like so:

grep 'stuffInFile' /path/to/file |
while read -r oldrev newrev refname; do
  generate_email "$oldrev" "$newrev" "$refname"
done
Tim Pote
  • 27,191
  • 6
  • 63
  • 65
  • As Henk pointed out, care must be taken if `generate_email` is also reading from stdin. – chepner Aug 03 '12 at 19:14
  • To read from an input stream without turning the `while` loop into a subshell (which would cause any variable values set inside the loop to be lost when it exits), use process substitution instead of a pipe. `while read -r ... done < <(grep ...)` – Dennis Williamson Aug 03 '12 at 19:25
1

I'd say it's not hanging, but just waiting for input.

Watch out though and make sure that generate_email does not read from the same input stream.

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57