26.1 發送消息
26.1.1 功能分析
1.确定系統中都有誰
$who
給出的資訊包括使用者名 使用者所在終端 使用者登入系統的時間
2.啟用消息功能
使用者可以禁止别人給我發消息,是以需要先檢查一下是否允許發送消息。
$mesg
結果是is n說明消息發送被關閉了。 is y 表示允許發送消息
還可以檢視别人的消息狀态,
$who –T
使用者名後面的-表示使用者的消息功能已經關閉。+表示已經啟用
要接受消息可以使用mesg 的y選項,這樣消息功能就啟用了。
$mesg y
3.向其他使用者發送消息
消息功能啟用以後,就可以使用write指令通過使用者名和目前終端向其發消息
用who檢視:給xiaochongyong pts/19發送消息

$write xiaochongyong pts/19 #這樣就是發送消息
我開了兩個終端,一個是pts/17 一個是19。下面是19接收到的
注意:接收方接到消息後經常需要按回車來重新獲得指令行提示符
26.1.2 建立腳本
有幾個步驟
1)先檢查使用者是否登入
2)檢查使用者是否能接收消息
3)檢查是否包含要發送的消息
4)發送消息
代碼如下:
xcy@xcy-virtual-machine:~/shell/26zhang$ cat mu.sh
#!/bin/bash
muser=$1
logged_on=$(who | grep -i -m 1 $muser | gawk '{print $1}')
#1) determine if user is logged
if [ -z $logged_on ]
then
echo "$muser is not logged on"
echo "Exiting script......"
exit
fi
#2) determine if user allows messages
allowed=$(who -T | grep -i -m 1 $muser | gawk '{print $2}')
if [ $allowed != "+" ]
echo "$muser does not allowing messageing."
# 3) check message
if [ -z $2 ]
echo "No message parameter included"
shift
while [ -n "$1" ]
do
whole_message=$whole_message' '$1
shift
done
# 4) Send message to user
uterminal=$(who | grep -i -m 1 $muser | gawk '{print $2}')
echo $whole_message | write $logged_on $uterminal
exit
運作結果:
可以看到右下角的提醒:
26.2 擷取格言
26.2.1 功能分析
在指定的網址上提取一行格言,勵志的話。
1.學習wget
wget能夠将web頁面下載下傳到本地linux系統中。
功能十分豐富,這裡隻介紹一點點。
如何使用,使用wget指令和網站位址就行了
$wget www.baidu.com
會将網站的資訊存儲在與web頁面同名的檔案中。這裡是儲存在了index.html
還可以通過-o選項指定将會話輸出儲存在日志檔案中
$wget –o baidu.log www.baidu.com
還可以用-O,控制web頁面資訊儲存的位置。可以自己指定檔案名,
$wget –o baidu.log –O baidu.html www.baidu.com
2.測試web位址
有寫wed位址會變化。是以需要測試位址的有效性。
使用 –spider選項
$wget –spider www.baidu.com
還可以加上-nv選項,精簡輸出資訊
$wget –spider –nv www.baidu.com
這裡可以測試一個無效的位址
$wget –spider –nv www.xiaochongyong.com
26.2.2 建立腳本
xcy@xcy-virtual-machine:~/shell/26zhang$ cat quota.sh
quote_url=www.quotationspage.com/qotd.html
#quote_url=www.baidu.com
check_url=$(wget -nv --spider $quote_url 2>&1)
if [[ $check_url == *error404* ]]
echo "Bad web address"
echo "$quote_url invalid"
echo "Exiting script ..."
#wget -o /tmp/quote.log -O /tmp/quote.html $quote_url
wget -o quote.log -O quote.html $quote_url
sed 's/<[^>]*//g' quote.html | #去除<>
#grep "$(date +%B' '%-d,' '%Y)" -A2 | #比對格言中目前日期的右邊。-A2選項提取出另外 兩行文本
grep "28, $(date +%Y)" -A2 | # xcy add
sed 's/>//g' | # 删除>
sed '/ /{n ; d}' |
gawk 'BEGIN{FS=" "} {print $1}' |
tee daily_quote.txt > /dev/null #儲存檔案
xcy@xcy-virtual-machine:~/shell/26zhang$ ./quota.sh
xcy@xcy-virtual-machine:~/shell/26zhang$ cat daily_quote.txt
Selected from Michael Moncur's Collection of Quotations - November 28, 2017
Perpetual devotion to what a man calls his business, is only to be sustained by perpetual neglect of many other things. Robert Louis Stevenson (1850 - 1894)
xcy@xcy-virtual-machine:~/shell/26zhang$
注意這個:#grep "$(date +%B' '%-d,' '%Y)" -A2 |
由于我的烏班圖是中文的,一直都搜不到November。我就把它換成了這個,隻比對28, 2017
grep "28, $(date +%Y)" -A2 | # xcy add
本來想這樣的grep "$(date +%-d,’ ’%Y)" -A2 | # xcy add,不知道為啥我的日期是29。而網站上的是28.可能跟是美國的網站有關系。
26.3 編造借口
curl工具允許你從特定的Web伺服器中接收資料。還可以用它發送資料。
這裡測試需要美國的SMS卡,就不研究了。