條件判斷

判斷變數是否為空值

variable=""
if [ -z "$variable" ]
then
      echo "\$variable Is NULL"
else
      echo "\$variable is NOT NULL"
fi

判斷目錄是否存在

if [ -d "$DIRECTORY" ]; then
  # 目錄存在執行此 script
fi
if [ ! -d "$DIRECTORY" ]; then
  # 目錄不存在執行此 script
fi

判斷檔案是否存在

file_path="/my/file/path"
if [ -f "$file_path" ]
then
    # 檔案存在
    echo "$file_path found."
else
    # 檔案不存在
    echo "$file_path not found."
fi

參考資料

results matching ""

    No results matching ""