當前位置:首頁 » 編程軟體 » shell腳本的應用

shell腳本的應用

發布時間: 2022-09-25 00:37:29

A. shell腳本是什麼

當執行命令或程序語句是通過程序文件而不是命令行,那這個程序被稱為Shell腳本。如果Shell腳本內置很多命令、語句及循環控制,然後一次性執行完畢,這種通過文件執行腳本的方式稱為非交互方式。用戶可以在Shell腳本中輸入一系列命令及命令語句組合。這些命令、變數和流程式控制制語句等有機地結合在一起,就形成一個功能強大的Shell腳本。

B. shell腳本具體是干什麼的,是在用什麼的時候會用得到

Shell遵從經典UNIX哲學:把復雜的問題分解成簡單的小問題,然後再把各部分功能組合起來解決復雜問題。
例如我可以用shell腳本來監控伺服器整體的性能,當CPU負載超過我預設的警戒線,磁碟空間的閥值超過我預設的標准,伺服器宕機,這些都可以通過shell編寫腳本做到自動給我發告警郵件或者發簡訊通知我。簡化每日繁瑣的工作步驟,數據的備份,日誌的搜集整理。太多太多了。..

C. shell腳本的常用循環

在簡單介紹shell腳本(shell script)之前,先簡單介紹一下shell。

在計算機科學中, shell (殼層)指「為用戶提供用戶界面」的軟體,通常指的是命令行界面的解析器。一般來說,這個詞是指操作系統中,提供訪問內核所提供之服務的程序。Shell也用於泛指所有為用戶提供操作界面的程序,也就是程序和用戶交互的介面。因此與之相對的是程序內核(英語:Kernel),內核不提供和用戶的交互功能。
不過這個詞也拿來指應用軟體,或是任何在特定組件外圍的軟體,例如瀏覽器或電子郵件軟體是HTML排版引擎的shell。Shell這個詞是來自於操作系統(內核)與用戶界面的外層界面。
通常將shell分為兩類:命令行與圖形界面。命令行殼層提供一個命令行界面(CLI);而圖形殼層提供一個圖形用戶界面(GUI)。

shell腳本 (英語:shell script),又稱shell命令稿、程序化腳本,是一種電腦程序與文本文件,內容由一連串的shell命令組成,經由Unix Shell直譯其內容後運作。被當成是一種腳本語言來設計,其運作方式與解釋型語言相當,由Unix shell扮演命令行解釋器的角色,在讀取shell腳本之後,依序運行其中的shell命令,之後輸出結果。利用shell腳本可以進行系統管理,文件操作等。
在Unix及所有的類Unix系統中,如Linux、FreeBSD等操作系統,都存在shell腳本。依照Unix shell的各種不同類型,shell腳本也有各種不同方言,如 Bourne shell (sh), Bourne-Again shell (bash), C shell (csh)等等。在DOS、OS/2、Microsoft Windows中的批處理文件,跟shell腳本有類似的功能。

常見的shell腳本循環語法包括for、while和until。除此之外,還有一些其他語法,能夠實現循環(遍歷)功能。和循環相關的還有循環控制符break和continue,用於控制流程轉向。

for循環屬於「當型循環」,即「當滿足條件的時候執行」。

for循環有三種類型:

常見的數值for循環語法還有:

for file in $( ls )語法的等效語法有很多,如下:

或者

類C風格的for循環,和C沒有太大的區別,這里只進行示例,不再贅述。

或者

結果:

也稱為前測試循環語句,重復次數是利用一個條件來控制是否繼續重復執行這個語句。為了避免死循環,必須保證循環體中包含循環出口條件即表達式存在退出狀態為非0的情況。例如,計算1~100以內所有的奇數之和:

當然,條件可以使字元串,可以是輸入值,可以是標記等,此處不再贅述。

until命令和while命令類似,while能實現的功能until也可以實現。但區別是until循環的退出狀態是為0(與while剛好相反),即whie循環在條件為真時繼續執行循環,而until則在條件為假時執行循環。
例如,計算1~100以內所有的奇數之和:

select結構從技術角度看不能算是循環結構,只是相似而已,它是bash的擴展結構用於互動式菜單顯示,功能類似於case結構,但是比case的交互性要好。在遍歷列表功能中,select結構可以實現循環的功能。
例如,以下選擇,只有選擇white的時候,退出循環

在循環語法中,經常需要根據條件控制循環退出或跳過本次執行,這時候就需要用到循環控制符。循環控制符主要包括兩個:break和continue。

循環控制符和C以及Java等常見語法功能類似,在理不再贅述,只給出常用示例。

在for、while和until循環中break可強行退出循環。
注意 :break語句僅能退出當前的循環,如果是兩層循環嵌套,則需要在外層循環中使用break。
例如,計算1~100以內所有的奇數之和:

在for、while和until中用於讓腳本跳過其後面的語句,執行下一次循環。
例如,顯示10以內能被3整除的正整數。

以上是自己在學習Linux的過程中的一部分總結,和大家共享。同時,也參考了其他人的博客,在參考列表中列出來了。

D. Shell腳本語言優勢怎樣

Shell腳本語言的優勢在於處理偏操作系統底層的業務,例如:Linux系統內部的很多應用(有的是應用的一部分)都是使用Shell腳本開發的,因為有1000多個Linux系統命令為它做支撐,特別是Linux正則表達式及三劍客grep、sed、awk等命令。
對於一些常見的系統腳本使用Shell開發會更簡單、更快速,例如:讓軟體一鍵自動化安裝、優化,監控報警腳本,軟體啟動腳本,日誌分析腳本等,雖然PHP/Python語言也能夠做到這些,但是,考慮到掌握難度、開發效率、開發習慣等因素,它們可能就不如Shell腳本語言流行及有優勢了。對於一些常規的業務應用,使用Shell更符合Linux運維簡單、易用、高效的三大基本原則。
PHP語言的優勢在於小型網站系統的開發;Python語言的優勢在於開發較復雜的運維工具、Web界面的管理工具和Web業務的開發(例如:CMDB自動化運維平台、跳板機、批量管理軟體SaltStack、雲計算OpenStack軟體)等。我們在開發一個應用時應根據業務需求,結合不同語言的優勢及自身擅長的語言來選擇,揚長避短,從而達到高效開發及易於自身維護等目的。

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

shell腳本 都是解釋執行的。
一種是通過 .命令來執行。比如 ./t.sh

一種是通過source命令來執行,source等同於.

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

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

G. 標準的shell腳本應該包含什麼

一個標準的shell腳本包括bin、conf、log三個文件夾bin包含腳本的具體內容;conf包含腳本需要獲取的配置;log包含腳本執行過程有無錯誤的log和跟業務相關的log。在計算機科學中,Shell俗稱殼,是指「為使用者提供操作界面」的軟體(命令解析器)。它用於接收用戶命令,然後調用相應的應用程序。同時它又是一種程序設計語言。作為命令語言,它互動式解釋和執行用戶輸入的命令或者自動地解釋和執行預先設定好的一連串的命令;作為程序設計語言,它定義了各種變數和參數,並提供了許多在高級語言中才具有的控制結構,包括循環和分支。在排序演算法中,Shell是希爾排序的名稱。

H. 什麼是shell腳本

操作系統與外部最主要的介面就叫做shell。shell是操作系統最外面的一層。shell管理你與操作系統之間的交互:等待你輸入,向操作系統解
釋你的輸入,並且處理各種各樣的操作系統的輸出結果。shell提供了你與操作系統之間通訊的方式。這種通訊可以以交互方式(從鍵盤輸
入,並且可以立即得到響應),或者以shellscript(非交互)方式執行。shellscript是放在文件中的一串shell和操作系統命令,它們可以被
重復使用。本質上,shellscript是命令行命令簡單的組合到一個文件裡面。Shell基本上是一個命令解釋器,類似於DOS下的command.com
。它接收用戶命令(如ls等),然後調用相應的應用程序。較為通用的shell有標準的Bourneshell(sh)和Cshell(csh)。
互動式shell和非互動式shell互動式模式就是shell等待你的輸入,並且執行你提交的命令。這種模式被稱作互動式是因為shell與用戶進行交
互。這種模式也是大多數用戶非常熟悉的:登錄、執行一些命令、簽退。當你簽退後,shell也終止了。shell也可以運行在另外一種模式:非
互動式模式。在這種模式下,shell不與你進行交互,而是讀取存放在文件中的命令,並且執行它們。當它讀到文件的結尾,shell也就終止了。
shell的類型在UNIX中主要有兩大類shellBourneshell(包括sh,ksh,andbash)Bourneshell(sh)Kornshell(ksh)BourneAgain
shell(bash)POSIXshell(sh)Cshell(包括cshandtcsh)Cshell(csh)TENEX/TOPSCshell(tcsh).
附:LINUX系統的shell原理
Linux系統的shell作為操作系統的外殼,為用戶提供使用操作系統的介面。它是命令語言、命令解釋程序及程序設計語言的統稱。
shell是用戶和Linux內核之間的介面程序,如果把Linux內核想像成一個球體的中心,shell就是圍繞內核的外層。當從shell或其他程序向
Linux傳遞命令時,內核會做出相應的反應。shell是一個命令語言解釋器,它擁有自己內建的shell命令集,shell也能被系統中其他應用
程序所調用。用戶在提示符下輸入的命令都由shell先解釋然後傳給Linux核心。

I. 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功底上升一個段位!

熱點內容
安卓70能用什麼軟體 發布:2025-05-16 01:45:09 瀏覽:480
編程發展史 發布:2025-05-16 01:38:52 瀏覽:528
android圖片氣泡 發布:2025-05-16 01:38:40 瀏覽:885
文件加密編輯器下載 發布:2025-05-16 01:30:41 瀏覽:343
linuxapacheyum安裝 發布:2025-05-16 01:30:31 瀏覽:476
大連賓利浴池wifi密碼是多少 發布:2025-05-16 01:25:36 瀏覽:172
緩存數據生產服務 發布:2025-05-16 01:08:58 瀏覽:584
普通電腦伺服器圖片 發布:2025-05-16 01:04:02 瀏覽:971
伺服器地址和埠如何區分 發布:2025-05-16 01:03:17 瀏覽:834
重新編目資料庫 發布:2025-05-16 00:54:34 瀏覽:514