哈哈哈哈哈哈
这篇文章不错!
这篇文章不错!
作者对主题的挖掘深入骨髓,展现了非凡的洞察力和理解力。
情感真挚自然,字里行间传递出强烈的感染力。
作者的情感表达细腻入微,让人在阅读中找到了心灵的慰藉。
内容的丰富性和深度让人仿佛置身于知识的海洋,受益匪浅。
# 图片回复
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/93624.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/12554.html
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/82001.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/83913.html
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/82001.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/83913.html
首页
网站统计
关于本站
在线留言
友链申请
高清壁纸
论坛
开往
虫洞
推荐
Linux命令
资源网
Search
1
Typecho Cuteen主题美化
5,129 阅读
2
京东呆瓜搭建青龙面板+xdd-plus机器人+nvjdc配置
4,597 阅读
3
好久不见之网站底部样式
4,237 阅读
4
傻妞机器人最新版安装教程
4,108 阅读
5
Joe 主题 6.xx 底部增强,显示标签及二维码分享
3,114 阅读
Linux
Shell
Mysql
Typecho
网络
其他
Redis
登录
Search
标签搜索
Linux
Typecho
美化
Nginx
Shell
综合架构
Mysql
Joe
源码
Web
数据备份
命令
Ansible
k8s
定时任务
视频
网易云
白嫖
网络
Rsync
小黑
累计撰写
155
篇文章
累计收到
921
条评论
博主
4月14日
在线
首页
栏目
Linux
Shell
Mysql
Typecho
网络
其他
Redis
页面
网站统计
关于本站
在线留言
友链申请
高清壁纸
推荐
Linux命令
资源网
开往
搜索到
1
篇与
算术运算符
的结果
2021-04-03
Shell:变量数值计算(上)
算数运算符{mtitle title="Shell常见的算数运算符"/}算数运算符意义+、-加法(正号)、减法(负号)*、/、%乘、除、取余**幂运算++、--增加及减少!、&&、双竖线逻辑非(取反)、逻辑与、逻辑或<、<=、>、=>比较符号(小于、小于等于、大于、大于等于)==、!=、=比较符号(等于、不相等、对于字符串“=”也可以表示等于)<<、>>向左移,向右移~、单竖线、&、^按位取反、按位异或、按位与、按位或=、+=、-=、*=、/=、%=赋值运算符{mtitle title="Shell中常见的算数运算命令"/}运算符与运算命令意义(())用于整数运算的常用命令let用于整数运算,类似于(())expr可用于整数运算bcLinux中的一个计算器程序(适合整数和小数运算)$[]用于整数运算awkawk既可以整数运算,也可以小数运算declare定义变量值和属性,-i参数可以用于定义整形变量,做运算(())运算用法root@cs:/server/scripts# echo $((8+2)) 10 root@cs:/server/scripts# echo $((8-2)) 6 root@cs:/server/scripts# echo $((8*2)) 16 root@cs:/server/scripts# echo $((8/2)) 4 root@cs:/server/scripts# echo $((8%2)) 0 root@cs:/server/scripts# echo $((8**2)) 64 root@cs:/server/scripts# ((i=6)) root@cs:/server/scripts# ((i=i*2)) root@cs:/server/scripts# echo $i 12 root@cs:/server/scripts# a=$((100*(100+1)/2)) root@cs:/server/scripts# echo $a 5050 root@cs:/server/scripts# echo $((100*(100+1)/2)) 5050 root@cs:/server/scripts# echo $((3<8)) 1 #1表示为真 root@cs:/server/scripts# echo $((1>8)) 0 #0表示为假{message type="warning"}PS:上面的数字及变量必须为整数,不能用小数和字符串{/message}(())运算的Shell脚本示例root@cs:/server/scripts# cat test.sh #!/bin/bash a=6 b=12 echo "a+b=$(($a+$b))" echo "a-b=$(($a-$b))" echo "a*b=$(($a*$b))" echo "a/b=$(($a/$b))" echo "a**b=$(($a**$b))" echo "a%b=$(($a%$b))" echo "a++:$((a++))" echo "a=$a" echo "++a:$((++a))" echo "a=$a" echo "b--:$((--b))" echo "b=$b" echo "--b:$((--b))" echo "b=$b"其执行结果root@cs:/server/scripts# sh test.sh a+b=18 a-b=-6 a*b=72 a/b=0 a**b=2176782336 a%b=6 a++:6 a=7 ++a:8 a=8 b--:11 b=11 --b:10 b=10通过命令行传参方式,实现混合运算root@cs:/server/scripts# cat test1.sh #!/bin/bash a=$1 b=$2 echo "a+b=$(($a+$b))" echo "a-b=$(($a-$b))" echo "a*b=$(($a*$b))" echo "a/b=$(($a/$b))" echo "a**b=$(($a**$b))" echo "a%b=$(($a%$b))" echo "a++:$((a++))" echo "a=$a" echo "++a:$((++a))" echo "a=$a" echo "b--:$((--b))" echo "b=$b" echo "--b:$((--b))" echo "b=$b"其执行结果root@cs:/server/scripts# sh test1.sh 8 4 a+b=12 a-b=4 a*b=32 a/b=2 a**b=4096 a%b=0 a++:8 a=9 ++a:10 a=10 b--:3 b=3 --b:2 b=2实现输入两个数进行加减乘除的计算器利用read命令读入功能root@cs:/server/scripts# cat jsq.sh #!/bin/bash print_usage(){ print "please enter an integer\n" exit 1 } read -p "Please input first number:" a if [ -n "`echo $a|set 's/[0-9]//g'`" ];then print_usage fi read -p "Please input first operators:" b if [ "${b}" != "+" ]&&[ "${b}" != "-" ]&&[ "${b}" != "*" ]&&[ "${b}" != "/" ];then print_usage exit 2 fi read -p "Please input first number:" c if [ -n "`echo $c|set 's/[0-9]//g'`" ];then print_usage fi echo "${a}${b}${c}=$((${a}${b}${c}))" 其输入结果为 root@cs:/server/scripts# sh jsq.sh Please input first number:6 Please input first operators:+ Please input first number:4 6+4=10 root@cs:/server/scripts# sh jsq.sh Please input first number:8 Please input first operators:- Please input first number:4 8-4=4利用命令行传参方式root@cs:/server/scripts# sh jsq1.sh 8 + 5 8+5=13 root@cs:/server/scripts# sh jsq1.sh 8 - 5 8-5=3 root@cs:/server/scripts# sh jsq1.sh 8 \* 5 #*号要转义 8*5=40 root@cs:/server/scripts# sh jsq1.sh 8 / 5 8/5=1let 运算用法root@cs:/server/scripts# i=8 root@cs:/server/scripts# i=i+8 root@cs:/server/scripts# echo $i i+8 root@cs:/server/scripts# unset i root@cs:/server/scripts# i=8 root@cs:/server/scripts# let i=i+8 root@cs:/server/scripts# echo $i 16提示:let i=i+8等同于 ((i=i+8)),不过后者效率高一些{mtitle title="示例"/}root@cs:/server/scripts# cat 04_03_test.sh #!/bin/bash CheckUrl(){ #定义一个函数 timeout=5 #定义wget访问的超出时间 fails=0 #初始化访问网站失败的次数记录变量,若失败达到两次,报警 success=0 #初始化访问网站成功的次数记录变量,如果为1 则表示成功,退出 while true #持续循环检测 do wget --timeout=$timeout --tries=1 https://www.xiaobai666.top -q -O /dev/null #wget访问测试 if [ $? -ne 0 ] #如果上述wget不成功,即返回值不为零,执行if语句 then let fails=fails+1 #失败加1 else let success+=1 #成功加1 fi if [ $success -ge 1 ] #如果成功次数大于等于1 then echo success exit 0 fi if [ $fails -ge 2 ] #如果失败的次数大于等于2 then Critical="sys is down." echo "$Critical|tee|mail -s $Critical" #3285884651@qq.com exit 2 fi done } CheckUrl执行结果root@cs:/server/scripts# sh 04_03_test.sh success root@cs:/server/scripts# sh -x 04_03_test.sh + CheckUrl + timeout=5 + fails=0 + success=0 + true + wget --timeout=5 --tries=1 https://www.xiaobai666.top -q -O /dev/null + '[' 0 -ne 0 ']' + let success+=1 + '[' 1 -ge 1 ']' + echo success success + exit 0expr命令用法root@cs:/server/scripts# expr 2 + 2 4 root@cs:/server/scripts# expr 2 - 2 0 root@cs:/server/scripts# expr 2 \* 2 4 root@cs:/server/scripts# expr 2 / 2 1expr配合变量计算root@cs:/server/scripts# i=5 root@cs:/server/scripts# i=$(expr $i + 6) root@cs:/server/scripts# echo $i 11 root@cs:/server/scripts# i=`expr $i + 6` root@cs:/server/scripts# echo $i 17{message type="warning"}PS:expr $i +6=$(expr $i + 6),不能用小数和字符串{/message}通过expr判断变量或者数字是否为整数root@cs:/server/scripts# i=8 root@cs:/server/scripts# expr $i + 6 &>/dev/null root@cs:/server/scripts# echo $? 0 root@cs:/server/scripts# i=skfhs root@cs:/server/scripts# expr $i + 6 &>/dev/null root@cs:/server/scripts# echo $? 2也可以通过echo $? 判断上个命令是否运行成功通过read 读入持续等待例子cat expr.sh #!/bin/bash while true do read -p "Please input:" num expr $num + 0 >/dev/null 2>&1 [ $? -eq 0 ]&& echo int || echo chars done 执行结果 root@cs:/server/scripts# sh expr.sh Please input:123 int Please input:ersfs chars Please input:hf chars Please input:456456 int将前文的混合运算,判断传参的个数及通过expr判断传入的参数是否为整数root@cs:/server/scripts# cat test2-2.sh #!=/bin/bash print_usage(){ echo "please input two number:" exit 1 } [ $# -ne 2 ] && { echo $"USAGE $0 NUM1 NUM2" exit 1 } a=$1 b=$2 expr $a + 1 >/dev/null RETVAL_A=$? expr $b + 1 >/dev/null RETVAL_B=$? if [ $RETVAL_A -ne 0 -o $RETVAL_B -ne 0 ] then echo "Please input two num" exit 2 fi echo "a+b=$(($a+$b))" echo "a-b=$(($a-$b))" echo "a*b=$(($a*$b))" echo "a/b=$(($a/$b))" echo "a**b=$(($a**$b))" echo "a%b=$(($a%$b))" echo "a++:$((a++))" echo "a=$a" echo "++a:$((++a))" echo "a=$a" echo "b--:$((--b))" echo "b=$b" echo "--b:$((--b))" echo "b=$b"执行结果root@cs:/server/scripts# sh test2-2.sh 6 3 a+b=9 a-b=3 a*b=18 a/b=2 a**b=216 a%b=0 a++:6 a=7 ++a:8 a=8 b--:2 b=2 --b:1 b=1通过espr计算字符串的长度root@cs:/server/scripts# char="are you ok" root@cs:/server/scripts# expr length "$char" 10 root@cs:/server/scripts# echo ${#char} 10 root@cs:/server/scripts# echo ${char}|awk '{print length($0)}' 10 root@cs:/server/scripts# echo ${char}|wc -L 10找出字符数不大于5的单词root@cs:/server/scripts# cat test2-3.sh #!/bin/bash for i in dhf gsh df adfs asdjkja do if [ `expr length $i` -le 5 ] then echo $i fi done执行结果root@cs:/server/scripts# sh test2-3.sh dhf gsh df adfs
2021年04月03日
492 阅读
0 评论
0 点赞