gitbashunixgit-post-receivepost-receive-email

Why is read hanging my bash script?


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?


Solution

  • 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