在Shell腳本中,你可以使用if語句和文件測試操作符來檢查文件的存在性、類型或屬性
if [ -e "filename" ]; then
echo "File exists."
else
echo "File does not exist."
fi
if [ -f "filename" ]; then
echo "It's a regular file."
else
echo "It's not a regular file."
fi
if [ -d "directoryname" ]; then
echo "It's a directory."
else
echo "It's not a directory."
fi
if [ -r "filename" ]; then
echo "The file is readable."
else
echo "The file is not readable."
fi
if [ -w "filename" ]; then
echo "The file is writable."
else
echo "The file is not writable."
fi
if [ -x "filename" ]; then
echo "The file is executable."
else
echo "The file is not executable."
fi
注意:在上述示例中,"filename"
和 "directoryname"
需要替換為你要檢查的實際文件名或目錄名。另外,-e
是 -exists
的簡寫,-f
是 -file
的簡寫,依此類推。你可以根據需要選擇使用簡寫或完整形式。