文件相关
-d:检查是否为目录(directory)
if [ -d "/path/to/directory" ]; then
echo "It's a directory."
fi
-f:检查是否为普通文件(file)
if [ -f "/path/to/file" ]; then
echo "It's a regular file."
fi
-e:检查文件是否存在
if [ -e "/path/to/file" ]; then
echo "The file exists."
fi
-s:检查文件是否非空
if [ -s "/path/to/file" ]; then
echo "The file is not empty."
fi
-r:检查文件是否可读
if [ -r "/path/to/file" ]; then
echo "The file is readable."
fi
-w:检查文件是否可写
if [ -w "/path/to/file" ]; then
echo "The file is writable."
fi
-x:检查文件是否可执行
if [ -x "/path/to/file" ]; then
echo "The file is executable."
fi
字符串
-n:检查字符串是否非空
string="Hello, World!"
if [ -n "$string" ]; then
echo "The string is not empty."
fi
-z:检查字符串是否为空
string="Hello, World!"
if [ -z "$string" ]; then
echo "The string is not empty."
fi
算术
-eq:检查两个整数是否相等
number1=10
number2=20
if [ "$number1" -eq "$number2" ]; then
echo "The numbers are equal."
fi
-ne:检查两个整数是否不相等
number1=10
number2=20
if [ "$number1" -ne "$number2" ]; then
echo "The numbers are not equal."
fi
-gt:检查第一个整数是否大于第二个整数
number1=30
number2=20
if [ "$number1" -gt "$number2" ]; then
echo "The first number is greater than the second number."
fi
-lt:检查第一个整数是否小于第二个整数
number1=10
number2=20
if [ "$number1" -lt "$number2" ]; then
echo "The first number is less than the second number."
fi
-ge:检查第一个整数是否大于等于第二个整数
number1=30
number2=20
if [ "$number1" -ge "$number2" ]; then
echo "The first number is greater than or equal to the second number."
fi
-le:检查第一个整数是否小于等于第二个整数
number1=10
number2=20
if [ "$number1" -le "$number2" ]; then
echo "The first number is less than or equal to the second number."
fi