加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell:数值运算、条件测试、if判断

发布时间:2020-12-15 09:12:06 所属栏目:安全 来源:网络整理
导读:bash-completion包:支持tab补齐 数值运算:整数运算、小数运算 一、整数运算 1、expr expr A + B,A和B之间要空格,否则直接显示A+B expr A - B expr A * B #乘法*需要采用转义*,避免被作为shell通配符 expr A / B :只能屏蔽一个字符e.g: [root@shell ~]# a

bash-completion包:支持tab补齐

数值运算:整数运算、小数运算 一、整数运算 1、expr expr A + B,A和B之间要空格,否则直接显示A+B expr A - B expr A * B #乘法*需要采用转义*,避免被作为shell通配符 expr A / B :只能屏蔽一个字符e.g: [root@shell ~]# a=24 [root@shell ~]# echo $a $a [root@shell ~]# echo $a 24 [root@shell ~]# echo $a$b $a [root@shell ~]# echo $a$b $a$b

2、$[]或$(()) 举例: [root@shell ~]# echo $[10*3] 30 [root@shell ~]# echo $[10/3] 3 [root@shell ~]# echo $[10+3] 13 [root@shell ~]# echo $[10-3] 7 [root@shell ~]# echo $[10%3] 1 [root@shell ~]# x=3 [root@shell ~]# y=5 [root@shell ~]# echo $[x+y] 8

可以一行进行多个运算 [root@shell ~]# x=8 [root@shell ~]# echo $[x+8],$[x*8] [root@shell ~]# 16,64

3、let运算 不回显结果 [root@shell ~]# x=9 [root@shell ~]# let x=x+8 [root@shell ~]# echo $x 17

4、特殊用法: i=n let i++ ---->i=n+1 let i-- ---->i=n-1 let i+=3 --->i=n+3 let i-=2 --->i=n-2 let i*=2 --->i=n2 let i/=2 --->i=n/2 举例: [root@shell ~]# i=24 [root@shell ~]# let i++ [root@shell ~]# echo $i 25 [root@shell ~]# let i+=3 [root@shell ~]# echo $i 28 [root@shell ~]# let i-=1 [root@shell ~]# echo $i 27 [root@shell ~]# let i=2 [root@shell ~]# echo $i 54 [root@shell ~]# let i/=2 [root@shell ~]# echo $i 27

二、小数运算 scale=n 控制小数点位数为n位 举例: [root@shell ~]# bc scale=5 #取小数点后保留5位 24/5.2555 #24除以5.2555 4.56659 #结果为4.56659

bc可做数值大小比较>、<、==、>=、<=、!=,真为1,假为0 举例: [root@shell ~]# echo "1>2" | bc [root@shell ~]# 0 [root@shell ~]# echo "2>1" | bc [root@shell ~]# 1 [root@shell ~]# A=12.5;B=24.8 [root@shell ~]# echo "$A>$B" | bc 0

bc非交互式运算 [root@shell ~]# echo 'scale=4;12.34*56.789;5/3' | bc #一行可以有多个命令,用分号;隔开! 700.7762 1.666

三、条件测试操作 1、字符串比较:==(判断两个字符串是否相当)、-z(字符串的值是否为空),-n(字符串的值是否不为空,相当于! -z) 举例: 判断当前用户是否为root 当root用户执行时: [root@shell ~]# [ $USER == "root" ] [root@shell ~]# echo $? [root@shell ~]# 0 当普通用户执行时: [kobe@shell ~]$ [ $USER == "root" ] [kobe@shell ~]$ echo $? [kobe@shell ~]$ 1

且&&,或|| 一行中有多个命令,A命令和B命令 A;B #执行A,执行B A&&B #执行A,仅当A成功才执行B A||B #执行A,仅当A失败才执行B 举例: [ a == a ] && echo "yes" || echo "no" yes [ a == b ] && echo "yes" || echo "no" no

[root@shell ~]# [ a == a ]
[root@shell ~]# echo $? [root@shell ~]# 0 [root@shell ~]# [ -z $A ] #判断A变量是否为空 [root@shell ~]# echo $? #查看0为真,非0为假

[root@shell ~]# a="abc" ; b="" #定义变量a的值为abc,b的值为空,注意:""里如果有空格也算不为空 [root@shell ~]# [ -z "$a" ] && echo "空值" || echo "非空值" 非空值 [root@shell ~]# [ -z $b ] && echo "空值" || echo "非空值" 空值

2、整数比较(不支持小数比较) -eq(等于)、-ne(不相等)、-gt(大于)、-ge(大于等于)、-lt(小于)、-le(小于等于) 举例: [root@shell ~]# x=20 [root@shell ~]# [ $x -eq 20 ] && echo "相等" || echo "不相等" 相等 [root@shell ~]# [ $x -eq 30 ] && echo "相等" || echo "不相等" 不相等

提取当前登录此虚拟机的用户数,比较是否超过4 [root@shell ~]# who | wc -l 3 #统计用户数为3 [root@shell ~]# N=$(who | wc -l) #将统计的值赋给N [root@shell ~]# [ $N --gt 4 ] && echo "超过了" || echo "没超过" 没超过

也可简化为: [root@shell ~]# [ $(who | wc -l) -gt 4 ] && echo "超过了" || echo "没超过"

3、文件、目录判断 -e #判断是否存在(不管是文件还是目录) -f #判断是否存在文件 -d #判断是否存在目录 -r #判断是否有r权限 -w #判断是否有w权限 -x #判断是否有x权限

举例: [root@shell ~]# [ -e "/usr/src" ] && echo 存在 || echo 不存在 存在

判断对象是否为目录 -d,是否不为目录 ! -d [root@shell ~]# [ -d "/usr/src/" ] && echo "是目录" || echo "不是目录" 是目录 [root@shell ~]# [ -d "/etc/fstab" ] && echo "是目录" || echo "不是目录" 不是目录 [root@shell ~]# [ -d "/home/nooby" ] && echo "是目录" || echo "不是目录" 不是目录

判断对象是否有-r、-w、-x权限 注意:r和w对root用户判断无效,无论是否设置权限,root都可读或写 举例: [root@shell ~]# cp install.log /tmp/t.txt #复制一个文件做测试 [root@shell ~]# chmod -r /tmp/t.txt #去掉所有的r权限 [root@shell ~]# [ -r "/tmp/t.txt" ] && echo "可读" || echo "不可读" #root仍然可读 可读

切换为普通用户时: [kobe@shell ~]$ [ -r "/tmp/t.txt" ] && echo "可读" || echo "不可读" 不可读

普通用户只对自己拥有r权限的对象,-r测试才成立 [kobe@shell ~]$ ls -l .bashrc -rw-r--r-- 1 kobe kobe 124 09-24 16:44 .bashrc [kobe@shell ~]$ [ -r ".bashrc" ] && echo "可读" || echo "不可读" 可读

-x权限:取决于文件本身,root或普通用户都适用 [root@shell ~]# chmod 644 /tmp/t.txt #设置无x权限 [root@shell ~]# ls -l /tmp/t.txt -rw-r--r-- 1 root root 33139 12-11 10:52 /tmp/t.txt [root@shell ~]# [ -x "/tmp/t.txt" ] && echo "可执行" || echo "不可执行" 不可执行 [root@shell ~]# chmod +x /tmp/t.txt #添加x权限 [root@shell ~]# [ -x "/tmp/t.txt" ] && echo "可执行" || echo "不可执行" 可执行

条件组合应用: 当前用户为root,且位于/root目录下 [root@shell ~]# [ $USER == "root" ]&&[ $PWD == "/root"] [root@shell ~]# [ $? -eq 0 ]&& echo "YES" YES

[root@shell ~]# [ ! -d "/xx" ]&& mkdir /xx #判断是否没有目录/xx,是就创建
[root@shell ~]# mount /dev/cdrom /xx #挂载 或 [root@shell ~]# [ -d "/xx" ]|| mkdir /xx #判断是否有目录/xx,没有就创建 [root@shell ~]# mount /dev/cdrom /xx 或 [root@shell ~]# [ -d "/xx" ]&& mount /dev/cdrom /xx


每分钟检测当前计算机登录的用户数量 若数量大于2,则发送邮件报警 先写好脚本: #!/bin/bash user=$(who | wc -l) [ $user -ge 2 ] && mail -s "登陆用户过多" root < /root/用户报警.txt

crontab -e -u root */1 * * * * bash 脚本名


if判断语句

1、单分支if(仅判断对) if [ 判断 ];then 命令 fi

if [ 判断 ];then 成功 else 失败 fi

举例: 脚本: #!/bin/bash read -p '请输入您的IP:' ip ping -c2 $ip >&/dev/null #-c2是只ping2个包,-i0.1(缩短发送每个包的间隔时间为0.1秒),-W2(等待反馈是否ping通的超时时间2秒) if [ $? -eq 0 ];then echo 通了! else echo 不通! fi

2、多分支if if [ 判断1 ];then 命令 elif [ 判断2 ];then 命令 ... ... else 命令 fi

举例: $RANDOM(随机数) $[RANDOM%10+1] 求余(因为余数不可能大于除数)设定随机数的范围1-10,不能用%11,因为有可能全部除尽余数为0 #!/bin/bash x=$[RANDOM%10+1] read -p "我有个1-10的随机数,你猜:" cai if [ $cai -eq $x ];then echo 恭喜你,猜对了! exit #必须要有!不然猜对了还会继续猜下去 elif [ $cai -gt $x ];then echo 太大了 else echo 太小了 fi 增加概率可以从read开始到最后多复制几遍

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读