别着急,坐和放宽
+,++
-,--
*
/
**(乘方)
%(求余)
使用expr进行运算
expr 4 + 5 # 运算符左右需要空格
num1=`expr 4 + 5`
echo $num1
name=zhangsan
len=`expr length $name`
echo $len
是let命令的简写
注意做赋值和输出时需要在外面加$
最常用
((a=10))
((a++))
echo $((10+20))
((a=4+5))
echo $a
# 如果是下面写法呢
b=4+5
echo $b
((5>4))
echo $?
# 真为0值
((5<4))
echo $?
# 假为非0值
(( 5>4 && 6>5 ))
echo $?
[]还有扩展写法为[[]],支持&&,||,<,>
至少要有一个空格
./start_service.sh abc
# 是否存在且是文件
test -f /etc/passwd
# 等价 [ -f /etc/passwd ]
# 是否存在且是目录
test -d /etc
# 等价 [ -d /etc ]
# 是否存在
test -e /etc
# 等价 [ test -e /etc ]
# 字符串长度是否为0(为0则表达式成立)
test -z hello
# 等价 [ test -z hello ]
# 判断数字
[ 5 -gt 4 ]
# 等价 [[ 5 > 4 ]]
# -gt >
# -lt <
# -eq =
# -ge >=
# -le <=
# 判断字符串
[ "abc" = "abc" ]
# 区分大小写
[ "abc" = "Abc" ]
if [ 测试条件成立 ] 或者使用命令返回0值
then
执行命令
fi
if [ $UID = 0 ]
then
echo "you are root"
fi
if pwd
then
echo "pwd is running"
fi
if [ 测试条件成立 ]
then
执行命令
else
执行条件不成功的命令
fi
#!/bin/bash
# if-else的使用
if [ $USER = root ] ;then
echo "you are root"
echo $UID
else
echo "you are other"
echo $UID
fi
if [ 测试条件成立 ]
then
执行命令
elif [ 测试条件成立 ]
then
执行命令
else
执行命令
fi
#!/bin/bash
# if-elif-else的使用
if [ $USER = root ] ; then
echo "you are root"
elif [ $USER = chendong ] ; then
echo "you are chendong"
else
echo "you are other"
fi
#!/bin/bash
if [ $UID = 0 ] ;then
echo "please run"
if [ -x /root/shell/5.sh ] ;then
bash /root/shell/5.sh
fi
else
echo "switch root user"
fi
case "$变量" in
"情况1")
执行命令 ;;
"情况2")
执行命令 ;;
*)
执行命令 ;;
esac
#!/bin/bash
# 注意:判断条件中的字符串一定要使用双引号
case "$1" in
"start"|"START")
echo "start application"
echo "applicaion is running"
;;
"stop"|"STOP")
echo "stop application"
echo "applicaion is stopped"
;;
*)
echo "unknow option"
echo "Usage: $0 {start|stop}"
;;
esac