num1-eq num2 等于 [3 -eq $mynum ]
num1-ne num2 不等于 [3 -ne $mynum ]
num1-lt num2 小于 [3 -lt $mynum ]
num1-le num2 小于或等于 [3 -le $mynum ]
num1-gt num2 大于 [3 -gt $mynum ]
num1-ge num2 大于或等于 [3 -ge $mynum ]
脚本示例:
#!/bin/bash
#This script prints a message about your weight if you give it your
#weight in kilos and hight in centimeters.
if[ ! $# == 2 ]; then
echo"Usage: {GetProperty(Content)} weight_in_kiloslength_in_centimeters"
exit
fi
weight="{GetProperty(Content)}"
height="$2"
idealweight=$[$height- 110]
if[ $weight -le $idealweight ] ; then
echo"You should eat a bit more fat."
else
echo"You should eat a bit more fruit."
fi
#weight.sh 70 150
Youshould eat a bit more fruit.
#weight.sh 70 150 33
Usage:./weight.sh weight_in_kilos length_in_centimeters
位置参数{GetProperty(Content)},$2,..., $N,$#代表了命令行的参数数量,{GetProperty(Content)}代表了脚本的名字,
第一个参数代表{GetProperty(Content)},第二个参数代表$2,以此类推,参数数量的总数存在$#中,上面的例子显示了怎么改变脚本,如果参数少于或者多余2个来打印出一条消息。
执行,并查看情况。
#bash -x tijian.sh 60 170
+weight=60
+height=170
+idealweight=60
+'[' 60 -le 60 ']'
+echo 'You should eat a bit more fat.'
Youshould eat a bit more fat.
其中-x用来检查脚本的执行情况
if 语句格式
if 条件
then
Command
else
Command
fi 别忘了这个结尾
if语句忘了结尾fi
test.sh:line 14: syntax error: unexpected end of fi
if的三种条件表达式
if
command
then
if
函数
then
命令执行成功,等于返回0(比如grep ,找到匹配)
执行失败,返回非0(grep,没找到匹配)
if[ expression_r_r_r ]
then
表达式结果为真,则返回0,if把0值引向then
iftest expression_r_r_r
then
表达式结果为假,则返回非0,if把非0值引向then
[ ]&& ——快捷if
[ -f"/etc/shadow" ] && echo "This computer usesshadow passwors"
&&可以理解为then
如果左边的表达式为真则执行右边的语句
shell的if与c语言if的功能上的区别
shellif c语言if
0为真,走then
正好相反,非0走then
不支持整数变量直接if
必须:if[ i –ne 0 ]
但支持字符串变量直接if
if [str ] 如果字符串非0
支持变量直接if
if (i)
以command作为if条件
以多条command或者函数作为if条件
echo–n “input:”
readuser
if
多条指令,这些命令之间相当于“and”(与)
grep$user /etc/passwd >/tmp/null
who -u| grep $user
then 上边的指令都执行成功,返回值$?为0,0为真,运行then










