检查是否存在一个或多个目录 - Bash

时间:2018-01-15 13:17:43

标签: bash

我正在尝试打印每个目录的行,如果目录/文件不存在,我需要显示“missing”。

file="file.txt"
if [[ -d "$/folders/folder1" && -d "$/folders/folder1" ...]]; then
  if [ -e "$file" ]; then
      #the files that exist and contain the file, call the function
      for i in folder{1..11}; do
          echo  $i function
      done
   else
       #for the directories that don't exist
       echo "no directory exists"
 fi

else
   #for the directories that don't exist
   echo "no directory exists"

1 个答案:

答案 0 :(得分:2)

根据评论的要求,您可以这样做:

#!/usr/bin/env bash

do_stuff() {
    local file=$1
    local score max junk
    IFS=$'/ \t' read -r score max junk < <(tail -n 1 "$file")
    echo "$score / $max"
}

file="feedback.txt"

for dir in folders/folder{1..11}; do
    if ! [[ -f "$dir/$file" ]]; then
        echo "${dir##*/}: missing"
        continue
    fi

    #OK - do stuff here
    do_stuff "$dir/$file"
done