推薦掌握Linux shell中這7種運算命令

 

#常見的算術運算符號

1.+、-:加減
2.*、/、%:乘、除、取餘
3.**:冪運算
4.++、--:增加記減少
5.!、&&、||:取反,並且,或
6.<,<=,>,=> :比較符號:小於,小於等於,大於,大於等於
7.==、!=、=:比較符號:相等,不相等
8.<<,>> :向左移,向右移
9.+=、-=:賦值運算,+=相當於i=i+1,-+相當於i=i-1

 

#常見的的算數運算命令

1.(()):用於整數運算的常用運算符
2.let:用於整數運算
3.expr:也可用於整數運算,還有其他額外功能
4.bc:計算機程序,適用整數記小數運算
5.$[]:用於整數運算
6.awk:可以用於整數運算,也可用於小數運算
7.declare:定義變量值和屬性

 

#1.(())雙小括號

(())雙小括號作用是進行數值運算和數字比較

 

#常見的操作

((a=i+1))
i=$((i+1))
echo $((1+4))

 

#應用

#使用(())進行數值計算
[root@game ~]# echo $((10+5)) #進行相加
15
[root@game ~]# echo $((10-5)) #進行相減
5
[root@game ~]# echo $((10*5)) #相乘
50
[root@game ~]# echo $((10/5)) #相除
2
[root@game ~]# echo $((10**5)) #次冪
100000
[root@game ~]# echo $((10%5)) #取餘
0

[root@game ~]# ((i=10))
[root@game ~]# ((i=i*5))
[root@game ~]# echo $i #適用echo輸出
50

[root@game ~]# ((n=10+2-5*2/4+5))
[root@game ~]# echo $n
15
[root@game ~]# echo $((100*(100+1)/2)) #計算1+2+3..+100的和
5050

 

#使用(())進行比較判斷

[root@game ~]# echo $((4<9)) #如果結果爲真就輸出1
1
[root@game ~]# echo $((4>9)) #如果結果爲假就輸出0
0
[root@game ~]# echo $((4==4))
1
[root@game ~]# if ((8>7&&6==6));then echo yes;fi #使用if語句進行判斷條件是否成立
yes

 

#(())運算後賦值給變量

[root@game ~]# mytest=66
[root@game ~]# echo $((mytest+34))
100
[root@game ~]# mytest=$((mytest+34))
[root@game ~]# echo $mytest
100

 

#將命令寫進腳本執行

[root@game test]# cat test1.sh 
#!/bin/bash
a=6
b=3
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))"

#效果
[root@game test]# sh test1.sh 
a-b=3
a+b=9
a*b=18
a/b=2
a%b=0

 

#將腳本中定義的變量通過傳參的方式來實現

[root@game test]# 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))"

#效果
[root@game test]# sh test1.sh 8 4
a-b=4
a+b=12
a*b=32
a/b=2
a%b=0

 

#2.let命令

#語法格式:let 賦值表達式

#示例

[root@game ~]# let n=10+3
[root@game ~]# echo $n
13
[root@game ~]# let a=20/4
[root@game ~]# echo $a
5

 

#實踐

#判斷如果登錄失敗次數等於3次,那麼就打印登錄失敗,退出腳本
[root@game test]# cat login.sh 
#!/bin/bash

fail=0

while ((1==1))
do
    read -p "please enter you username: " user
    read -p "please enter you password: " password
    if [[ $user = 'guoke' && $password = 'guoke123' ]];then
        echo "login success"
        exit 1
    else
        if [ $? -ne 0 ];then
            let fail=fail+1 #如果登錄失敗次數就會加1
            if [ $fail -ge 3 ];then #當失敗次數等於3就打印登錄失敗
                echo "login fail"
                exit 1
            fi
        fi
    fi
done

#效果
[root@game test]# sh login.sh 
please enter you username: 1
please enter you password: 1
please enter you username: 2
please enter you password: 2
please enter you username: 3
please enter you password: 3
login fail

[root@game test]# sh login.sh 
please enter you username: guoke   
please enter you password: guoke123
login success

 

#3.expr命令

#介紹

expr - evaluate expressions
expr - (evaluate(求值) expressions(表達式),可以用於整數運算,也可用於相關字符串、匹配的運算處理

#查看更多幫助
man expr

 

#示例1:用於基本的計算

[root@game ~]# expr 10 + 10 #相加
20
[root@game ~]# expr 10 - 10 #相減
0
[root@game ~]# expr 10 * 10 #相乘,相乘的時候需要使用\轉移,不然報錯
expr: syntax error
[root@game ~]# expr 10 \* 10
100
[root@game ~]# expr 10 / 10 #相除
1
#提示:expr後面的計算數字需要有一個空格

 

#示例2:配合變量進行計算,需要使用反引號將計算表達式括起來

[root@game ~]# n=10
[root@game ~]# n=`expr $n + 100`
[root@game ~]# echo $n
110

 

#案例

#1.判斷一個變量值或字符串是否爲整數

#將一個變量或字符串和一個非0的整數相加,查看返回值是否爲0,如果爲0,說明爲整數,否則就不是整數
[root@game ~]# a=10
[root@game ~]# expr $a + 10 &>/dev/null
[root@game ~]# echo $?
0 #返回0,證明a的值爲整數

[root@game ~]# n=guoke
[root@game ~]# expr $n + 10 &>/dev/null
[root@game ~]# echo $?
2 #返回非0,整數n的值不是整數

 

#4.bc命令

#介紹

bc是Linux下的計算器,還可以作爲命令行計算工具使用

#查看幫助man bc
#語法:
bc [ -hlwsqv ] [long-options] [  file ... ]

#提示:bc命令需要進行安裝
yum install bc

 

#示例1:作爲計算器來使用

#直接在命令輸入bc,然後進行相關的加減乘除操作
[root@game ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

10+200
210
2*2
4
333-22
311
^C
(interrupt) Exiting bc.

 

#示例2:在命令行下面實現運算功能

[root@game ~]# echo 333+333|bc
666
[root@game ~]# echo 3.33+3.33|bc
6.66
[root@game ~]# echo 33*33|bc
1089
[root@game ~]# echo "scale=3;333/45"|bc
7.400
[root@game ~]# echo "scale=2;333/45"|bc
7.40
提示:根據bc所具有的特殊性來看,如果是小數,則選擇bc運算沒有問題,若是整數場景,可用”(())“、let、expr等

 

#5.$[]命令

#使用$[]來計算運算

#應用

[root@game ~]# i=10
[root@game ~]# i=$[i+23]
[root@game ~]# echo $i
33
[root@game ~]# echo $[100+100]
200
[root@game ~]# echo $[100-100]
0
[root@game ~]# echo $[100*100]
10000
[root@game ~]# echo $[100/100]
1

 

#6.declare命令

#使用declare來運算,同typeset

#示例1:定義整數變量,進行計算

[root@game ~]# declare -i a=10 b=10
[root@game ~]# a=a+b
[root@game ~]# echo $a
20

 

#示例2:也可以作爲變量輸出

[root@game ~]# DIR_TEST=/tmp/test
[root@game ~]# declare |grep TEST
DIR_TEST=/tmp/test

 

#7.awk命令

awk是一種編程語言,可以用來處理數據和生成報告,也可以實現計算,適合小數和整數運算

#應用

#進行相加
[root@game ~]# echo "3.3 2.3" | awk '{print ($1+$2)}'
5.6 
#進行相減
[root@game ~]# echo "3.3 2.3" | awk '{print ($1-$2)}'
1
#相乘
[root@game ~]# echo "3.3 2.3" | awk '{print ($1*$2)}'
7.59
#先使用743-100再相除
[root@game ~]# echo "743 234" | awk '{print ($1-100)/$2}'
2.74786
#先使用743-100再相乘
[root@game ~]# echo "743 234" | awk '{print ($1-100)*$2}'
150462

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章