Dash與Bash的文法區別

來源:互聯網
上載者:User

Dash與Bash的文法區別 如今Debian和Ubuntu中,/bin/sh預設已經指向dash,這是一個不同於bash的shell,它主要是為了執行指令碼而出現,而不是互動,它速度更快,但功能相比bash要少很多,文法嚴格遵守POSIX標準,下面簡要列舉下從bash遷移到dash一般需要注意的問題 1.定義函數 bash: function在bash中為關鍵字 igi@gentoo ~ $ foo(){ echo $0;}igi@gentoo ~ $ foo/bin/bashigi@gentoo ~ $ function foo2(){ echo $0;}igi@gentoo ~ $ foo2/bin/bashdash: dash中沒有function這個關鍵字 $ foo(){ echo $0;}$ foodash$ function foo2(){ echo $0;}dash: Syntax error: "(" unexpected2.select var in list; do command; done bash:支援 igi@gentoo ~ $ select input in A B> do>   case $input in>     A)>        echo 'Input:A'>        break>        ;;>     B)>        echo 'Input:B'>        break>        ;;>   esac> done1) A2) B#? 1Input:Aigi@gentoo ~ $ echo $0/bin/bashdash:不支援, 替代方法:採用while+read+case來實現 menu(){ echo -n "1)A;\n2)B\n>";}menuwhile read inputdo    case $input in      1)         echo 'A'         break         ;;      2)         echo 'B'         break         ;;      *)         menu         continue         ;;    esacdone3. echo {0..10} bash:支援{n..m}展開 igi@gentoo ~ $ echo $0/bin/bashigi@gentoo ~ $ echo {0..10}0 1 2 3 4 5 6 7 8 9 10dash:不支援,替代方法, 採用seq外部命令 $ echo $0dash$ echo {0..10}{0..10}$ echo `seq 0 10`0 1 2 3 4 5 6 7 8 9 104. here string bash:支援here string igi@gentoo ~ $ cat <<<"string"stringigi@gentoo ~ $ echo $0/bin/bashdash:不支援, 替代方法:可採用here documents $ echo $0dash$ cat <<<"string"dash: Syntax error: redirection unexpected$ cat <<EOF> string> EOFstring5. >&word重新導向標準輸出和標準錯誤 bash: 當word為非數字時,>&word變成重新導向標準錯誤和標準輸出到檔案word, 常見用法>&/dev/null igi@gentoo ~/test $ lsaigi@gentoo ~/test $ ls a bls: cannot access b: No such file or directoryaigi@gentoo ~/test $ ls a b >&/dev/nulligi@gentoo ~/test $ ls a b >/dev/null 2>&1igi@gentoo ~/test $ echo $0/bin/bashdash: >&word, word不支援非數字, 替代方法: >word 2>&1; 常見用法 >/dev/null 2>&1 $ echo $0dash$ ls aa$ ls a bls: cannot access b: No such file or directorya$ ls a b >&/dev/nulldash: Syntax error: Bad fd number$ ls a b >/dev/null 2>&1$6. 數組 bash: 支援數組, bash4支援關聯陣列 igi@gentoo ~/test $ echo $0/bin/bashigi@gentoo ~/test $ array=( a b c )igi@gentoo ~/test $ echo ${array[2]}cdash: 不支援數組,替代方法, 採用變數名+序號來實作類別似的效果 $ for i in a b c> do> id=$((${id:=-1}+1))> eval array_$id=$i> done$ echo ${array_1}b$ echo $0dash很蛋疼的方法,非不得以不建議這麼用 7. 子字串擴充 bash: 支援${parameter:offset:length},${parameter:offset} igi@gentoo ~/test $ string='hello'igi@gentoo ~/test $ echo ${string:1:3}elligi@gentoo ~/test $ echo ${string:1}elloigi@gentoo ~/test $ echo $0/bin/bashdash: 不支援, 替代方法:採用expr或cut外部命令代替 $ string='hello'$ expr substr "$string" 2 3ell$ echo "$string" | cut -c2-4ell$ expr substr "$string" 2 "${#string}"ello$ echo "$string" | cut -c2-ello$ echo $0dash$8. 大小寫轉換 bash: 支援${parameter^pattern},${parameter^^pattern},${parameter,pattern},${parameter,,pattern} igi@gentoo ~/test $ string="abcABC"igi@gentoo ~/test $ echo ${string^^}ABCABCigi@gentoo ~/test $ echo ${string,,}abcabcigi@gentoo ~/test $ echo ${string^^b}aBcABCigi@gentoo ~/test $ echo $0/bin/bashdash: 不支援,替代方法:採用tr/sed/awk等外部命令轉換 $ string='abcABC'$ echo "$string" | tr '[a-z]' '[A-Z]'ABCABC$ echo "$string" | tr '[A-Z]' '[a-z]'abcabc$ echo "$string" | sed 's/b/\U&/g'aBcABC$9. 進程替換<(command), >(command) bash: 支援進程替換 igi@gentoo ~ $ diff <(seq 3) <(seq 4)3a4> 4igi@gentoo ~ $ echo $0/bin/bashdash: 不支援, 替代方法, 通過臨時檔案中轉 $ diff <(seq 3) <(seq 4)dash: Syntax error: "(" unexpected$ seq 3 >tmp1$ seq 4 >tmp2$ diff tmp1 tmp23a4> 4$ echo $0dash$10. [ string1 = string2 ] 和 [ string1 == string2 ] bash: 支援兩者 igi@gentoo ~ $ [ 'a' = 'a' ] && echo 'equal'equaligi@gentoo ~ $ [ 'a' == 'a' ] && echo 'equal'equaligi@gentoo ~ $ echo $0/bin/bashdash: 只支援= $ [ 'a' = 'a' ] && echo 'equal'equal$ [ 'a' == 'a' ] && echo 'equal'[: 2: a: unexpected operator$ echo $0dash$11. [[ 加強版test bash: 支援[[ ]], 可實現正則匹配等強大功能 igi@gentoo ~ $ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal'equaligi@gentoo ~ $ echo $0/bin/bashdash: 不支援[[ ]], 替代方法,採用外部命令 $ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal'dash: [[: not found$ echo 'xyz123' | grep -q 'xyz[0-9]\+' && echo 'equal'equal$ echo $0dash$12. for (( expr1 ; expr2 ; expr3 )) ; do list ; done bash: 支援C語言格式的for迴圈 igi@gentoo ~ $ for((i=0;i<=3;i++));do echo "$i";done igi@gentoo ~ $ echo $0/bin/bashdash: 不支援該格式的for, 替代方法,用while+$((expression))實現 $ i=0$ while [ "$i" -le 3 ]> do> echo "$i"> i=$((i+1))> done0123$ echo $0dash$13. let命令和((expression)) bash: 有內建命令let, 也支援((expression))方式 igi@gentoo ~ $ i=0igi@gentoo ~ $ let i++igi@gentoo ~ $ echo $i1igi@gentoo ~ $ ((i++))igi@gentoo ~ $ echo $i2igi@gentoo ~ $ echo $0/bin/bashdash: 不支援,替代方法,採用$((expression))或者外部命令做計算 $ i=0$ i=$((i+1))$ echo $i1$ echo $0dash$14. $((expression)) bash: 支援id++,id--,++id,--id這樣到運算式 igi@gentoo ~ $ i=0igi@gentoo ~ $ echo $((i++))0igi@gentoo ~ $ echo $i1igi@gentoo ~ $ echo $((++i))2igi@gentoo ~ $ echo $i2igi@gentoo ~ $ echo $0/bin/bashdash: 不支援++,--, 替代方法:id+=1,id-=1, id=id+1,id=id-1 $ i=0$ echo $((i++))dash: arithmetic expression: expecting primary: "i++"$ echo $i;i=$((i+1))0$ echo $i1$ echo $((i+=1))2$ echo $i2$ echo $0dash$

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.