1. 程式人生 > >Linux運維之shell指令碼基礎知識

Linux運維之shell指令碼基礎知識

1、bash中的算術運算

 

  • let運算子
[[email protected]:vg_adn_tidbCkhsTest~/tidb-bench/sysbench]#let i=1+2
[[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#echo $i
3
[[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#let i1=10
[[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench
]#let i2=20 [[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#let i3=$i1+$i2 [[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#echo $i3 30
  • $[expression]
[[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#i1=$[11+22]
[[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench
]#echo $i1 33
  • $((experssion))
[[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#i=$((22+33))
[[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#echo $i
55
  • expr  arg1  操作符  arg2
[[email protected]:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench
]#i=`expr 11 \* 10`;echo $i 110

     一般情況下使用expr運算子都要用反引號

2、判斷某個使用者是否存在,如果不存在,則建立此使用者

#!/bin/bash
id hadop &> /dev/null || useradd hadoop

3、計算passwd檔案中第10個使用者和第20個使用者的UID之和

#!/bin/bash
uid1=$(id -u `cat /etc/passwd |head | tail -1|cut -d':' -f1`)
uid2=$(id -u `cat /etc/passwd |head -20| tail -1|cut -d':' -f1`)
sum=$[$uid1+$uid2]
echo "10 and 20 user 's uuid is $sum"

4、統計某個檔案的空白行

[[email protected]:vg_adn_tidbCkhsTest /tmp]#cat /etc/init.d/functions | grep '^$'| wc -l
90

5、判斷返回值狀態

[[email protected]:vg_adn_tidbCkhsTest /tmp]#cat /etc/fstab &> /dev/null
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[[email protected]:vg_adn_tidbCkhsTest /tmp]#cat /etc/fstabhaha &> /dev/null             #檢視一個不存在的檔案
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1

6、條件測試

判斷某需求是否滿足,需要由測試機制來實現:

  如何編寫測試表達式以實現所需的測試

  • (1)執行命令,並利用命令狀態返回值來判斷

    0:成功

    1:失敗

  • (2)測試表達式

    test EXPRESSION

    [ EXPRESSION ]  單中括號多見於數值比較

    [[ EXPRESSION ]]   雙中括號多見於字串比較

      注意:EXPRESSION兩段必須有空白字元,否則為語法錯誤

bash的測試型別

數值測試:

-eq:是否等於

-ne:是否不等於

-gt:是否大於

-ge:是否大於等於

-lt:是否小於

-le:是否小於等於

[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ 11 -eq 21 ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ 11 -eq 11 ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0

字串測試

==:是否等於

>:是否大於

<:是否小於

!=:是否不等於

=~:左側字串能否被右側的PATTERN所匹配。右側的PATTERN不能使用bash的萬用字元來進行。並且PATTERN是匹配左側字串的某一部分而已

-z "STRING":判斷指定的字串是否為空,空則為真,不空則為假

-n "STRING":判斷指定的字串是否不空,不空則真,空則為假。

[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ tom == tom ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ tom != tom ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ "tom" != "join" ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0

並不是說字串比較這樣子就完美了,如下所示:

[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ "a" > "b" ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ "a" < "b" ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[[ "a" > "b" ]]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[[ "a" < "b" ]]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0

所以說字串的比較有兩點要注意:

1、字串要加引用

2、要使用雙中括號[[ EXPRESSION ]]來進行比較

[[email protected]:vg_adn_tidbCkhsTest /tmp]#name=tom[[email protected]:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "o" ]]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "o.*" ]]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "o*" ]]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "om" ]]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "o?" ]]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1

7、檔案測試

檔案存在性測試:

-e:FILE    檔案的存在性測試,存在則為真,不存在則為假

檔案型別測試:

-b FILE:檔案是否為塊裝置檔案

-c FILE: 檔案是否為字元裝置檔案

-d FILE:檔案是否是目錄檔案

-f FILE:檔案是否是普通檔案

-l FILE:檔案是否為連結檔案

-S FILE:檔案是否為套接字檔案

-p FILE:檔案是否為管道檔案

[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ -b /dev/xvda1 ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ -f /etc/init.d/functions ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0

檔案許可權性測試

-r  FILE:檔案是否可讀

-w FILE:檔案是否可寫

-x FILE:檔案是否可執行

[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ -x /usr/bin/passwd ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0

檔案特殊許可權測試

-g FILE:檔案是否擁有SGID許可權

-u FILE:檔案是否擁有SUID許可權

-k FILE:檔案是否擁有sticky許可權

[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ -u /usr/bin/passwd ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0

檔案大小測試

-s FILE :檔案是否為非空內容,有內容則為真,無內容則為假

[[email protected]:vg_adn_tidbCkhsTest /tmp]#touch ./haha.sh
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ -s haha.sh ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo "hahahaha" > haha.sh
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ -s haha.sh ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0

從屬關係測試

-O FILE:當前有效使用者是否為檔案屬主

-G FILE:當前有效使用者是否為檔案屬組

[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[[email protected]:vg_adn_tidbCkhsTest /tmp]#[ -O /home/tidb ]
[[email protected]:vg_adn_tidbCkhsTest /tmp]#echo $?
1

組合條件運算

[ condition1 -a condition2 ]    相當於  condition1  &&  condition2

[ condition1 -o condition2 ]    相當於  condition1  ||  condition2

[ -not condition ]  相當於  !condition

練習題:判斷主機名是否為空或主機名是“localhost.localdomain”

#!/bin/bash
hostName=$(hostname)
[ -z "$hostName" ] || [[ $hostName == "localhost.localdomain" ]]           #這一句相當於:      [ -n "$hostName" -o $hostName == "localhost.localdomain" ]
if [ $? -eq 0 ];then
        hostname www.feng.com
fi

指令碼的狀態返回值

預設是指令碼中執行的最後一條命令的狀態返回值

自定義狀態退出狀態碼:

exit [n]:n為自己指定的狀態碼。

注意L:shell程序遇到exit時,即會終止,因此,整個指令碼執行即為結束。