複数のディレクトリをファイルから読み込んで、それぞれのディレクトリが存在するか、そしてパーミッションが「770」かどうかをチェックするスクリプトは以下の通りです。
check_dirs.sh
#!/bin/bash
# ディレクトリ一覧ファイルの指定(引数で渡す)
if [ $# -ne 1 ]; then
echo "使用法: $0 ディレクトリ一覧ファイル"
exit 1
fi
file="$1"
# ファイルが存在するか確認
if [ ! -f "$file" ]; then
echo "ファイル '$file' が存在しません。"
exit 1
fi
# ファイル内の各ディレクトリを読み込んで処理
while IFS= read -r dir; do
# 空行やコメント行を無視
[ -z "$dir" ] && continue
[[ "$dir" =~ ^# ]] && continue
if [ -d "$dir" ]; then
perm=$(stat -c '%a' "$dir")
if [ "$perm" = "770" ]; then
echo "$dir は存在し、パーミッションは 770 です。"
else
echo "$dir は存在しますが、パーミッションは $perm です(770 ではありません)。"
fi
else
echo "$dir は存在しません。"
fi
done < "$file"
使用例:
ディレクトリ一覧ファイル(例: dirs.txt)を用意:
dirs.txt
/test/env1/dirA
/test/env2/dirB
# コメント行もOK
/test/env3/dirC
スクリプトを実行:
./check_dirs.sh dirs.txt