1. -x 参数判断$myPath是否存在并且是否具有可执行权限
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
if [ ! -x "$myPath"]; then
mkdir "$myPath"
fi
2. -d 参数判断$myPath是否存在
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
if [ ! -d "$myPath"]; then
mkdir "$myPath"
fi
3. -f参数判断$myFile是否存在
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
if [ ! -f "$myFile" ]; then
touch "$myFile"
fi
4. -n是判断一个变量是否是否有值
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
if [ ! -n "$myVar" ]; then
echo "$myVar is empty"
exit 0
fi
5. #两个变量判断是否相等
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
if [ ! -n "$myVar" ]; then
echo "$myVar is empty"
exit 0
fi
4. 判断条件和实例
整数比较:
-eq(equal) : 测试两个整数是否相等;比如 $A -eq $B
-ne(inequality) : 测试两个整数是否不等;不等,为真;相等,为假;
-gt(greter than) : 测试一个数是否大于另一个数;大于,为真;否则,为假;
-lt(less than) : 测试一个数是否小于另一个数;小于,为真;否则,为假;
-ge(greter equal): 大于或等于
-le(less equal) :小于或等于
命令的间逻辑关系:
逻辑与: &&
第一个条件为假时,第二条件不用再判断,最终结果已经有;
第一个条件为真时,第二条件必须得判断;
逻辑或: ||
! id user6 && useradd user6
id user6 || useradd user6
[ `wc -l /etc/inittab | cut -d' ' -f1` -gt 100 ] && echo"Large file."
id user1 && echo "user1 exists." || useradd user1
! id user1 && useradd user1 || echo "user1 exists."
! id user1 && useradd user1 && echo "user1" |passwd --stdin user1 || echo "user1exists."
5. 特殊符号含义
$0 这个程式的执行名字
$n 这个程式的第n个参数值,n=1..9
$* 这个程式的所有参数,此选项参数可超过9个。
$# 这个程式的参数个数
$$ 这个程式的PID(脚本运行的当前进程ID号)
$! 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号)
$? 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误)
$- 显示shell使用的当前选项,与set命令功能相同
$@ 跟$*类似,但是可以当作数组用