曾聽一位大神講過,帶著目的去學(xué),知識往往能記得牢,記得穩(wěn)。借助思維導(dǎo)圖這個工具,對一些我感興趣的知識點進行分類管理。以后方便自己復(fù)習(xí)。
我會以思維導(dǎo)圖+代碼段的方式,回滾學(xué)習(xí)linux shell編程。
與用戶交互的接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #打印一個普通的字符串 [root@beijing ~] # echo "it's isa dog" it's is a dog #打印一個帶有單引號和換行符的字符串,單引號可正常輸出,但換行符沒有效果 #沒有達到想要的效果 [root@beijing ~] # echo "it's isa dog\n this is new line" it's is a dog\n this is new line # -e 開啟轉(zhuǎn)義功能 [root@beijing ~] # echo -e "it'sis a dog\nthis is new line" it's is a dog this is new line -e enable interpretation of backslash escapes [root@beijing ~] # echo it is a dog it is a dog #紅字 [root@beijing ~] # echo -e "\e [1;31mthisis a color\e[0m" this is a color [root@beijing ~] # echo -e"\033[1;31mthis is a red color\033[0m" this is a red color #綠底 [root@beijing ~] # echo -e"\e[1;42mthis is a red color\e[0m" this is a red color #紅字綠底 [root@beijing ~] # echo -e"\e[1;31;42mthis is a red color\e[0m" this is a red color #有效數(shù)字 echo "scale=3;3/8" | bc echo $ bc |
計算
這是編程語言的功能之一了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | va=1; vb=2; #echo $($va+$vb);error #echo $(va+vb); error echo [$va+$vb] #output :[1+2] echo $[va+vb] #ok echo $(($va+$vb)) #//ok let result=$va+vb #ok echo $result result=` expr 3 + 1` #ok, 注意等號,兩邊不能有空格;result=`expr $va + 1` 也可以 echo $result result=$( expr $va + 1) #ok, 注意等號,兩邊不能有空格,+號必須有空格,否則會當(dāng)成字符串輸出 echo $result |
內(nèi)置功能(感興趣而已)
1 2 3 4 5 | [root@beijing test ] # exportSTR="1234" [root@beijing test ] # echo $STR 1234 [root@beijing test ] # echo ${#STR} 4 |
這是最基本的,不能語句羅列吧
1 2 3 4 5 6 7 | #括號里不能有參數(shù),獲取參數(shù)通過$1,$2....獲取 function sayHello(){ echohello $1 } #$@:參數(shù)列表 #$*:參數(shù)字符串 sayHello zgy; #這樣調(diào)用 |
可得一個命令的結(jié)果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #!/bin/bash COMMANDS= ls | cat -n echo $COMMANDS #輸出為空 COMMANDS=$( ls | cat -n) #$COMMANDS #error echo $COMMANDS #輸出期望結(jié)果 echo `$COMMANDS` #error echo ` ls | cat -n` #輸出期望結(jié)果 反引用 ############################################### #子shell,在子shell操作,不影響主shell echo ` pwd `; cd /bin echo ` pwd `; # output# # /root/test # /bin echo ` pwd `; ( cd /bin ) echo ` pwd `; # output# # /root/test # /root/test |
評定一個算法的效率
1 2 3 4 5 6 7 8 9 10 | start=$( date +%s) #start=`date +%s`,等號不能有空格,如果有空格,會被變量當(dāng)成命令 for (( i = 0; i < 100000; i++ )); do echo $i > /dev/null done end=` date +%s` diff =$(($end-$start)) echo "use times(ms):" $ diff echo "use times(ms):" $(($end-$start)) |
判斷權(quán)限等,shell編程匯總功能常用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #[[]] 必須有空格 #是否是文件,文件是否存在 [root@beijing test ] # [[ -f 1.txt ]]&& echo "1.txt is file" || echo "1.txt is notfile" 1.txt is file #是否是可執(zhí)行文件 [root@beijing test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" || echo "1.txt can be execute" 1.txt can be execute [root@beijing test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" || echo "1.txt can't be execute" 1.txt can't be execute [root@beijing test ] # chmod +x 1.txt [root@beijing test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" || echo "1.txt can't be execute" 1.txt can be execute [root@beijing test ] # #是否是目錄 [root@beijing test ] # [[ -d 1.txt ]]&& echo "1.txt is dir" || echo "1.txt is't dir" 1.txt is't dir [root@beijing test ] # [[ -d /bin ]]&& echo "1.txt is dir" || echo "1.txt is't dir" 1.txt is dir #判斷是空串嗎? [root@beijing test ] # [[ -z"1" ]] && echo "is null" || echo "is not null" is not null [root@beijing test ] # [[ -z"" ]] && echo "is null" || echo "is not null" is null -z 與-n功能相反 |
小計
看書本,很簡單的代碼,也就是一看就懂的代碼。其實真正自己寫出來,在運行起來得到結(jié)果,也不容易。 眼高手低要不得。
我就在寫程序是經(jīng)常遇到一些這樣情況。有時候要求有空格(比如條件判斷時)。有時候不能有空格(變量賦值時)。有時候,單引號有時候又 反引號。哎要注意啊這些小細節(jié),總結(jié)經(jīng)驗。
小小代碼也不簡單。
如果廣大讀者,也可以看著我的腦圖,一步步寫一下腳本,也會有所收獲。
算個開篇吧。斷斷續(xù)續(xù),隨著學(xué)習(xí)深入,例子也會逐漸深入。希望自己的shell水平,能有所突破。
本文出自 “簡單” 博客,請務(wù)必保留此出處http://dba10g.blog.51cto.com/764602/1607563