定義正則表達式
export NAME=wyc declare -x NAME=wyc NAME=wyc ;export NAME
打印環境變量shell
echo $HOME /$UID /$PWD /$SHELL /$USER printf "$HOME\n" # printf "" $HOME \n
用unset消除本地變量和環境變量bash
echo $USER unset USER
變量名=value # 無引號
變量名='value' # 單引號
變量名="value" # 雙引號運維
變量名通常是由字母、數字、下劃線組成的,開頭除了數字。學習
$變量名錶示輸出變量,能夠用$a和${a}兩種用法。測試
通常的變量定義、賦值經常使用雙引號;簡單連續的字符串能夠不加引號;但願原樣輸出時使用單引號。spa
變量名=`ls` 變量名=$(ls)
W=`ls` echo $W Y=$(pwd) echo $Y
連續的數字或字符串,不加引號;強引用。通常連續的字符串、數字、路徑等能夠不加任何引號進行賦值和輸出。
有空格,需解析,就加雙引號;弱引用,默認。內容中有帶反引號的命令、變量、特殊轉義等所需的解析出結果,再輸出最終內容。雙引號來代替,特別是對變量賦值時。
原樣就用單引號;不能空格。通常連續的字符串、數字、路徑等能夠不加任何引號進行賦值和輸出。
命令用反引號,或者括號,注意反引號和單引號的區別;至關於$()。
通常用echo,複雜用printf,去掉f就能夠;命令行
$A=${A};#大括號
$dbname_tname,改${dbname}_tname,當變量後面鏈接有其餘字符的時候。
建議:code
收集不明白的地方:
grep.log?orm
問題爲:
4)已知:/etc/hosts的內容爲
192.168.1.11 oldboy11.etiantian.org 192.168.1.21 oldboy21.etiantian.org 192.168.1.31 oldboy31.etiantian.org #192.168.1.111 oldboy111.etiantian.org
請用shell腳本實現,怎麼才能在輸入IP後找到/etc/hosts裏對應的惟一的hostname?
解答:
法1)腳本過濾法
[root@old_boy scripts]# cat judgehost.sh #!/bin/bash echo "please input ip address:" read ip [ -n "`grep "$ip " /etc/hosts`" ] && \ #注意前面的過濾條件結尾帶有空格。 echo "The hostname is: `grep "$ip " /etc/hosts |awk '{print $2}'`" || \ echo "The ip is invalid"
提示:
1)這是一個grep過濾加條件判斷的實現語法:
2)條件判斷語法爲[ -n "ddd" ] && echo 1 || echo 0
3)[ -n "grep "$ip " /etc/hosts
" ] && #注意前面的過濾條件結尾帶有空格。這裏啊,是爲了排除下面的重複狀況
192.168.1.11 oldboy11.etiantian.org
192.168.1.111 oldboy111.etiantian.org
----------------我是每種方法分隔符---------------
法2)腳本精確匹配法:
#!/bin/bash #author oldboy #qq 31333741 #judge input if [ $# -ne 1 ] then echo "input error!" exit 1 fi flag=0 exec < /etc/hosts while read line do if [ "$1" = "`echo $line|awk '{print $1}'`" ] then flag=1 echo "the $1 's hostname is `echo $line|awk '{print $2}'`" break; fi done [ $flag -eq 0 ] && echo " sorrry,not find $1 's hostname!"
提示:此題,請你們學習while的用法及設置flag的思路。
執行結果:
[root@old_boy scripts]# sh oldboy.sh 192.168.1.11
the 192.168.1.11 's hostname is oldboy11.etiantian.org
[root@old_boy scripts]# sh oldboy.sh 192.168.1.21
the 192.168.1.21 's hostname is oldboy21.etiantian.org
[root@old_boy scripts]# sh oldboy.sh 192.168.1.311
sorrry,not find 192.168.1.311 's hostname!
----------------我是每種方法分隔符---------------
特別提示:下面的方法中,老男孩老師大量的使用了awk的不一樣方法來實現一樣的功能,來告訴你們,awk是很強大的, 但願同窗們能按照老師的教學要求精通之。
法3)awk精確匹配:
準備:
[root@old_boy scripts]# tail -4 /etc/hosts
192.168.1.11 oldboy11.etiantian.org
192.168.1.111 oldboy111.etiantian.org
192.168.1.21 oldboy21.etiantian.org
192.168.1.31 oldboy31.etiantian.org
腳本:
[root@old_boy scripts]# cat awkhost1.sh awk 'BEGIN {a="'$1'"} {if($1==a) print $2; }' /etc/hosts
執行結果:
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.21
oldboy21.etiantian.org
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.31
oldboy31.etiantian.org
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.11
oldboy11.etiantian.org
提示:注意a="'$1'"的用法,$1爲命令行傳參。awk程序中調用系統變量的方法a="'$1'"。
----------------我是每種方法分隔符---------------
法4)awk精確匹配法
[root@old_boy scripts]# cat awkhost2.sh awk '{if($1=="'$1'") print $2}' /etc/hosts
執行結果:
[root@old_boy scripts]# awkhost2.sh 192.168.1.11
oldboy11.etiantian.org
[root@old_boy scripts]# awkhost2.sh 192.168.1.21
oldboy21.etiantian.org
[root@old_boy scripts]# awkhost2.sh 192.168.1.311
----------------我是每種方法分隔符---------------
法5)awk過濾法
[root@old_boy scripts]# cat awkhost4.sh awk '/'"${1} "'/''{print $2}' /etc/hosts 執行結果: [root@old_boy scripts]# awkhost4.sh 192.168.1.21 oldboy21.etiantian.org [root@old_boy scripts]# awkhost4.sh 192.168.1.11 oldboy11.etiantian.org [root@old_boy scripts]# awkhost4.sh 192.168.1.31 oldboy31.etiantian.org 提示:除了語法外,這道題有個學問,就是過濾時傳參結尾要帶個空格,這樣才能過濾重複IP的狀況 如: 192.168.1.11 oldboy11.etiantian.org 192.168.1.111 oldboy111.etiantian.org
----------------我是每種方法分隔符---------------
法6)awk過濾法
[root@old_boy scripts]# cat awkhost5.sh awk '{if($1~/'$1'/) print $2}' /etc/hosts ##若是文件第一列包含命令行第一個參數字符則打印第二列 執行結果: [root@old_boy scripts]# awkhost5.sh 192.168.1.31 oldboy31.etiantian.org [root@old_boy scripts]# awkhost5.sh 192.168.1.11 oldboy11.etiantian.org oldboy111.etiantian.org ------>這裏有bug了。 [root@old_boy scripts]# awkhost5.sh 192.168.1.21 oldboy21.etiantian.org 改進下來排除bug: [root@old_boy scripts]# cat awkhost5-1.sh awk '{if($1~/'$1' /) print $2}' /etc/hosts ==>用上面加空格的思路不對。 [root@old_boy scripts]# cat awkhost5-1.sh awk '{if($1~/'$1'$/) print $2}' /etc/hosts #增長一個正則表達式$ 執行結果: [root@old_boy scripts]# awkhost5-1.sh 192.168.1.21 oldboy21.etiantian.org [root@old_boy scripts]# awkhost5-1.sh 192.168.1.11 oldboy11.etiantian.org [root@old_boy scripts]# awkhost5-1.sh 192.168.1.31 oldboy31.etiantian.org
----------------我是每種方法分隔符---------------
法7)awk -v精確匹配法
命令行測試: [root@old_boy scripts]# awk -v p=192.168.1.21 '$1 == p{print $2}' /etc/hosts oldboy21.etiantian.org [root@old_boy scripts]# awk -v p=192.168.1.11 '$1 == p{print $2}' /etc/hosts oldboy11.etiantian.org [root@old_boy scripts]# awk -v p=192.168.1.11 '$1 == p {print $2}' /etc/hosts oldboy11.etiantian.org 實際腳本: [root@old_boy scripts]# cat awkhost6.sh #!/bin/bash #p=$1 #awk -v p="$p" '$1 == p{print $2}' /etc/hosts awk -v p="$1" '$1 == p{print $2}' /etc/hosts
執行結果:
[root@old_boy scripts]# sh awkhost6.sh 192.168.1.11
oldboy11.etiantian.org
[root@old_boy scripts]# sh awkhost6.sh 192.168.1.21
oldboy21.etiantian.org
提示:
1)傳參非awk程序,所以寫法p="$1"
2)man awk
-v var=val --assign var=val Assign the value val to the variable var, before execution of the program begins. Such vari- able values are available to the BEGIN block of an AWK program.
----------------我是每種方法分隔符---------------
法8:精確匹配簡單的寫法
[root@old_boy scripts]# cat awkhost9.sh awk '$1 == "'$1'" {print $2}' /etc/hosts 執行結果: [root@old_boy scripts]# sh awkhost9.sh 192.168.1.11 oldboy11.etiantian.org [root@old_boy scripts]# sh awkhost9.sh 192.168.1.21 oldboy21.etiantian.org [root@old_boy scripts]# sh awkhost9.sh 192.168.1.31 oldboy31.etiantian.org 特別提示:這裏老男孩老師大量的使用了awk的不一樣方法來實現一樣的功能,很強大吧, 但願同窗們能按照老師的教學要求精通之。
----------------我是每種方法分隔符---------------
法9:學生的一個不成熟的實現法
#!/bin/bash b=/$PWD/wang.txt echo -n "plase input ip : " read a if [ $a == "192.168.1.11" ] then cat $b | grep $a | awk -F ' ' '{print $2}' elif [ $a == "192.168.1.21" ] then cat $b | grep $a | awk -F ' ' '{print $2}' elif [ $a == "192.168.1.31" ] then cat $b | grep $a | awk -F ' ' '{print $2}' else echo "plase input the correct IP address " && exit 1 fi 提示:你們看看問題在哪?腳本不通用。
----------老男孩老師改進後
#!/bin/bash #author oldboy #qq 31333741 hosts_file="$PWD/oldboy.txt" #judge file [ ! -f $hosts_file ] && echo "no test file!" && exit 1 echo -n "plase input ip : " read ip #judge ip format [ "${#a}" -lt 8 ] && [ "`echo $ip|sed 's/[0-9]//g'`" != "..." ] && \ echo "Plase input the correct IP address" && exit 1 #start result1=$(grep "$ip" $hosts_file|awk '{print $1}') if [ "$ip" == "$result1" ] then grep "$ip" $hosts_file|awk '{print $2}' exit 0 else echo "Not find the hostname of $ip" exit 1 fi 提示:此法不可取,多此一舉了。
$0 # 路徑
$n # 1~9不用說了,0表示腳本的文件名,大於9,帶大括號
$# # 參數的總個數
$* # 不加引號和$@相同,加雙引號視爲單個字符串,至關於「$1 $2 $3」
$@ # 不加引號和$*相同,加雙引號有區別,視爲不一樣的獨立字符串,至關於"$1" "$2" "$3" "..."
# \ ${1..15} > n.sh # 四個 echo \${1..15} >n.sh # 利用大括號輸出15個位置參數並定向到文件 n.sh 裏。 echo ${10} # 正確 echo $10 # 不正確
只須要關注特殊變量($1)的內容。