生活随笔
收集整理的這篇文章主要介紹了
Ubuntu下默认使用dash而非bash
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天在Ubuntu下調(diào)試代碼,明明是正確的,卻仍然報錯,查了錯誤信息才知道:Ubuntu中默認不是bash,而是為了加快開機速度,使用了dash。
dash中需要嚴格的語法,而且與bash語法不同。例如,bash中定義函數(shù)是使用function關(guān)鍵字的(function foo() {}),但在dash中沒有該關(guān)鍵字,直接使用foo(){}定義。我的錯誤就是這樣因為有了function而沒有通過。
注意:在sh文件首行定義的“#!/bin/bash”,指的是在(文件具有x權(quán)限的前提下),使用“./filename.sh”方法進行執(zhí)行的時候使用的bash。不過,我在把這行刪掉之后,使用"./filename.sh"方法還是可以執(zhí)行不出錯的。只有在使用“sh filename.sh”下才會出錯。
網(wǎng)上資料如下:
------:http://blog.sina.com.cn/s/blog_49c2012f0100z3bb.html
今天跟著練習(xí)一個shell scripts,內(nèi)容如下:
======================================= #!/bin/bash # Using for and loop # allen 2010/04/13 declare -i s # <==變量宣告 for (( i=1; i<=100; i=i+1 )) do ????????s=s+i done echo "The count is =l =======================================
但是運行時總是報下面這個錯,如下:
test11-loop.sh: 5: Syntax error: Bad for loop variable
幾經(jīng)查找語法,沒有問題,后來在網(wǎng)上找到問題原因:
代碼對于標(biāo)準(zhǔn)bash而言沒有錯,因為Ubuntu為了加快開機速度,用dash代替了傳統(tǒng)的bash,是dash在搗鬼。 解決方法是 取消dash sudo dpkg-reconfigure dash 在選擇項中選No,即可。
-----:http://www.2cto.com/os/201305/210033.html
Dash與Bash的語法區(qū)別
2013-05-11 14:08:06 ????我來說兩句??? 來源:www.igigo.net?? 收藏? ???我要投稿
Dash與Bash的語法區(qū)別 如今Debian和Ubuntu中,/bin/sh默認已經(jīng)指向dash,這是一個不同于bash的shell,它主要是為了執(zhí)行腳本而出現(xiàn),而不是交互,它速度更快,但功能相比bash要少很多,語法嚴格遵守POSIX標(biāo)準(zhǔn),下面簡要列舉下從bash遷移到dash一般需要注意的問題 1.定義函數(shù) bash: function在bash中為關(guān)鍵字 igi@gentoo ~ $ foo(){ echo $0;} igi@gentoo ~ $ foo /bin/bash igi@gentoo ~ $ function foo2(){ echo $0;} igi@gentoo ~ $ foo2 /bin/bash dash: dash中沒有function這個關(guān)鍵字 $ foo(){ echo $0;} $ foo dash $ function foo2(){ echo $0;} dash: Syntax error: "(" unexpected 2.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 > done 1) A 2) B #? 1 Input:A igi@gentoo ~ $ echo $0 /bin/bash dash:不支持, 替代方法:采用while+read+case來實現(xiàn) menu(){ echo -n "1)A;\n2)B\n>";} menu while read input do case $input in 1) echo 'A' break ;; 2) echo 'B' break ;; *) menu continue ;; esac done 3. echo {0..10} bash:支持{n..m}展開 igi@gentoo ~ $ echo $0 /bin/bash igi@gentoo ~ $ echo {0..10} 0 1 2 3 4 5 6 7 8 9 10 dash:不支持,替代方法, 采用seq外部命令 $ echo $0 dash $ echo {0..10} {0..10} $ echo `seq 0 10` 0 1 2 3 4 5 6 7 8 9 10 4. here string bash:支持here string igi@gentoo ~ $ cat <<<"string" string igi@gentoo ~ $ echo $0 /bin/bash dash:不支持, 替代方法:可采用here documents $ echo $0 dash $ cat <<<"string" dash: Syntax error: redirection unexpected $ cat <<EOF > string > EOF string 5. >&word重定向標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤 bash: 當(dāng)word為非數(shù)字時,>&word變成重定向標(biāo)準(zhǔn)錯誤和標(biāo)準(zhǔn)輸出到文件word, 常見用法>&/dev/null igi@gentoo ~/test $ ls a igi@gentoo ~/test $ ls a b ls: cannot access b: No such file or directory a igi@gentoo ~/test $ ls a b >&/dev/null igi@gentoo ~/test $ ls a b >/dev/null 2>&1 igi@gentoo ~/test $ echo $0 /bin/bash dash: >&word, word不支持非數(shù)字, 替代方法: >word 2>&1; 常見用法 >/dev/null 2>&1 $ echo $0 dash $ ls a a $ ls a b ls: cannot access b: No such file or directory a $ ls a b >&/dev/null dash: Syntax error: Bad fd number $ ls a b >/dev/null 2>&1 $ 6. 數(shù)組 bash: 支持數(shù)組, bash4支持關(guān)聯(lián)數(shù)組 igi@gentoo ~/test $ echo $0 /bin/bash igi@gentoo ~/test $ array=( a b c ) igi@gentoo ~/test $ echo ${array[2]} c dash: 不支持數(shù)組,替代方法, 采用變量名+序號來實現(xiàn)類似的效果 $ for i in a b c > do > id=$((${id:=-1}+1)) > eval array_$id=$i > done $ echo ${array_1} b $ echo $0 dash 很蛋疼的方法,非不得以不建議這么用 7. 子字符串?dāng)U展 bash: 支持${parameter:offset:length},${parameter:offset} igi@gentoo ~/test $ string='hello' igi@gentoo ~/test $ echo ${string:1:3} ell igi@gentoo ~/test $ echo ${string:1} ello igi@gentoo ~/test $ echo $0 /bin/bash dash: 不支持, 替代方法:采用expr或cut外部命令代替 $ string='hello' $ expr substr "$string" 2 3 ell $ echo "$string" | cut -c2-4 ell $ expr substr "$string" 2 "${#string}" ello $ echo "$string" | cut -c2- ello $ echo $0 dash $ 8. 大小寫轉(zhuǎn)換 bash: 支持${parameter^pattern},${parameter^^pattern},${parameter,pattern},${parameter,,pattern} igi@gentoo ~/test $ string="abcABC" igi@gentoo ~/test $ echo ${string^^} ABCABC igi@gentoo ~/test $ echo ${string,,} abcabc igi@gentoo ~/test $ echo ${string^^b} aBcABC igi@gentoo ~/test $ echo $0 /bin/bash dash: 不支持,替代方法:采用tr/sed/awk等外部命令轉(zhuǎn)換 $ 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 > 4 igi@gentoo ~ $ echo $0 /bin/bash dash: 不支持, 替代方法, 通過臨時文件中轉(zhuǎn) $ diff <(seq 3) <(seq 4) dash: Syntax error: "(" unexpected $ seq 3 >tmp1 $ seq 4 >tmp2 $ diff tmp1 tmp2 3a4 > 4 $ echo $0 dash $ 10. [ string1 = string2 ] 和 [ string1 == string2 ] bash: 支持兩者 igi@gentoo ~ $ [ 'a' = 'a' ] && echo 'equal' equal igi@gentoo ~ $ [ 'a' == 'a' ] && echo 'equal' equal igi@gentoo ~ $ echo $0 /bin/bash dash: 只支持= $ [ 'a' = 'a' ] && echo 'equal' equal $ [ 'a' == 'a' ] && echo 'equal' [: 2: a: unexpected operator $ echo $0 dash $ 11. [[ 加強版test bash: 支持[[ ]], 可實現(xiàn)正則匹配等強大功能 igi@gentoo ~ $ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal' equal igi@gentoo ~ $ echo $0 /bin/bash dash: 不支持[[ ]], 替代方法,采用外部命令 $ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal' dash: [[: not found $ echo 'xyz123' | grep -q 'xyz[0-9]\+' && echo 'equal' equal $ echo $0 dash $ 12. for (( expr1 ; expr2 ; expr3 )) ; do list ; done bash: 支持C語言格式的for循環(huán) igi@gentoo ~ $ for((i=0;i<=3;i++));do echo "$i";done igi@gentoo ~ $ echo $0 /bin/bash dash: 不支持該格式的for, 替代方法,用while+$((expression))實現(xiàn) $ i=0 $ while [ "$i" -le 3 ] > do > echo "$i" > i=$((i+1)) > done 0 1 2 3 $ echo $0 dash $ 13. let命令和((expression)) bash: 有內(nèi)置命令let, 也支持((expression))方式 igi@gentoo ~ $ i=0 igi@gentoo ~ $ let i++ igi@gentoo ~ $ echo $i 1 igi@gentoo ~ $ ((i++)) igi@gentoo ~ $ echo $i 2 igi@gentoo ~ $ echo $0 /bin/bash dash: 不支持,替代方法,采用$((expression))或者外部命令做計算 $ i=0 $ i=$((i+1)) $ echo $i 1 $ echo $0 dash $ 14. $((expression)) bash: 支持id++,id--,++id,--id這樣到表達式 igi@gentoo ~ $ i=0 igi@gentoo ~ $ echo $((i++)) 0 igi@gentoo ~ $ echo $i 1 igi@gentoo ~ $ echo $((++i)) 2 igi@gentoo ~ $ echo $i 2 igi@gentoo ~ $ echo $0 /bin/bash dash: 不支持++,--, 替代方法: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 $i 1 $ echo $((i+=1)) 2 $ echo $i 2 $ echo $0 dash $
總結(jié)
以上是生活随笔 為你收集整理的Ubuntu下默认使用dash而非bash 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。