運維 我常用的shell指令碼彙總

2021-09-19 08:06:33 字數 1347 閱讀 3525

某天發現/home分割槽滿了,想知道是哪個目錄佔了大頭,使用該指令碼可以幫你完成排序

du --max-depth=1 /home/ | sort -n -r
--max-depth=1只統計一級目錄

sort -n -r按照數字-逆序排序

ps -ef | egrep 'foo|bar' | grep -v grep | awk ''|xargs kill
命令解讀:

ps -ef | egrep 'mysql|ssh'配合grep是常見的組合可以全欄位搜尋

$ps -ef | head

uid pid ppid c stime tty time cmd

root 1 0 0 2015 ? 00:00:30 /sbin/init

root 2 0 0 2015 ? 00:00:16 [kthreadd]

root 3 2 0 2015 ? 00:08:19 [migration/0]

root 4 2 0 2015 ? 00:07:58 [ksoftirqd/0]

root 5 2 0 2015 ? 00:00:00 [migration/0]

root 6 2 0 2015 ? 00:00:00 [watchdog/0]

root 7 2 0 2015 ? 00:08:01 [migration/1]

root 8 2 0 2015 ? 00:00:00 [migration/1]

root 9 2 0 2015 ? 00:04:57 [ksoftirqd/1]

egrep就是grep -e的變體,支援擴充套件正規表示式。

grep -v grep也是常見的字尾,目的是過濾grep程序本身的。

awk利用awk做列切分,預設按照空白符進行切分,切分後列印出第二列。結合上面的ps就是選擇列印第二列結果。

運維常用shell指令碼

開頭加直譯器 bin bash 語法縮排,開頭用四個空格 多加注釋說明。命名規則 變數名大寫 區域性變數小寫 函式名小寫 名字能夠體現實際作用。預設變數是全域性的,在函式中變數local指定為區域性變數,避免汙染其他作用域。指令碼寫完後一定要先除錯再線上使用。echo random md5sum c...

運維常用shell

列出你最常用的10條shell history awk end sort rn head history awk end sort rn head grep v bash history awk end head 網路連線數目 netstat an grep e tcp cut c 68 sort ...

使用shell指令碼輔助運維工作

儲存到 etc ethers檔案中,若此檔案已存在,則先轉移進行備份。每行一條記錄,第1列為ip位址,第2列為對應的mac位址。2 編寫乙個scanhost.sh的掃瞄指令碼,檢查有哪些主機開啟了ftp服務,掃瞄物件是 etc ethers中所有的ip位址。指令碼如下 root localhost ...