Shell的条件表达式介绍

判断b.txt这个文件是否存在,存在输出1,不存在输出0

[-fb.txt]&&echo1||echo0

-f:判断是否为文件

-e:判断文件是否存在

-d:判断是否为目录

-r:判断是否可读

-w:判断是否可写

-x:判断是否可执行


对单个文件或目录变量的测试需要加双引号,避免错误

file=/etc/services
[-f"$file"]&&echo1||echo0

条件表达式判断条件后面执行多条命令语句写法

#!/bin/bash
[$1-eq2]&&{
echo"true"
}||{
echo"false"
}
#&&成立后执行后面的语句;||不成立就执行后面的语句
#如果输入的值等于2就打印true
#否则打印false
#shtest.sh2:打印true


常用字符串测试操作符:

-z "字符串" 字符串长度为0则为真
-n "字符串" 字符串长度不为0则为真
"串1" = "串2" 串1等于串2则为真
"串1" != "串2" 串1不等于串2则为真

PS:

①、以上表格中的字符串测试操作符号务必要用""引起来

②、比较符号的两端必须有空格

#字符串长度为0所以输出1
[-n""]&&echo1||echo0


整数表达式:


Shell特殊变量:Shell $0,$#,$*,$@,$?,$$和命令行参数


特殊变量列表
变量 含义
$0 当前脚本的文件名
$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。
$# 传递给脚本或函数的参数个数。
$* 传递给脚本或函数的所有参数。
$@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。
$? 上个命令的退出状态,或函数的返回值。
$$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。

命令行参数

运行脚本时传递给脚本的参数称为命令行参数。命令行参数用 $n 表示,例如,$1 表示第一个参数,$2 表示第二个参数,依次类推。



逻辑连接符:


比较两个整数的大小案例:

第一种方法:

#!/bin/bash
read-p"pleaseinputtwonum:"num1num2
a=$num1
b=$num2
#判断键入值是否为空
[-z"$a"-o-z"$b"]&&{
echo"USAGE:num1num2"
exit1
}
#a变量如果不是整数则打印echo
["`echo"$a"|sed-r's#[^0-9]##g'`"="$a"]||{
echo"firstargmustbeint."
exit2
}
#b变量如果不是整数则打印echo
["`echo"$b"|sed-r's#[^0-9]##g'`"="$b"]||{
echo"secondargmustbeint."
exit2
}
#判断a与b的大小关系
[$a-lt$b]&&{
echo"$a<$b"
exit0
}
[$a-eq$b]&&{
echo"$a=$bA"
exit0
}
[$a-gt$b]&&{
echo"$a>$b"
exit0
}

第二种方法:

#!/bin/bash
#键入值必须为2个
[$#-ne2]&&{
echo"USAGE:num1num2"
exit1
}
#a变量如果不是整数则打印echo
["`echo"$1"|sed-r's#[^0-9]##g'`"="$1"]||{
echo"firstargmustbeint."
exit2
}
#b变量如果不是整数则打印echo
["`echo"$2"|sed-r's#[^0-9]##g'`"="$2"]||{
echo"secondargmustbeint."
exit2
}
#判断a与b的大小关系
[$1-lt$2]&&{
echo"$1<$2"
exit0
}
[$1-eq$2]&&{
echo"$1=$2"
exit0
}
[$1-gt$2]&&{
echo"$1>$2"
exit0
}


一二级菜单操作案例:

#!/bin/bash
file=/test/3.sh
menu(){
cat<<END
1.[installlamp]
2.[installlnmp]
3.[exit]
pleaseinputonenumber:
END
}
menu
readnum1
a=$num1
#判断脚本是否存在
[-e$file]||{
echo"$fileisnot!"
}
#判断脚本是否可执行
[-x$file]||{
echo"$filenotexecute!"
exit0
}
#用户键入1,执行脚本,并打印脚本执行语句
[$a-eq1]&&{
echo"startinstallinglamp"
sh$file
echo"lampisinstalled!"
exit1
}
[$a-eq2]&&{
echo"startinstallinglnmp"
sh$file
echo"lnmpisinstalled"
exit2
}
[$a-eq3]&&{
exit3
}
echo"Inputerror!"
exit0


if条件语句

单分支结构语法:

if[条件]
then
指令
fi

或

if[条件];then
指令
fi

双分支结构语法:

if[条件]
then
指令1
else
指令2
fi

多分支结构语法:

if条件1
then
指令1
elif条件2
then
指令2
else
指令3
fi


shell脚本之特殊变量

$0       脚本的名称
$1,$2,$3....   第一个参数,第二个参数,第三个参数
$?  上一次执行的状态码
$#  参数个数
$*  参数列表
$@  参数列表


shell的数值运算方法

expr、(())、let、bc、$[]、awk、typeset

常用就(())、let这两种

#加减乘除
[root@testtest]#echo4+6|bc
10
[root@testtest]#echo4*6|bc
24
[root@testtest]#echo4/6|bc
0
[root@testtest]#echo4-6|bc
-2

#10进制转2进制或16进制
[root@testtest]#echo"obase=2;6"|bc
110
[root@testtest]#echo"obase=16;60"|bc
3C

#awk数值运算
[root@testtest]#echo602|awk'{print$1-$2}'
58
[root@testtest]#echo602|awk'{print$1*$2}'
120
[root@testtest]#echo602|awk'{print$1/$2}'
30
[root@testtest]#echo602|awk'{print$1+$2}'
62

[root@testtest]#echo"`seq-s"+"10`="$((`seq-s"+"10`))
1+2+3+4+5+6+7+8+9+10=55

[root@test~]#echo`seq-s'+'"10"`=`seq-s"+""10"|bc`
1+2+3+4+5+6+7+8+9+10=55

[root@test~]#echo`echo{1..5}|tr"""+"`=`echo{1..5}|tr"""+"|bc`
1+2+3+4+5=15


变量的读入之read

read-p"pleaseinputthreenumber:"num1num2num3

例子:

#!/bin/sh
read-p"pleaseinputtwonumber:"num1num2
a=$num1
b=$num2
#判断键入值是否为空
if[-z$a]
then
echo"pleaseinputa!"
exit1
fi
if[-z$b]
then
echo"pleaseinputb!"
exit1
fi
#判断键入值是否为数字
expr$a+$b+1&>/dev/null
if[$?-ne0]
then
echo"isnotint!"
exit2
fi
#no.3
echo"$a-$b=$[a-b]"
echo"$a+$b=$[a+b]"
echo"$a*$b=$[a*b]"
if[$b-eq0]
then
echo"bisnotzero!"
else
echo"$a/$b=$[a/b]"
fi
echo"$a**$b=$[a**b]"
if[$b-eq0]
then
echo"bisnotzero!"
else
echo"$a%$b=$[a%b]"
fi


给字符串设定颜色:

#!/bin/sh
#颜色
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
#报错信息反馈
usage(){
echo"USAGE:$0red|green|yellow|blueword"
}
#设定字体颜色
color(){
if["$1"="red"];then
echo-e"${RED_COLOR}$2$RES"
elif["$1"="green"];then
echo-e"${GREEN_COLOR}$2$RES"
elif["$1"="yellow"];then
echo-e"${YELLOW_COLOR}$2$RES"
elif["$1"="blue"];then
echo-e"${BLUE_COLOR}$2$RES"
else
usage
fi
}
#主函数
main(){
if[$#-ne2];then
usage
else
color$1$2
fi
}
main$*


执行脚本打印水果菜单并加上不同的颜色以及介绍:

#!/bin/sh
#颜色
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
#菜单
menu(){
cat<<END
1.apple
2.pear
3.banana
4.cherry
END
read-p"pleaseinputonenumber:"num1
}
menu
a=$num1
case"$a"in
1)
echo-e"thisis${RED_COLOR}苹果$RES"
;;
2)
echo-e"thisis${GREEN_COLOR}梨子$RES"
;;
3)
echo-e"thisis${YELLOW_COLOR}香蕉$RES"
;;
4)
echo-e"thisis${BLUE_COLOR}樱桃$RES"
;;
*)
echo"pleaseinput1~4number!"
exit0
esac


Nginx启动脚本并实现加入开机自启动管理

#!/bin/sh
[-f/etc/init.d/functions]&&./etc/init.d/functions
nginx_pid=/usr/local/nginx/logs/nginx.pid
nginx=/usr/local/nginx/sbin/nginx
#start_nginx
start_nginx(){
if[-f$nginx_pid];then
action"Nginxisstarted!"/bin/false
else
$nginx&>/dev/null
action"Nginxisstart!"/bin/true
fi
}
#stop_nginx
stop_nginx(){
if[-f$nginx_pid];then
$nginx-sstop&>/dev/null
action"Nginxisstop!!"/bin/true
else
action"Nginxisstoped!"/bin/false
fi
}
#reload_nginx
reload_nginx(){
if[-f$nginx_pid];then
$nginx-sreload&>/dev/null
action"Nginxisreloaded!"/bin/true
else
action"Can'topen$nginx_pid,nosuchthisfile"/bin/false

fi
}
quit(){
exit0
}
case"$1"in
start)
start_nginx
;;
stop)
stop_nginx
;;
restart)
stop_nginx
sleep2
start_nginx
;;
reload)
reload_nginx
;;
*)
echo"USAGE:$0start|stop|reload"
quit
esac

添加到开机自启动步骤:

1、cp startNginx.sh /etc/rc.d/init.d/startNginx

2、chmod +x startNginx

3、vim startNginx

#添加以下两行
#chkconfig:23455660
#Description:thisisahttpserver.

2345:是指系统运行级别,分别为rc.2、rc.3、rc.4、rc.5

56:启动的优先级【不能使用被占用的序号】

60:关闭的优先级【不能使用被占用的序号】

PS:如果启动优先级配置的数太小时如0时,则有可能启动不成功,因为此时可能其依赖的网络服务还没有启动,从而导致自启动失败。

4、chkconfig --add startNginx

5、chkconfig startNginx on

6、chkconfig --list|grep startNginx【如果nginxStart存在,则添加成功】

就可以使用servicestartNginx start|stop|restart|reload


shell脚本手机充值例子:

#!/bin/sh
sum=1000
i=500
method(){
read-p"pleaseinputmsg:"TXT
read-p"sendmsg,areyousure?[y|n]"select
case"$select"in
y)
((sum=sum-i))
echo"send$TXTisok!"
echo"youhave$summoney!"

;;
n)
echo"youselectcan'tsendmsg!"
echo"youhave$summoney!"
exit1
;;
*)
echo"USAGE:pleaseinput[y|n]!"
exit2
esac
}
#判断是否为int
judge(){
expr$1+1&>/dev/null
#这里有个小问题,如果充值负数也可以...知道的,请赐教...谢谢
if[$?-ne0-a"$1"!="-1"];then
echo"INVALIDINPUT!"
exit8
else
return0
fi
}
whiletrue
do
if[$sum-ge$i];then
method
else
read-p"您的余额不足,是否需要充值?[y|n]"select2
case"$select2"in
y)
read-p"Howmuchdoyouwanttorecharge[INT]:"add
judge$add
((sum=sum+${add}))
echo"paysuccessful!Thebalanceis${sum}!"
read-p"Whethertocontinuetexting?[y|n]"msg
case"$msg"in
y)
while[$sum-ge$i]
do
method
done
continue
;;
n)
echo"即将退出!感谢使用!"
exit6
;;
*)
echo"USAGE:您的输入有误!"
exit5
esac
;;
n)
echo"youonly$summoney!Can'tsendmsg!"
exit3
;;
*)
echo"USAGE:withouttheselect!"
exit4
esac
break
fi
done


while循环小结:



shell脚本调试方法:

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...