shell脚本编程学习之路-逻辑操作符

1.逻辑操作符举例

shell脚本编程学习之路-逻辑操作符

提示:

!中文意思是反:与一个逻辑值相反的逻辑值。

-a中文意思是与(&&):两个逻辑值都为真返回值才为真,反之为假。

-o中文意思是或(or):两个逻辑值只要一个为真,返回值就为真。

结论:

真 true 1 假 false 0

(1)-a或&&(逻辑与)的运算规则:只有两端都是1才为真相当于乘法运算。

and 1*0=0 

and 0*1=0

and 1*1=1

and 0*0=0

只有两端都是1才为真,and交集

(2)-o或||(逻辑或)的运算规则:两端都是0才为假,任何一端不为0都是真。

or 1+0=1 真

or 1+1=2 真

or 0+1=1 真

or 0+0=0 假

两端都是0才为假,不为0就是真。or 并集

(3)这里有一个系统不等于的例子

shell脚本编程学习之路-逻辑操作符

例子演示:

shell脚本编程学习之路-逻辑操作符

2.实践

(1)“-a”和 “-o”,逻辑运算符号用于[]中使用。

(2)“&&”和 “||”,逻辑运算符号用于[[ ]]中使用。

shell脚本编程学习之路-逻辑操作符

(3)注意括号两端,必须要有空格。

3.系统脚本例子

shell脚本编程学习之路-逻辑操作符

4.小结:逻辑操作符使用总结

(1)[ ]中括号中使用-a,-o,!这种操作符(推荐使用)。

(2)[[ ]]双中括号中用&&,||,!这种操作符。

(3)逻辑操作符使用test和[]中括号。

(4)多个[]之间以及多个[[ ]]之间,或者任意混合中间逻辑操作符都用&&或||。

5.综合实例:

(1)以定义变量、脚本传参、以及read读入的方式比较两个整数的大小用条件表达式(禁止用if),进行判断并以屏幕输出的方式提醒用户比较结果,一共开发3个脚本。当用脚本传参以及read读入的方式需要对变量的值进行判断是否为数字并且如果传参个数不对给与用户提示。

a.脚本传参方式

[root@shellbiancheng ~]# cat zhengshubijiao.sh 
#!/bin/sh
#脚本传参
[ $# -ne 2 ]&&{
echo $"Usage :$0 {num1 and num2}"
exit 1
}

[[ "`echo "$1"|sed -r 's#[^0-9]##g'`" = "$1" ]]||{
echo "first arg must be int"
exit 2

}
[[ "`echo "$2"|sed -r 's#[^0-9]##g'`" = "$2" ]]||{
echo "first arg must be int"
exit 2

}
[ $1 -eq $2 ]&&{
echo "$1=$2"
exit 0
}
[ $1 -gt $2 ]&&{
echo "$1>$2"
exit 0
}
[ $1 -lt $2 ]&&{
echo "$1<$2"
exit 0
}
[root@shellbiancheng ~]# sh zhengshubijiao.sh 1  
Usage :zhengshubijiao.sh {num1 and num2}
[root@shellbiancheng ~]# sh zhengshubijiao.sh 1 2
1<2
[root@shellbiancheng ~]# sh zhengshubijiao.sh 1 0
1>0
[root@shellbiancheng ~]# sh zhengshubijiao.sh 1 1
1=1
[root@shellbiancheng ~]# sh zhengshubijiao.sh 1 e
first arg must be int
[root@shellbiancheng ~]# sh zhengshubijiao.sh e 1
first arg must be int

b.read读入的方式

[root@shellbiancheng ~]# cat zhengshubijiao1.sh 
#!/bin/sh
read -p "please input two num:" num1 num2
c="please input two num"
a=$num1;b=$num2
[ -z "$a" -o -z "$b" ]&&{
echo $"Usage :$c{num1 and num2}"
exit 1
}
[[ "`echo "$a"|sed -r 's#[^0-9]##g'`" = "$a" ]]||{
echo "first arg must be int"
exit 2
}
[[ "`echo "$b"|sed -r 's#[^0-9]##g'`" = "$b" ]]||{
echo "first arg must be int"
exit 2
}
[ $a -eq $b ]&&{
echo "$a=$b"
exit 0
}
[ $a -gt $b ]&&{
echo "$a>$b"
exit 0
}
[ $a -lt $b ]&&{
echo "$a<$b"
exit 0
}
[root@shellbiancheng ~]# sh zhengshubijiao1.sh
please input two num:1
Usage :please input two num{num1 and num2}
[root@shellbiancheng ~]# sh zhengshubijiao1.sh 
please input two num:1 1
1=1
[root@shellbiancheng ~]# sh zhengshubijiao1.sh 
please input two num:1 0
1>0
[root@shellbiancheng ~]# sh zhengshubijiao1.sh 
please input two num:1 2
1<2
[root@shellbiancheng ~]# sh zhengshubijiao1.sh 
please input two num:1 e
first arg must be int
[root@shellbiancheng ~]# sh zhengshubijiao1.sh 
please input two num:e 1
first arg must be int

定义变量就是将read读入中的a和b赋值,然后将read去掉这里我就不举例了。

(2)打印选择菜单,一键安装web服务

要求:

当用户输入1时,输出“start installing lamp”然后执行/server/scripts/lamp.sh,脚本内容输出“lamp is installed”后退出脚本;当用户输入2时,输出“start installing lnmp”然后执行/server/script/lnmp.sh,输出“lnmp is installed”后退出脚本;当输入3时退出当前菜单及脚本。当输入任何其他字符,给出提示“Input error”后退出脚本。要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行等。

打印菜单,实现web服务安装

[root@shellbiancheng ~]# cat webmenu.sh 
#!/bin/bash
export PATH="/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/curl/bin:/root/bin"
path="/server/scripts"
RETVAL=0
[ -d "$path" ]|| mkdir -p $path
menu(){
cat <<EOF
 1.[ install lamp ]
 2.[ install lnmp ]
 3.[ exit ]
please input the num you want:
EOF
}
menu

read num
function man(){
case "$num" in
  1)
   echo "start install lamp."
   sleep 2
   [ -x "$path/lamp.sh" ]||{
  echo "$path/lamp.sh does not exist or can not be exec."
  exit 1
}
   source $path/lamp.sh
   exit $?
   ;;
   2)
   echo "start install lnmp."
   sleep 2
   [ -x "$path/lnmp.sh" ]||{
  echo "$path/lamp.sh does not exist or can not be exec."
  exit 1
}
   source $path/lamp.sh
   exit $?
   ;;
   3)
   echo bye.
   exit 3
   ;;
   *)
   echo "you must input (1|2|3)"
   echo "input error"
   exit 4
   ;;
esac

}
expr $num + 1 &>/dev/null
if [ $? -ne $RETVAL ];then

echo "the num you input must be (1|2|3)."
exit 1
else
man
fi

转载于:https://blog.51cto.com/10642812/2084263

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值