#du -h –max-depth=1 ./</pre>
#tail
#more
#head
#touch newfile
#cat
//查看文件file內容,找到分隔符: 以第三項倒敘排列
#sort -t ':' -k 2 -r file
#man df
#df –help
//查看當前文件夾下所有文件的總大小,并以可讀的倒敘的形式排列
#du -sh * | sort -nr
#grep 'bluestep' file
//搜索包含bluestep的行,并顯示行號
#grep -n 'bluestep' file
//搜索包含bluestep的總數
#grep -c 'bluestep' file
//搜索不包含bluestep的行
#grep -v 'bluestep' file
//搜索包含blue和head的行
#grep -e blue -e head 'bluestep' file
//搜索“os_login”在當前目錄下所有文件,并顯示行號
#grep -n -R "os_login" ./*
#grep -n -R "os_login" ./
#tar -cvf test.tar /dir1 /dir2
#tar -xvf test.tar /dir1 /dir2
//進程樹
#ps –forest
//將進程列表置入后臺
#(sleep 2 ; echo $BASH_SUBSHELL ; sleep 2)&
#which ps
#type -a ps
#alias li='ls -li'
#$? 命令運行后的狀態
//vim編輯器
?關鍵詞
/關鍵詞
:s/old/new/ vim編輯器會跳到old第一次出現的地方,并用new來替換。
:s/old/new/g:一行命令替換所有old。
:n,ms/old/new/g:替換行號n和m之間所有old。
:%s/old/new/g:替換整個文件中的所有old。
:%s/old/new/gc:替換整個文件中的所有old,但在每次出現時提示。
//shfile內容如下
#!/bin/bash
var1=10.46
var2=43.67
var3=33.2
var4=71
var5=$(bc << EOF
scale = 4
a1 = ( $var1 * $var2)
b1 = $[$var1 * ($var2 – $var3)]
a1 + b1
EOF
)
echo The final answer for this mess is $var5
#chmod u+x shfile
./shfile
#rpm -qa > rpm.list
#sort < rpm.list
#rpm -qa | sort | more
#rpm -qa | sort > rpm.list
#expr 1 + 5
//重定向錯誤和數據,&包含STDOUT和STDERR
#ls -al test test2 test3 badtest 2> test6 1> test7
#ls -al test test2 test3 badtest &> test7
//第14章介紹了如何使用read命令讀取用戶在鍵盤上輸入的數據。將STDIN重定向到文件后,
//當read命令試圖從STDIN讀入數據時,它會到文件去取數據,而不是鍵盤。
$ cat test12
#!/bin/bash
# redirecting file input
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done
$ ./test12
Line #1: This is the first line.
Line #2: This is the second line.
//記錄消息 -a追加
#who | tee -a testfile
//lsof會顯示當前Linux系統上打開的每個文件的有關信息
#lsof -i :8080
//要想知道進程的當前PID,可以用特殊環境變量$$(shell會將它設為當前PID)。-a選項用來
//對其他兩個選項的結果執行布爾AND運算,這會產生如下輸出。
#/usr/sbin/lsof -a -p $$ -d 0,1,2
$cat test23
#!/bin/bash
# read file and create INSERT statements for MySQL
outfile='members.sql'
IFS=','
while read lname fname address city state zip
do
cat >> $outfile << EOF
INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('$lname', '$fname', '$address', '$city', '$state', '$zip');
EOF
done < ${1}
當運行程序test23時,$1代表第一個命令行參數。它指明了待讀取數據的文件。read語句
會使用IFS字符解析讀入的文本,我們在這里將IFS指定為逗號。
腳本中另外兩處重定向操作出現在同一條語句中:
cat >> $outfile << EOF
這條語句中包含一個輸出追加重定向(雙大于號)和一個輸入追加重定向(雙小于號) 。輸
出重定向將cat命令的輸出追加到由$outfile變量指定的文件中。 cat命令的輸入不再取自標準
輸入,而是被重定向到腳本中存儲的數據。EOF符號標記了追加到文件中的數據的起止。
#./test23 members.csv
//信號相關
1 SIGHUP 掛起
2 SIGINT 中斷
3 SIGQUIT 終止
9 SIGKILL 無條件終止
18 SIGTSTP 停止或暫停進程,但不終止進程
CTRL+C生成SIGINT信號,CTRL+Z生成SIGTSTP信號
kill -9 2343
//每次使用Ctrl+C組合鍵,腳本都會執行trap命令中指定的echo語句,而不是處理該信號并允許shell停止該腳本。
trap "echo ' Sorry! I have trapped Ctrl-C'" SIGINT
//要捕獲shell腳本的退出,只要在trap命令后加上EXIT信號就行
trap "echo Goodbye…" EXIT
//在后臺運行
#./test5.sh &
//查看哪些腳本正在后臺運行
#ps
//即使終端退出也不停止進程
#nohup ./test1.sh &
#./test10.sh > test10.out &
#jobs -l
#./sh
CTRL+Z停止,顯示作業id和狀態,bg或fg后可加作業id后臺或前臺啟動
#bg
#fg 2
//優先級
#nice -n -10 ./test4.sh > test4.out &
#ps -p 4993 -o pid,ppid,ni,cmd
#renice -n 10 -p 5055
#at -M -f test13b.sh now //-M不顯示輸出信息
#atq //哪些作業正在等待
#atrm 18
#crontab -e
#crontab -l
可以用nohup命令阻止這種情況發生。該命令會攔截任何發給某個命令來停止其運行的信號
(比如當你退出終端會話時)。這樣就可以讓腳本繼續在后臺運行,即便是你已經退出了終端會話。
當你將進程置入后臺時,仍然可以控制它的運行。 jobs命令可以查看該shell會話啟動的進程。
只要知道后臺進程的作業號,就可以用kill命令向該進程發送Linux信號,或者用fg命令將該進
程帶回到該shell會話的前臺。你可以用Ctrl+Z組合鍵掛起正在運行的前臺進程,然后用bg命令將
其置入后臺模式。
第三部分 Part 3
高級 shell 腳本編程
sed流編輯器(stream editor)
重要的是,要記住,sed編輯器并不會修改文本文件的數據。它只會將修改后的數據發送到
STDOUT。如果你查看原來的文本文件,它仍然保留著原始數據。
sed -e 's/brown/green/; s/dog/cat/' data1.txt
sed -f script1.sed data1.txt
//全局替換
sed 's/test/trial/g' data4.txt
//-n選項將禁止sed編輯器輸出。但p替換標記會輸出修改過的行。將二者配合使用的效果就是
//只輸出被替換命令修改過的行。
sed -n 's/test/trial/p' data5.txt
//只有那些包含匹配模式的行才會保存在指定的輸出文件中。
sed 's/test/trial/w test.txt' data5.txt
sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd
sed 's!/bin/bash!/bin/csh!' /etc/passwd
sed '2s/dog/cat/' data1.txt
sed '2,3s/dog/cat/' data1.txt
//如果想將命令作用到文本中從某行開始的所有行,可以用特殊地址——美元符
sed '2,$s/dog/cat/' data1.txt
grep Samantha /etc/passwd
Samantha:x:502:502::/home/Samantha:/bin/bash
//只有Samantha這行的bash被替換了
sed '/Samantha/s/bash/csh/' /etc/passwd
sed '2{s/fox/elephant/ s/dog/cat/}' data1.txt
sed '3,${
> s/brown/green/
> s/lazy/active/
> }' data1.txt
sed '3d' data6.txt
sed '2,3d' data6.txt
sed '3,$d' data6.txt
sed '/number 1/d' data6.txt
sed '/1/,/3/d' data6.txt
echo "Test Line 2" | sed 'a\Test Line 1'
echo "Test Line 2" | sed 'i\
> Test Line 1'
sed '$a\
> This is a new line of text.' data6.txt
sed '1i\
> This is one line of new text.\
> This is another line of new text.' data6.txt
sed '3c\
> This is a changed line of text.' data6.txt
sed '2,3c\
> This is a new line of text.' data6.txt
sed 'y/123/789/' data8.txt
echo "This 1 is a test of 1 try." | sed 'y/123/456/'
//打印命令最常見的用法是打印包含匹配文本模式的行
sed -n '/number 3/p' data6.txt
sed -n '2,3p' data6.txt
sed -n '/3/{
> p
> s/line/test/p
> }' data6.txt
//打印行號
sed '=' data1.txt
sed -n '/number 4/{
> =
> p
> }' data6.txt
//列出(list)命令(l)可以打印數據流中的文本和不可打印的ASCII字符
cat data9.txt
This line contains tabs.
sed -n 'l' data9.txt
This\tline\tcontains\ttabs.$
sed '1,2w test.txt' data6.txt
sed -n '/Browncoat/w Browncoats.txt' data11.txt
//讀取data12.txt所有文本到data6.txt匹配的行之后
sed '3r data12.txt' data6.txt
sed '/number 2/r data12.txt' data6.txt
sed '$r data12.txt' data6.txt
sed '/LIST/{
> r data11.txt
> d
> }' notice.std
gawk '{print $1}' data2.txt
gawk -F: '{print $1}' /etc/passwd
cat script2.gawk
{print $1 "'s home directory is " $6}
gawk -F: -f script2.gawk /etc/passwd
$ gawk 'BEGIN {print "The data3 File Contents:"}
> {print $0}
> END {print "End of File"}' data3.txt
cat script4.gawk
BEGIN {
print "The latest list of users and shells"
print " UserID \t Shell"
print "——– \t ——-"
FS=":"
}
{
print $1 " \t " $7
}
END {
print "This concludes the listing"
}
$ gawk -f script4.gawk /etc/passwd
正則表達式
//刪除空白航行
sed '/^$/d' data5
//*表示出現0次或多次
echo "btt" | sed -n '/b[ae]*t/p'
gawk程序能夠識別ERE模式,但sed編輯器不能
//*表示出現0次或1次,最多只能出現一次
echo "beet" | gawk '/be?t/{print $0}'
//可以出現1次或多次,但必須至少出現1次
echo "bet" | gawk '/be+t/{print $0}'
//e出現一次
echo "bt" | gawk –re-interval '/be{1}t/{print $0}'
//e最少出現一次,最多兩次,如下兩個都不打印
echo "beeet" | gawk –re-interval '/be{1,2}t/{print $0}'
echo "baeaet" | gawk –re-interval '/b[ae]{1,2}t/{print $0}'
//通道
echo "The dog is asleep" | gawk '/cat|dog/{print $0}'
//表達式分組
echo "Sat" | gawk '/Sat(urday)?/{print $0}'
//刪除匹配行的下一行
sed '/header/{n ; d}' data1.txt
//N,將下一行和當前行放在模式空間,當成“一行”
sed 'N ; s/System.Administrator/Desktop User/' data3.txt
//匹配度兩行都刪除
sed 'N ; /System\nAdministrator/d' data4.txt
//只刪除匹配的第一行
sed 'N ; /System\nAdministrator/D' data4.txt
//刪除header匹配行前的空行匹配行
sed '/^$/{N ; /header/D}' data5.txt
//-n選項來阻止腳本輸出,當多行匹配出現時,P命令只會打印模式空間中的第一行
sed -n 'N ; /System\nAdministrator/P' data3.txt
sed -n '/first/ {h ; p ; n ; p ; g ; p }' data2.txt
sed -n '/header/!p' data2.txt
//反轉文本文件中的行,同linux命令tac(cat的反寫)功能一樣
sed -n '{1!G ; h ; $p }' data2.txt
echo "This, is, a, test, to, remove, commas." | sed -n '{
> :start
> s/,//1p
> /,/b start
> }'
echo "The cat sleeps in his hat." | sed 's/.at/"&"/g'
The "cat" sleeps in his "hat".
echo "The System Administrator manual" | sed '
> s/\(System\) Administrator/\1 User/'
The System User manual
echo "That furry hat is pretty" | sed 's/furry \(.at\)/\1/'
That hat is pretty
//每行后插入空白行,不包括最后一行
sed '$!G' data2.txt
sed '/^$/d ; $!G' data6.txt
//N,將下一行和當前行放在模式空間,當成“一行”;n將下一行加入當前行模式空間,但打印只顯示下一行數據
sed '=' data2.txt | sed 'N; s/\n/ /'
nl data2.txt
cat -n data2.txt
1 This is the header line.
2 This is the first data line.
3 This is the second data line.
4 This is the last line.
//打印末尾行
sed -n '$p' data2.txt
//輸出中只會在行間保留一個空白行
sed '/./,/^$/!d' data8.txt
//刪除開頭的空白行
sed '/./,$!d' data9.txt
//刪除結尾的空白行
sed '{
:start
/^
\n*$/{$d; N; b start }
}'
//刪除 HTML標簽
sed 's/<[^>]*>//g ; /^$/d' data11.txt
gawk 'BEGIN{FS="\n"; RS=""} {print $1,$4}' data2
gawk 'BEGIN{x=4; x= x * 2 + 3; print x}'
cat script1
BEGIN{FS=","}
{print $n}
$ gawk -f script1 n=2 data1
gawk -F: '$1 ~ /rich/{print $1,$NF}' /etc/passwd
gawk –F: '$1 !~ /rich/{print $1,$NF}' /etc/passwd
gawk -F: '$4 == 0{print $1}' /etc/passwd
gawk -F, '$1 == "data11"{print $1}' data1
gawk '{if ($1 > 20) print $1}' data4
gawk '{
> if ($1 > 20)
> {
> x = $1 * 2
> print x
> }
> }' data4
gawk '{
> total = 0
> for (i = 1; i < 4; i++)
> {
> total += $i
> }
> avg = total / 3
> print "Average:",avg
> }' data5
gawk 'BEGIN{FS="\n"; RS=""} {print $1,$4}' data2
gawk 'BEGIN{FS="\n"; RS=""} {printf "%s %s\n", $1, $4}' data2
//16表示寬度
gawk 'BEGIN{FS="\n"; RS=""} {printf "%16s %s\n", $1, $4}' data2
gawk 'BEGIN{FS="\n"; RS=""} {printf "%-16s %s\n", $1, $4}' data2
gawk 'BEGIN{x = "testing"; print toupper(x); print length(x) }'
gawk 'BEGIN{
> date = systime()
> day = strftime("%A, %B %d, %Y", date)
> print day
> }'
很遺憾,Ubuntu Linux發行版將/bin/sh文件鏈接到了shell程序/bin/dash。由于dash shell只含有
原來Bourne shell中的一部分命令,這可能會(而且經常會)讓有些shell腳本無法正確工作。
下一節將帶你逐步了解dash shell的基礎知識以及它跟bash shell的區別。如果你編寫的bash
shell腳本可能要在Ubuntu環境中運行,了解這些內容就尤其重要。
由于dash的目標是簡潔,因此它的環境變量比bash shell少多了。在dash shell環境中編寫腳本時要記住這點。
dash shell用set命令來顯示環境變量
sudo du -S /var/log/ | sort -rn
鏈接:https://pan.baidu.com/s/1PN-qFxEFT3AAp3fAgZ9esg
提取碼:0563