2017-04-18 180 views
0

如何从查找管道while循环中退出脚本?在while循环中退出bash脚本

E.g.下面的退出只是退出管道子shell:

find some_directory -type d|while read dir_name; do 
    if [ -w "$dir_name" ]; then 
    exit 1 
    fi 
done 
+2

你不能在运行subshel​​l'完成<<找到some_directory -type d)',probs想要使用'而IFS =读-r dir_name'来处理闪避文件名也是一样。 – 123

+0

为什么这么复杂? '找到some_directory -type d! -perm -u + w'会给你目录 –

+0

@ the.Legend中的所有非可写目录,你如何获得该查找的返回值? $?是0是否找到文件 – JDiMatteo

回答

2

您可以检查管道的返回状态:

if ! find some_directory -type d|while read dir_name; do 
    if [ -w "$dir_name" ]; then 
    exit 1 
    fi 
done; then exit 1; fi 

或者更简单地说:

find some_directory -type d|while read dir_name; do 
     [ -w "$dir_name" ] && exit 1 
    done || exit 1