Linux Shell Bash HERE字符串
here string可以看成是here document的一種定制形式.除了COMMAND <<<$WORD, 就什么都沒有了,$WORD將被擴(kuò)展并且被送入COMMAND的stdin中.
1 String="This is a string of words."
2
3 read -r -a Words <<< "$String"
4 # "read"命令的-a選項
5 #+ 將會把結(jié)果值按順序的分配給數(shù)組中的每一項.
6
7 echo "First word in String is: ${Words[0]}" # This
8 echo "Second word in String is: ${Words[1]}" # is
9 echo "Third word in String is: ${Words[2]}" # a
10 echo "Fourth word in String is: ${Words[3]}" # string
11 echo "Fifth word in String is: ${Words[4]}" # of
12 echo "Sixth word in String is: ${Words[5]}" # words.
13 echo "Seventh word in String is: ${Words[6]}" # (null)
14 # $String的結(jié)尾.
15
16 # 感謝, Francisco Lobo的這個建議.
例子 17-13. 在一個文件的開頭添加文本
1 #!/bin/bash
2 # prepend.sh: 在文件的開頭添加文本.
3 #
4 # Kenny Stauffer所捐助的腳本例子,
5 #+ 本文作者對這個腳本進(jìn)行了少量修改.
6
7
8 E_NOSUCHFILE=65
9
10 read -p "File: " file # 'read'命令的-p參數(shù)用來顯示提示符.
11 if [ ! -e "$file" ]
12 then # 如果這個文件不存在, 那就進(jìn)來.
13 echo "File $file not found."
14 exit $E_NOSUCHFILE
15 fi
16
17 read -p "Title: " title
18 cat - $file <<<$title > $file.new
19
20 echo "Modified file is $file.new"
21
22 exit 0
23
24 # 下邊是'man bash'中的一段:
25 # Here String
26 # here document的一種變形,形式如下:
27 #
28 # <<<word
29 #
30 # word被擴(kuò)展并且被提供到command的標(biāo)準(zhǔn)輸入中.
例子 17-14. 分析一個郵箱
1 #!/bin/bash
2 # 由Francisco Lobo所提供的腳本,
3 #+ 本文作者進(jìn)行了少量修改和注釋.
4 # 并且經(jīng)過授權(quán), 可以使用在本書中.(感謝你!)
5
6 # 這個腳本不能運(yùn)行于比Bash version 3.0更低的版本中.
7
8
9 E_MISSING_ARG=67
10 if [ -z "$1" ]
11 then
12 echo "Usage: $0 mailbox-file"
13 exit $E_MISSING_ARG
14 fi
15
16 mbox_grep() # 分析郵箱文件.
17 {
18 declare -i body=0 match=0
19 declare -a date sender
20 declare mail header value
21
22
23 while IFS= read -r mail
24 # ^^^^ 重新設(shè)置$IFS.
25 # 否則"read"會從它的輸入中截去開頭和結(jié)尾的空格.
26
27 do
28 if [[ $mail =~ "^From " ]] # 匹配消息中的"From"域.
29 then
30 (( body = 0 )) # 取消("Zero out"俚語)變量.
31 (( match = 0 ))
32 unset date
33
34 elif (( body ))
35 then
36 (( match ))
37 # echo "$mail"
38 # 如果你想顯示整個消息體的話, 那么就打開上面的注釋行.
39
40 elif [[ $mail ]]; then
41 IFS=: read -r header value <<< "$mail"
42 # ^^^ "here string"
43
44 case "$header" in
45 [Ff][Rr][Oo][Mm] ) [[ $value =~ "$2" ]] && (( match++ )) ;;
46 # 匹配"From"行.
47 [Dd][Aa][Tt][Ee] ) read -r -a date <<< "$value" ;;
48 # ^^^
49 # 匹配"Date"行.
50 [Rr][Ee][Cc][Ee][Ii][Vv][Ee][Dd] ) read -r -a sender <<< "$value" ;;
51 # ^^^
52 # 匹配IP地址(可能被欺騙).
53 esac
54
55 else
56 (( body++ ))
57 (( match )) &&
58 echo "MESSAGE ${date:+of: ${date[*]} }"
59 # 整個$date數(shù)組 ^
60 echo "IP address of sender: ${sender[1]}"
61 # "Received"行的第二個域 ^
62
63 fi
64
65
66 done < "$1" # 將文件的stdout重定向到循環(huán)中.
67 }
68
69
70 mbox_grep "$1" # 將郵箱文件發(fā)送到函數(shù)中.
71
72 exit $?
73
74 # 練習(xí):
75 # -----
76 # 1) 拆開上面的這個函數(shù), 把它分成多個函數(shù),
77 #+ 這樣可以提高代碼的可讀性.
78 # 2) 對這個腳本添加額外的分析, 可以分析不同的關(guān)鍵字.
79
80
81
82 $ mailbox_grep.sh scam_mail
83 --> MESSAGE of Thu, 5 Jan 2006 08:00:56 -0500 (EST)
84 --> IP address of sender: 196.3.62.4