Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the left angle bracket after a while loop mean in bash?

Tags:

bash

loops

The following is from /etc/init.d/functions on RHEL. I'm trying to figure out what the __pids_var_run() function does when I came across this while loop.

            while : ; do
                    read line
                    [ -z "$line" ] && break
                    for p in $line ; do
                            if [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] ; then
                                    if [ -n "$binary" ] ; then
                                            local b=$(readlink /proc/$p/exe | sed -e 's/\s*(deleted)$//')
                                            [ "$b" != "$binary" ] && continue
                                    fi
                                    pid="$pid $p"
                            fi
                    done
            done < "$pid_file"

Could someone explain what while : ; do ; ... done < "$pid_file" does? More specifically, the last part after done, as the rest of it more or less makes sense.

like image 936
Teofrostus Avatar asked Mar 07 '16 19:03

Teofrostus


1 Answers

It means that any command in the loop that reads something from stdin will read from the given file (instead of the keyboard, for example).

In this case in particular, the loop uses read line to read a single line from stdin, so when you redirect from $pidfile it effectively reads the file line by line.

To further read about redirections, here's an Illustrated redirection tutorial which is recommended by this Bash Guide by Lhunath and GreyCat.

like image 119
alfC Avatar answered Oct 04 '22 11:10

alfC