在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
的简写,依此类推。你可以根据需要选择使用简写或完整形式。