首页 学习笔记 📖 / 未收录,推送中

请注意,本文最后更新时间:2021-04-06,最后编辑于79天前,内容可能已经不具有时效性,请谨慎参考。

算数运算符

{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=1

let 运算用法

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 0

expr命令用法

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
1

expr配合变量计算

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

您阅读这篇文章共花了:



文章评论

评论已关闭