0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

複数のディレクトリをファイルから読み込んで存在とパーミッションチェック

Posted at

複数のディレクトリをファイルから読み込んで、それぞれのディレクトリが存在するか、そしてパーミッションが「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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?