在while循环中退出bash脚本

时间:2017-04-18 23:08:41

标签: linux bash

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

E.g。以下退出只退出管道子shell:

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

1 个答案:

答案 0 :(得分:2)

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

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

或者更简单:

  private Map <Integer, Integer> map = new HashMap<>();