DBA需要掌握的Shell知識

AlfredZhao發表於2015-05-07

每個中高階DBA都需要掌握一些簡單指令碼的編寫,這樣才能從繁雜重複的基礎維護工作中解脫出來,才能有時間去研究更有價值的技術。VBird在講shell script的時候,給出了幾個經典的小范例練習,對於初學shell的人來說是很好的入門,現就根據VBird給出的幾個典型練習進行近一步的系統整理,總結出bash shell的系統知識,希望能給各位讀者起到拋磚引玉的作用。

1.順序執行
2.分支判斷
3.迴圈結構
4.鞏固練習

1.順序執行

練習1:使用者選擇輸入Y/N,不區分大小寫,根據使用者輸入螢幕列印不同內容。

考查:read,[],exit 0,&&,echo

#!/bin/bash
#Usage: user input a charector, program shows the different result.
#Author: Alfred Zhao
#Creation: 2015-05-06
#Version: 1.0.0

#1.Input 'Y' or 'N'
read -p "Input (Y/N)" input 
[ "$input" == "Y" -o "$input" == "y" ] && echo -e "you choice is: $input\n" && exit 0
[ "$input" == "N" -o "$input" == "n" ] && echo -e "you choice is: $input\n" && exit 0
echo -e "I don't know what your choice is" && exit 0

2.分支判斷

兩種常用的分支判斷:if…else…fi分支判斷,case…esac分支判斷。

練習2:將練習1中的程式碼改寫為if分支判斷,使程式的執行邏輯更直觀。

考查:==,||
if[]; then

elif[]; then

else

fi

#!/bin/bash
#Usage: user input a charector, program shows the different result.
#Author: Alfred Zhao
#Creation: 2015-05-06
#Version: 1.0.1

#1.Input 'Y' or 'N'
read -p "Input (y/n)" input
if [ "$input" == "Y" ] || [ "$input" == "y" ]; then
	echo -e "you choice is: $input\n"
	exit 0
elif [ "$input" == "N" ] || [ "$input" == "n" ]; then
	echo -e "you choice is: $input\n"
	exit 0
else
	echo -e "I don't know what you choice is.\n"
	exit 0
fi

練習3:用分支判斷來辨別引數1的輸入是否合法。

考查:$0,$1

#!/bin/bash
#Usage: To judge $1's identity. Aha, Only Alfred is ok. 
#Author: Alfred Zhao
#Creation: 2015-05-07
#Version: 1.0.0

if [ "$1" == "Alfred" ]; then
	echo -e "Authorization Successful! \n"
	exit 0
elif [ "$1" == "" ]; then
	echo -e "Waring: Authorization is null! ex> {$0 Username}\n"
	exit 0
else
	echo -e "Waring: Only Alfred can be authorized. ex> {$0 Alfred}\n"
	exit 0
fi

練習4:用case判斷改寫練習3.

考查:case…esac判斷

#!/bin/bash
#Usage: To judge $1's identity. Aha, Only Alfred is ok. 
#Author: Alfred Zhao
#Creation: 2015-05-07
#Version: 1.0.1

case "$1" in
	"Alfred")
		echo -e "Authorization Successful! \n"
		;;
	"")
		echo -e "Waring: Authorization is null! ex> {$0 Username}\n"
		;;
	*)
		echo -e "Waring: Only Alfred can be authorized. ex> {$0 Alfred}\n"
		;;
esac

3.迴圈結構

while do done, until do done(不定迴圈)

練習5:輸入名字直到輸入的名字是“Alfred”為止。

考查:while do done

#!/bin/bash
#Usage: Input the name until it is "Alfred". 
#Author: Alfred Zhao
#Creation: 2015-05-07
#Version: 1.0.0

while [ "$name" != "Alfred" ]
do
        read -p "Please Input your name: " name
done
echo -e "\nWelcome, My friend, Alfred.\n"

而如果是使用until do done,
只需要修改while [ "$name" != "Alfred" ]until [ "$name" == "Alfred" ]

練習6:計算1+2+3+…+num的結果

考察:正則

#!/bin/bash
#Usage: Calculate the result "1+2+...+num". 
#Author: Alfred Zhao
#Creation: 2015-05-07
#Version: 1.0.0

i=0 #i
s=0 #sum

echo -e "This program will help you calculate the result of '1+2+...+num'\n"
read -p "Please input your num: " num

if [ "$(echo "$num"|grep '[0-9]'|grep -v '[:alpha:]')" == "" ]; then
        echo -e "Waring: Please input a number.\n"
        exit 1
elif [ "$num" -lt "1" ]; then
        echo -e "Waring: Not support.\n"
elif [ "$num" == "1" ]; then
        echo -e "1=1\n"
        exit 0
elif [ "$num" == "2" ]; then
        echo -e "1+2=3\n"
        exit 0
elif [ "$num" == "3" ]; then
        echo -e "1+2+3=6\n"
        exit 0
else
        while [ "$i" != "$num" ]
        do
                i=$(($i+1))
                s=$(($s+$i))
        done

        echo -e "\n1+2+...+$num= $s\n"
        exit 0
fi

for do done(固定迴圈)

for do done 第一種用法示例:

練習7:迴圈輸出變數who的內容

#!/bin/bash
#Usage: for do done 
#Author: Alfred Zhao
#Creation: 2015-05-07
#Version: 1.0.0

for who in mum dad brother sister 
do
        echo -e "This is my ${who}.\n"
done

for do done 第二種用法示例:

練習8:計算1+2+..+100的值

#!/bin/bash
#Usage: 1+2+...+100
#Author: Alfred Zhao
#Creation: 2015-05-07
#Version: 1.0.0
sum=0
for ((i=1; i<=100; i=i+1))
do
        sum=$(($sum+$i))
done

echo -e "The result is $sum.\n"

4.鞏固練習

1.用分支判斷哪些資料庫預設埠在執行.

提示:不同資料庫的預設監聽埠不同
Oracle資料庫判斷netstat -tuln |grep ":1521 "是否有結果;
Mysql資料庫判斷netstat -tuln |grep ":3306 "是否有結果;
IEE資料庫判斷netstat -tuln |grep ":5029 "是否有結果;
Vertica資料庫判斷netstat -tuln |grep ":5433 "是否有結果.

2.輸入畢業日期,計算當前離畢業還有多少天。

提示:將時間換算成秒,相減後換算成天數。
day1=$(date --date="20150507" +%s)
day2=$(date --date="20160630" +%s)
days=$((($day2-$day1)/3600/24))

3.檢查Linux系統所有使用者的識別符號與特殊引數

提示:cut -d ':' -f1 /etc/passwd

4.檢查192.168.1.1~192.168.1.100的主機網路情況

提示:for site in $(seq 1 100)

相關文章