操作 |
描述 |
-d |
测试是否为目录(Directory) |
-a |
测试目录或文件是否存在(Exist) |
-f |
测试是否为文件(File) |
-r |
测试当前用户是否可读(read) |
-w |
测试当前用户是否可写(write) |
-x |
测试当前用户是否可执行(cxcutc) |
[root@hdp-1 shell]
-rw-r--r--. 1 root root 102 11月 2 15:41 host.txt
[root@hdp-1 shell]
0
[root@hdp-1 shell]
[root@hdp-1 shell]
--w-------. 1 root root 102 11月 2 15:41 host.txt
[root@hdp-1 shell]
0
[root@hdp-1 shell]
0
[root@hdp-1 shell]
1
2.2 整数测试
- 整数测试通常用于数值之间的运算,其语法格式为 [ 整数1 操作符 整数2 ]
- 或 test 整数1 操作符 整数2。
操作符 |
含义 |
-eq |
等于(Equal) |
-ne |
不等于(Not Equal) |
-gt |
大于(Greater Than) |
-lt |
小于(Lesser Than) |
-le |
小于或等于(Lesser or Equal) |
-ge |
大于或等于(Greater or Equal) |
[root@hdp-1 shell]
read -p "Input a username: " user
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user already exsits."
else
useradd $user
if [ $? -eq 0 ];then
echo