文件判断的参数如下:
-d FILE True if file is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-z STRING True if string is empty.
-n STRING True if string is not empty.
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal§
if [ -d /path/to/directory ]; then
echo "This is a directory."
else
echo "This is not a directory."
fi
if [ -e /path/to/file ]; then
echo "File exists."
else
echo "File does not exist."
fi
在 Linux 中,普通文件(regular file) 是一种最常见的文件类型,它表示存储在文件系统中的常规数据文件。普通文件可以是文本文件、二进制文件、图像、音频、程序等,它与目录、链接、设备文件等其他类型的文件不同。
具体理解:
普通文件 是日常创建和使用的文件,例如 .txt
、.jpg
、.sh
等文件。
不是普通文件的类型
包括:
if [ -f /path/to/file ]; then
echo "This is a regular file."
else
echo "This is not a regular file."
fi
if [ -r /path/to/file ]; then
echo "File is readable."
else
echo "File is not readable."
fi
if [ -s /path/to/file ]; then
echo "File exists and is not empty."
else
echo "File does not exist or is empty."
fi
if [ -w /path/to/file ]; then
echo "File is writable."
else
echo "File is not writable."
fi
if [ -x /path/to/file ]; then
echo "File is executable."
else
echo "File is not executable."
fi
if [ -z "$STRING" ]; then
echo "String is empty."
else
echo "String is not empty."
fi
if [ -n "$STRING" ]; then
echo "String is not empty."
else
echo "String is empty."
fi
if [ "$STRING1" = "$STRING2" ]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
if [ "$STRING1" != "$STRING2" ]; then
echo "Strings are not equal."
else
echo "Strings are equal."
fi
使用各种文件和字符串判断条件,可以来控制脚本的执行。