如果if-else语句中的所有条件都为空,则打印'something'

时间:2017-10-20 11:51:14

标签: bash if-statement

我在bash脚本中有一个函数,它对文件执行diff。如果存在echo,我已设置为diff,但如果不存在,我只想打印"No differences found in ANY Task Definitions"

这就是我所拥有的;

diff_taskDef () {
MAS=master.json
CUR=current.json
clear
mapfile -t awsReg < <(ls ~/regions)
for awsrg in "${awsReg[@]}"; do
 mapfile -t awsTD < <(ls ~/regions/"$awsrg")
    for td in "${awsTD[@]}"; do
     DIFF="$(diff -q ~/regions/"$awsrg"/"$td"/"$td"-"$MAS" ~/regions/"$awsrg"/"$td"/"$td"-"$CUR")"
       echo "$DIFF"
    done
done

所以不是空白屏幕,或者在没有正面diff"no diff found"的情况下滚动每个语句我只想要一个,如果没有什么不同

1 个答案:

答案 0 :(得分:2)

首先假设没有找到差异。如果找到差异,请更改标记diff_found的值。如果您愿意,可以停止寻找更多差异。

diff_taskDef () {
  MAS=master.json
  CUR=current.json
  clear
  diff_found=false
  for awsrg in ~/regions/*; do
    for td in "$awsrg"/*; do
      if ! diff -q "$td-$MAS" "$td/$td-$CUR"; then
          diff_found=true
          break 2  # optional
      fi
    done
  done
  if [[ $diff_found = false ]]; then
    echo "No differences found"
  fi
}