在bash中,while循环后的左尖括号是什么意思?

Teo*_*tus 5 bash loops

以下内容来自RHEL上的/etc/init.d/functions.我试图找出__pids_var_run()当我遇到这个while循环时函数的作用.

            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"
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下while : ; do ; ... done < "$pid_file"吗?更具体地说,之后的最后一部分done,其余部分或多或少都是有道理的.

alf*_*lfC 5

这意味着循环中从 stdin 读取内容的任何命令都将从给定文件中读取(例如,而不是键盘)。

特别是在这种情况下,循环用于read line从标准输入读取一行,因此当您从$pidfile它重定向时,可以有效地逐行读取文件。

要进一步了解重定向,请参阅LhunathGreyCatBash 指南推荐的插图重定向教程

  • In 表示从文件中获取 stdin。它与键盘完全没有关系。通常,stdin 与键盘相关联,但将两者混为一谈是错误的。 (3认同)