判断b.txt这个文件是否存在,存在输出1,不存在输出0
[ -f b.txt ]&& echo 1||echo 0
-f:判断是否为文件
-e:判断文件是否存在
-d:判断是否为目录
-r:判断是否可读
-w:判断是否可写
-x:判断是否可执行
对单个文件或目录变量的测试需要加双引号,避免错误
file=/etc/services [ -f "$file" ]&& echo 1||echo 0
条件表达式判断条件后面执行多条命令语句写法
#!/bin/bash [ $1 -eq 2 ]&&{ echo "true" }||{ echo "false" } # &&成立后执行后面的语句; ||不成立就执行后面的语句 #如果输入的值等于2就打印true #否则打印false #sh test.sh 2:打印true
常用字符串测试操作符:
-z "字符串" | 字符串长度为0则为真 |
-n "字符串" | 字符串长度不为0则为真 |
"串1" = "串2" | 串1等于串2则为真 |
"串1" != "串2" | 串1不等于串2则为真 |
PS:
①、以上表格中的字符串测试操作符号务必要用""引起来
②、比较符号的两端必须有空格
#字符串长度为0所以输出1 [ -n "" ]&& echo 1||echo 0
整数表达式:
Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
变量 | 含义 |
---|---|
$0 | 当前脚本的文件名 |
$n | 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。 |
$# | 传递给脚本或函数的参数个数。 |
$* | 传递给脚本或函数的所有参数。 |
$@ | 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。 |
$? | 上个命令的退出状态,或函数的返回值。 |
$$ | 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。 |
命令行参数
运行脚本时传递给脚本的参数称为命令行参数。命令行参数用 $n 表示,例如,$1 表示第一个参数,$2 表示第二个参数,依次类推。
逻辑连接符:
比较两个整数的大小案例:
第一种方法:
#!/bin/bash read -p "please input two num:" num1 num2 a=$num1 b=$num2 #判断键入值是否为空 [ -z "$a" -o -z "$b" ]&&{ echo "USAGE: num1 num2" exit 1 } #a变量如果不是整数则打印echo [ "`echo "$a"|sed -r 's#[^0-9]##g'`" = "$a" ]||{ echo "first arg must be int." exit 2 } #b变量如果不是整数则打印echo [ "`echo "$b"|sed -r 's#[^0-9]##g'`" = "$b" ]||{ echo "second arg must be int." exit 2 } #判断a与b的大小关系 [ $a -lt $b ]&&{ echo "$a<$b" exit 0 } [ $a -eq $b ]&&{ echo "$a=$bA" exit 0 } [ $a -gt $b ]&&{ echo "$a>$b" exit 0 }
第二种方法:
#!/bin/bash #键入值必须为2个 [ $# -ne 2 ]&&{ echo "USAGE: num1 num2" exit 1 } #a变量如果不是整数则打印echo [ "`echo "$1"|sed -r 's#[^0-9]##g'`" = "$1" ]||{ echo "first arg must be int." exit 2 } #b变量如果不是整数则打印echo [ "`echo "$2"|sed -r 's#[^0-9]##g'`" = "$2" ]||{ echo "second arg must be int." exit 2 } #判断a与b的大小关系 [ $1 -lt $2 ]&&{ echo "$1<$2" exit 0 } [ $1 -eq $2 ]&&{ echo "$1=$2" exit 0 } [ $1 -gt $2 ]&&{ echo "$1>$2" exit 0 }
一二级菜单操作案例:
#!/bin/bash file=/test/3.sh menu(){ cat <<END 1.[install lamp] 2.[install lnmp] 3.[exit] please input one number: END } menu read num1 a=$num1 #判断脚本是否存在 [ -e $file ]||{ echo "$file is not!" } #判断脚本是否可执行 [ -x $file ]||{ echo "$file not execute!" exit 0 } #用户键入1,执行脚本,并打印脚本执行语句 [ $a -eq 1 ]&&{ echo "start installing lamp" sh $file echo "lamp is installed!" exit 1 } [ $a -eq 2 ]&&{ echo "start installing lnmp" sh $file echo "lnmp is installed" exit 2 } [ $a -eq 3 ]&&{ exit 3 } echo "Input error!" exit 0
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@test test]# echo 4+6|bc 10 [root@test test]# echo 4*6|bc 24 [root@test test]# echo 4/6|bc 0 [root@test test]# echo 4-6|bc -2 #10进制转2进制或16进制 [root@test test]# echo "obase=2;6"|bc 110 [root@test test]# echo "obase=16;60"|bc 3C #awk数值运算 [root@test test]# echo 60 2|awk '{print $1-$2}' 58 [root@test test]# echo 60 2|awk '{print $1*$2}' 120 [root@test test]# echo 60 2|awk '{print $1/$2}' 30 [root@test test]# echo 60 2|awk '{print $1+$2}' 62 [root@test test]# 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 "please input three number:" num1 num2 num3
例子:
#!/bin/sh read -p "please input two number:" num1 num2 a=$num1 b=$num2 #判断键入值是否为空 if [ -z $a ] then echo "please input a!" exit 1 fi if [ -z $b ] then echo "please input b!" exit 1 fi #判断键入值是否为数字 expr $a + $b + 1 &>/dev/null if [ $? -ne 0 ] then echo "is not int!" exit 2 fi #no.3 echo "$a-$b =$[a-b]" echo "$a+$b =$[a+b]" echo "$a*$b =$[a*b]" if [ $b -eq 0 ] then echo "b is not zero!" else echo "$a/$b =$[a/b]" fi echo "$a**$b =$[a**b]" if [ $b -eq 0 ] then echo "b is not zero!" 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:$0 red|green|yellow|blue word" } #设定字体颜色 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 [ $# -ne 2 ];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 "please input one number:" num1 } menu a=$num1 case "$a" in 1) echo -e "this is ${RED_COLOR}苹果 $RES" ;; 2) echo -e "this is ${GREEN_COLOR}梨子 $RES" ;; 3) echo -e "this is ${YELLOW_COLOR}香蕉 $RES" ;; 4) echo -e "this is ${BLUE_COLOR}樱桃 $RES" ;; *) echo "please input 1~4 number!" exit 0 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 "Nginx is started!" /bin/false else $nginx &>/dev/null action "Nginx is start!" /bin/true fi } #stop_nginx stop_nginx(){ if [ -f $nginx_pid ];then $nginx -s stop &>/dev/null action "Nginx is stop!!" /bin/true else action "Nginx is stoped!" /bin/false fi } #reload_nginx reload_nginx(){ if [ -f $nginx_pid ];then $nginx -s reload &>/dev/null action "Nginx is reloaded!" /bin/true else action "Can't open $nginx_pid, no such this file" /bin/false fi } quit(){ exit 0 } case "$1" in start) start_nginx ;; stop) stop_nginx ;; restart) stop_nginx sleep 2 start_nginx ;; reload) reload_nginx ;; *) echo "USAGE:$0 start|stop|reload" quit esac
添加到开机自启动步骤:
1、cp startNginx.sh /etc/rc.d/init.d/startNginx
2、chmod +x startNginx
3、vim startNginx
#添加以下两行 # chkconfig: 2345 56 60 # Description: this is a http server.
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存在,则添加成功】
就可以使用service startNginx start|stop|restart|reload
shell脚本手机充值例子:
#!/bin/sh sum=1000 i=500 method(){ read -p "please input msg:" TXT read -p "send msg,are you sure?[y|n]" select case "$select" in y) ((sum=sum-i)) echo "send $TXT is ok!" echo "you have $sum money!" ;; n) echo "you select can't send msg!" echo "you have $sum money!" exit 1 ;; *) echo "USAGE: please input [y|n]!" exit 2 esac } #判断是否为int judge(){ expr $1 + 1 &>/dev/null #这里有个小问题,如果充值负数也可以...知道的,请赐教...谢谢 if [ $? -ne 0 -a "$1" != "-1" ];then echo "INVALID INPUT!" exit 8 else return 0 fi } while true do if [ $sum -ge $i ];then method else read -p "您的余额不足,是否需要充值?[y|n]" select2 case "$select2" in y) read -p "How much do you want to recharge[INT]:" add judge $add ((sum=sum+${add})) echo "pay successful! The balance is ${sum} !" read -p "Whether to continue texting?[y|n]" msg case "$msg" in y) while [ $sum -ge $i ] do method done continue ;; n) echo "即将退出!感谢使用!" exit 6 ;; *) echo "USAGE:您的输入有误!" exit 5 esac ;; n) echo "you only $sum money!Can't send msg!" exit 3 ;; *) echo "USAGE: without the select!" exit 4 esac break fi done
while循环小结:
shell脚本调试方法: