思維導圖學 Linux Shell攻略之小試牛刀篇

來源:互聯網
上載者:User

標籤:思維導圖   linux   shell   

曾聽一位大神講過,帶著目的去學,知識往往能記得牢,記得穩。藉助思維導圖這個工具,對一些我感興趣的知識點進行分類管理。以後方便自己複習。

我會以思維導圖+程式碼片段的方式,復原學習linux shell編程。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/58/FA/wKiom1TCUMziHNpwAAFVIO3v-Io357.jpg" title="Linux Shell 攻略.png" alt="wKiom1TCUMziHNpwAAFVIO3v-Io357.jpg" />

轉義/色彩

與使用者互動的介面

#列印一個普通的字串[[email protected] ~]# echo "it‘s isa dog"it‘s is a dog #列印一個帶有單引號和分行符號的字串,單引號可正常輸出,但分行符號沒有效果#沒有達到想要的效果[[email protected] ~]# echo "it‘s isa dog\n this is new line"it‘s is a dog\n this is new line # -e 開啟轉義功能[[email protected] ~]# echo -e "it‘sis a dog\nthis is new line"it‘s is a dogthis is new line-e     enable interpretation of backslash escapes [[email protected] ~]# echo it is a dogit is a dog #紅字[[email protected] ~]# echo -e "\e [1;31mthisis a color\e[0m"this is a color[[email protected] ~]# echo -e"\033[1;31mthis is a red color\033[0m"this is a red  color#綠底[[email protected] ~]# echo -e"\e[1;42mthis is a red color\e[0m"this is a red  color #紅字綠底[[email protected] ~]# echo -e"\e[1;31;42mthis is a red color\e[0m"this is a red  color #有效數字echo "scale=3;3/8"|bcecho $bc

計算

這是程式設計語言的功能之一了

va=1;vb=2;#echo $($va+$vb);error#echo $(va+vb); errorecho [$va+$vb] #output :[1+2] echo $[va+vb]  #okecho $(($va+$vb)) #//ok let result=$va+vb #okecho $resultresult=`expr 3 + 1` #ok, 注意等號,兩邊不能有空格;result=`expr $va + 1` 也可以echo $resultresult=$(expr $va + 1) #ok, 注意等號,兩邊不能有空格,+號必須有空格,否則會當成字串輸出echo $result
輸出變數長度

內建功能(感興趣而已)

[[email protected] test]# exportSTR="1234" [[email protected] test]# echo $STR1234[[email protected] test]# echo ${#STR}4
函數

這是最基本的,不能語句羅列吧

#括弧裡不能有參數,擷取參數通過$1,$2....擷取function sayHello(){         echohello $1}#[email protected]:參數列表#$*:參數字串sayHello zgy;#這樣調用
讀取命令序列

可得一個命令的結果

#!/bin/bash  COMMANDS=ls|cat -n echo $COMMANDS #輸出為空白  COMMANDS=$(ls|cat -n) #$COMMANDS #error echo $COMMANDS #輸出期望結果   echo `$COMMANDS` #error echo `ls|cat -n` #輸出期望結果  反引用 ################################################子shell,在子shell操作,不影響主shellecho `pwd`;cd /binecho `pwd`; # output## /root/test# /bin echo `pwd`;(cd /bin)echo `pwd`;# output## /root/test# /root/test
列印所用時間

評定一個演算法的效率

start=$(date +%s) #start=`date +%s`,等號不能有空格,如果有空格,會被變數當成命令for (( i = 0; i < 100000; i++ ));do         echo$i >/dev/nulldoneend=`date +%s` diff=$(($end-$start))echo "use times(ms):"$diff echo "use times(ms):"$(($end-$start))
常用的測試

判斷許可權等,shell編程匯總功能常用

#[[]] 必須有空格#是否是檔案,檔案是否存在[[email protected] test]# [[ -f 1.txt ]]&& echo "1.txt is file" || echo  "1.txt is notfile"1.txt is file#是否是可執行檔[[email protected] test]# [[ -x 1.txt ]]&& echo "1.txt can be execute" ||  echo  "1.txt can be execute"1.txt can be execute[[email protected] test]# [[ -x 1.txt ]]&& echo "1.txt can be execute" ||  echo  "1.txt can‘t be execute"1.txt can‘t be execute [[email protected] test]# chmod  +x 1.txt[[email protected] test]# [[ -x 1.txt ]]&& echo "1.txt can be execute" ||  echo  "1.txt can‘t be execute"1.txt can be  execute[[email protected] test]##是否是目錄[[email protected] test]# [[ -d 1.txt ]]&& echo "1.txt is dir" || echo  "1.txt is‘t dir"1.txt is‘t dir[[email protected] test]# [[ -d /bin ]]&& echo "1.txt is dir" || echo  "1.txt is‘t dir"1.txt is dir#判斷是空串嗎?[[email protected] test]# [[ -z"1" ]] && echo "is null" ||  echo "is not null"is not null[[email protected] test]# [[ -z"" ]] && echo "is null" ||  echo "is not null"is null-z 與-n功能相反


小計

看書本,很簡單的代碼,也就是一看就懂的代碼。其實真正自己寫出來,在運行起來得到結果,也不容易。 眼高手低要不得。

我就在寫程式是經常遇到一些這樣情況。有時候要求有空格(比如條件判斷時)。有時候不能有空格(變數賦值時)。有時候,單引號有時候又 反引號。哎要注意啊這些小細節,總結經驗。

小小代碼也不簡單。

如果廣大讀者,也可以看著我的腦圖,一步步寫一下指令碼,也會有所收穫。

算個開篇吧。斷斷續續,隨著學習深入,例子也會逐漸深入。希望自己的shell水平,能有所突破。

本文出自 “簡單” 部落格,請務必保留此出處http://dba10g.blog.51cto.com/764602/1607563

思維導圖學 Linux Shell攻略之小試牛刀篇

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.