當前位置:首頁 » 編程軟體 » shell腳本的練習與使用

shell腳本的練習與使用

發布時間: 2023-02-07 00:09:21

1. shell腳本

| 對於初學者而言,因為沒有實戰經驗,寫不出來 Shell 腳本 很正常,如果工作了幾年的運維老年還是寫不出來,那就是沒主動找需求,缺乏練習,缺乏經驗。針對以上問題,總結了30個生產環境中經典的 Shell 腳本 ,通過這些需求案例,希望能幫助大家提升Shell編寫思路,掌握編寫技巧。 |

先了解下編寫Shell過程中注意事項:

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">開頭加解釋器:#!/bin/bash
語法縮進,使用四個空格;多加註釋說明。
命名建議規則:變數名大寫、局部變數小寫,函數名小寫,名字體現出實際作用。
默認變數是全局的,在函數中變數local指定為局部變數,避免污染其他作用域。
有兩個 命令 能幫助我調試腳本:set -e 遇到執行非0時退出腳本,set-x 列印執行過程。
寫腳本一定先測試再到生產上。
</pre>

1、獲取隨機字元串或數字

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">獲取隨機8位字元串:
方法1:

471b94f2
方法2:

vg3BEg==
方法3:

ed9e032c

獲取隨機8位數字:
方法1:

23648321
方法2:

38571131
方法3:

69024815

cksum:列印CRC效驗和統計位元組
</pre>

2、定義一個顏色輸出字元串函數

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:
function echo_color() {
if [ 233[0m"
elif [ 233[0m"
fi
}
方法2:
function echo_color() {
case 2[0m"
;;
red)
echo -e "[31;40m$2[0m"
;;
*)
echo "Example: echo_color red string"
esac
}

使用方法:echo_color green "test"

function關鍵字定義一個函數,可加或不加。
</pre>

3、批量創建用戶

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
DATE= 1 == "green" ]; then
echo -e "[32;40m 1 == "red" ]; then
echo -e "[31;40m$2[0m"
fi
}

if [ -s USER_FILE {DATE}.bak
echo_color green " {USER_FILE}- USER_FILE
echo "----------------" >> USER &>/dev/null; then
PASS= RANDOM |md5sum |cut -c 1-8)
useradd PASS |passwd --stdin USER USER_FILE
echo " USER User already exists!"
fi
done
</pre>

4、檢查軟體包是否安裝

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
if rpm -q sysstat &>/dev/null; then
echo "sysstat is already installed."
else
echo "sysstat is not installed!"
fi
</pre>

5、檢查服務狀態

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
PORT_C= (ps -ef |grep ntpd |grep -vc grep)
if [ PS_C -eq 0 ]; then
echo "內容" | mail -s "主題" [email protected]
fi
</pre>

6、檢查主機存活狀態

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:將錯誤IP放到數組裡面判斷是否ping失敗三次

IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in NUM -le 3 ]; do
if ping -c 1 IP Ping is successful."
break
else
# echo " NUM"
FAIL_COUNT[ IP
let NUM++
fi
done
if [ {FAIL_COUNT[1]} Ping is failure!"
unset FAIL_COUNT[*]
fi
done

方法2:將錯誤次數放到FAIL_COUNT變數裡面判斷是否ping失敗三次

IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in IP >/dev/null; then
echo " IP Ping is failure FAIL_COUNT -eq 3 ]; then
echo "$IP Ping is failure!"
fi
done

方法3:利用for循環將ping通就跳出循環繼續,如果不跳出就會走到列印ping失敗

ping_success_status() {
if ping -c 1 IP Ping is successful."
continue
fi
}
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in IP Ping is failure!"
done
</pre>

7、監控CPU、內存和硬碟利用率

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)CPU
藉助vmstat工具來分析CPU統計信息。

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (( SY))
if [ DATE
Host: USE
" | mail -s "CPU Monitor" $MAIL
fi

2)內存

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (free -m |awk /Mem/{print (free -m |awk /Mem/{print 6- (( USE))

if [ DATE
Host: TOTAL,Use= FREE
" | mail -s "Memory Monitor" $MAIL
fi

3)硬碟

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (fdisk -l |awk -F [: ]+ BEGIN{OFS="="}/^Disk /dev/{printf "%s=%sG,", 3} )
PART_USE= 1,int( 6} )
for i in (echo (echo (echo USE -gt 80 ]; then
echo "
Date: IP
Total: PART= MOUNT)
" | mail -s "Disk Monitor" $MAIL
fi
done
</pre>

8、批量主機磁碟利用率監控

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">前提監控端和被監控端SSH免交互登錄或者密鑰登錄。

寫一個配置文件保存被監控主機SSH連接信息,文件內容格式:IP User Port

HOST_INFO=host.info
for IP in 1} (awk -v ip= 1{print HOST_INFO)
PORT= IP ip== 3} PORT IP df -h > (awk BEGIN{OFS="="}/^/dev/{print 5)} USE_RATE_LIST; do
PART_NAME= {USE_RATE#*=}
if [ PART_NAME Partition usage $USE_RATE%!"
fi
done
done
</pre>

9、檢查網站可用性

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)檢查URL可用性
方法1:
check_url() {
HTTP_CODE= 1)
if [ 1 Access failure!"
fi
}
方法2:
check_url() {
if ! wget -T 10 --tries=1 --spider $1 >/dev/null 2>&1; then

}

使用方法:check_url www..com

2)判斷三次URL可用性
思路與上面檢查主機存活狀態一樣。

方法1:利用循環技巧,如果成功就跳出當前循環,否則執行到最後一行

check_url() {
HTTP_CODE= 1)
if [ URL_LIST; do
check_url URL
check_url URL Access failure!"
done

方法2:錯誤次數保存到變數

URL_LIST=" www..com www.agasgf.com "
for URL in (curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" HTTP_CODE -ne 200 ]; then
let FAIL_COUNT++
else
break
fi
done
if [ URL Access failure!"
fi
done

方法3:錯誤次數保存到數組

URL_LIST=" www..com www.agasgf.com "
for URL in NUM -le 3 ]; do
HTTP_CODE= URL)
if [ NUM]= NUM下標, {#FAIL_COUNT[ ]} -eq 3 ]; then
echo "Warning: $URL Access failure!"
unset FAIL_COUNT[ ] #清空數組
fi
done
</pre>

10、檢查MySQL主從同步狀態

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
USER=bak
PASSWD=123456
IO_SQL_STATUS= USER -p 0} ) #gsub去除冒號後面的空格
for i in {i%:*}
THREAD_STATUS= THREAD_STATUS" != "Yes" ]; then
echo "Error: MySQL Master-Slave THREAD_STATUS!"
fi
done
</pre>

動手練一練,讓你的Shell功底上升一個段位!

2. vivo本地終端shell怎麼用

1、首先創建數據文件(以.txt後綴或無後綴的文件)或導入已有的數據文件。
2、然後創建shell腳本(.sh後綴的文件),在shell腳本文件中使用whileread變數名讀取數據文件中的數據。
3、最後執行shell腳本文件,在使用tar壓縮文件即可。

3. linux(shell)腳本編程題目求教(200分請教)

1. 用vi打開一個空文件,文件名隨意,然後在第一行寫:
#!/bin/bash
另起一行跟上要使用的命令,比如ls date之類的,一條命令一行。
退出vi後,在命令行下輸入 chmod +x 文件名 給文件增加運行許可權
最後用 ./文件名 執行這個文件就可以了。

2. 環境變數用 env 和 set 2個命令可以看到,在命令行下直接輸入就可以了,至於具體內容,太多,你得自己去查

3.
for 循環:
i=1
for i in 1..100; do
i=$(($i + 1))
done

while循環
i=1
while [ $i -le 100 ]; do
i=$(($i + 1))
done

算了,這些都能做,但做起來太麻煩,放棄了

4. Shell腳本有幾種運行方式,如何使用

1. 輸入定向到Shell腳本,具體來講就是利用輸入重定向的機制,讓Shell解釋器順次讀取每一行腳本命令,進行執行。bash > test.sh 2. 類似方法一,只不過它是以腳本文件名為參數的。bash bash.sh 3. 將腳本文件設置成執行文件,直接在提示符下調用。

5. Shell腳本應該怎麼使用

1、用滑鼠對shell腳本點右鍵-》屬性-》許可權,顯示下圖

2、將「允許以程序執行文件」的選項打勾

3、即可在終端中執行腳本

6. shell怎麼用

shell 腳本類似於windos中的批處理,要學好shell得會大量的linux命令
shell腳本通常以 #!/bin/bash 開頭
簡單用法
例:
$vim test.sh //建一個shell文件
#!/bin/bash #shell文件以#!/bin/bash 開頭 #號為注釋
ehco hello world! #echo為輸出
:wq //退出vim
$sh ./test.sh //運行test.sh 腳本
hello world! //hello world!輸出在終端上

7. linux shell腳本執行命令詳解

在Linux下,如果你需要執行shell腳本,那麼應該使用那個命令呢?下面由我為大家整理了linux shell腳本執行命令的相關知識,希望對大家有幫助!

linux shell腳本執行

linux shell腳本執行方法一: 切換到shell腳本所在的目錄(此時,稱為工作目錄)執行shell腳本:

cd /data/shell

./hello.sh

./的意思是說在當前的工作目錄下執行hello.sh。如果不加上./,bash可能會響應找到不到hello.sh的錯誤信息。因為目前的工作目錄(/data/shell)可能不在執行程序默認的搜索路徑之列,也就是說,不在環境變數PASH的內容之中。查看PATH的內容可用 echo $PASH 命令。現在的/data/shell就不在環境變數PASH中的,所以必須加上./才可執行。

linux shell腳本執行方法二: 以絕對路徑的方式去執行bash shell腳本:

/data/shell/hello.sh

linux shell腳本執行方法三: 直接使用bash 或sh 來執行bash shell腳本:

cd /data/shell

bash hello.sh

cd /data/shell

sh hello.sh

注意,若是以方法三的方式來執行,那麼,可以不必事先設定shell的執行許可權,甚至都不用寫shell文件中的第一行(指定bash路徑)。因為方法三是將hello.sh作為參數傳給sh(bash)命令來執行的。這時不是hello.sh自己來執行,而是被人家調用執行,所以不要執行許可權。那麼不用指定bash路徑自然也好理解了啊。

linux shell腳本執行方法四: 在當前的shell環境中執行bash shell腳本:

cd /data/shell

. hello.sh

cd /data/shell

source hello.sh

前三種方法執行shell腳本時都是在當前shell(稱為父shell)開啟一個子shell環境,此shell腳本就在這個子shell環境中執行。shell腳本執行完後子shell環境隨即關閉,然後又回到父shell中。而方法四則是在當前shell中執行的。

8. shell編程 編寫SHELL腳本,能將文件d1和d2整合為文件d3。 編寫兩個shell 腳本s1、s2,其中s1能夠啟動3個進

1) cat d1 d2 >>d3

9. 使用shell 腳本 進行操作

nihao.txt中的目錄是絕對目錄嗎?
for d in `cat nihao.txt`;do cd $d;git pull;done

熱點內容
c語言負數運算 發布:2025-05-13 18:45:21 瀏覽:428
太空殺電腦版連接不到伺服器 發布:2025-05-13 18:40:19 瀏覽:457
同樣的配置為什麼跑分不同 發布:2025-05-13 18:39:06 瀏覽:278
獲取linuxcpu序列號 發布:2025-05-13 18:36:35 瀏覽:738
appleid為什麼連接伺服器出現問題 發布:2025-05-13 18:17:37 瀏覽:971
書翁怎麼配置 發布:2025-05-13 18:17:36 瀏覽:911
雲資料庫mongodb 發布:2025-05-13 18:16:12 瀏覽:774
A7編程 發布:2025-05-13 18:15:26 瀏覽:742
python視圖 發布:2025-05-13 18:14:01 瀏覽:759
win為什麼干不過安卓 發布:2025-05-13 18:12:27 瀏覽:586