濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > shell腳本中case條件控制語(yǔ)句的一個(gè)bug分析

shell腳本中case條件控制語(yǔ)句的一個(gè)bug分析

熱門標(biāo)簽:打電話機(jī)器人接我是他的秘書(shū) 華鋒e路航港口地圖標(biāo)注 江蘇云電銷機(jī)器人公司 如果做線上地圖標(biāo)注 地圖標(biāo)注員都是年輕人 百度地圖標(biāo)注錯(cuò)了有責(zé)任嗎 揭陽(yáng)智能電話機(jī)器人推薦 河南信譽(yù)好的不封卡電話外呼系統(tǒng) 客服外呼系統(tǒng)怎么樣

在shell腳本中,發(fā)現(xiàn)case語(yǔ)句的一個(gè)問(wèn)題。
就是指定小寫(xiě)字母[a-z]和大寫(xiě)字母[A-Z]的這種方法不管用了。

出現(xiàn)如下情況:

復(fù)制代碼 代碼如下:

[root@station1 ~]# cat case.sh
#!/bin/bash
while :
do
echo -n "input a letter: "
read var
case "$var" in
  [a-z]) echo "Lowercase letter";;
  [A-Z]) echo "Uppercase letter";;
 [0-9]) echo "Digit";;
  *) echo "Punctuation, whitespace, or other";;
esac
done
[root@station1 ~]# bash case.sh
input a letter: a
Lowercase letter
input a letter: A
Lowercase letter
input a letter: 2
Digit
input a letter: 0
Digit
input a letter: B
Lowercase letter
input a letter: y
Lowercase letter
input a letter: ^C
[root@station1 ~]#

可以看到當(dāng)輸入大小寫(xiě)字母都會(huì)輸出“Lowercase letter”

就當(dāng)我疑惑不解的時(shí)候,奇跡發(fā)生了。。。。

復(fù)制代碼 代碼如下:

[root@station1 ~]# bash case.sh
input a letter: Z
Uppercase letter
input a letter:

當(dāng)輸入大寫(xiě)Z的時(shí)候,終于出現(xiàn)了我們想要的結(jié)果:Uppercase letter
后來(lái)在man bash文檔中也沒(méi)有關(guān)于"-"代表范圍的說(shuō)明,值說(shuō)想匹配"-",就把"-"放到[]中最前面或者最后面。
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for pathname
expansion (see Pathname Expansion below). The word is expanded using tilde expansion, parameter and variable expansion, arithmetic sub-
stitution, command substitution, process substitution and quote removal. Each pattern examined is expanded using tilde expansion, param-
eter and variable expansion, arithmetic substitution, command substitution, and process substitution. If the shell option nocasematch is
enabled, the match is performed without regard to the case of alphabetic characters. When a match is found, the corresponding list is
executed. If the ;; operator is used, no subsequent matches are attempted after the first pattern match. Using ; in place of ;; causes
execution to continue with the list associated with the next set of patterns. Using ;; in place of ;; causes the shell to test the next
pattern list in the statement, if any, and execute any associated list on a successful match. The exit status is zero if no pattern
matches. Otherwise, it is the exit status of the last command executed in list.

再看下面這段代碼:

復(fù)制代碼 代碼如下:

[root@station1 ~]# cat case.sh
#!/bin/bash
while :
do
echo -n "input a letter: "
read var
case "$var" in
[a-c]) echo "Lowercase letter";;
[A-Z]) echo "Uppercase letter";;
[0-9]) echo "Digit";;
*) echo "Punctuation, whitespace, or other";;
esac
done
[root@station1 ~]# bash case.sh
input a letter: a
Lowercase letter
input a letter: b
Lowercase letter
input a letter: c
Lowercase letter
input a letter: d
Uppercase letter
input a letter: e
Uppercase letter
input a letter: ^C
[root@station1 ~]#

可以看出來(lái)它的編碼方式是:aAbBcCdDeE...yYzZ
所以才會(huì)出現(xiàn)這種情況。這也算是一個(gè)小bug吧,如果想真的想達(dá)到我們想要的結(jié)果,可以用posix的[:upper:]。
個(gè)人想法:有時(shí)候出現(xiàn)這種情況也不是個(gè)壞事,或許還可以利用這個(gè)bug去做點(diǎn)事。

您可能感興趣的文章:
  • 詳解JavaScript中循環(huán)控制語(yǔ)句的用法
  • 詳解JavaScript的流程控制語(yǔ)句
  • linux shell流程控制語(yǔ)句實(shí)例講解(if、for、while、case語(yǔ)句實(shí)例)
  • Flash中常用到的ActionScript控制語(yǔ)句用法
  • JavaScript中的for循環(huán)與雙重for循環(huán)詳解
  • C語(yǔ)言控制語(yǔ)句之 循環(huán)

標(biāo)簽:許昌 邵陽(yáng) 赤峰 婁底 巴彥淖爾 馬鞍山 金昌 淘寶邀評(píng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《shell腳本中case條件控制語(yǔ)句的一個(gè)bug分析》,本文關(guān)鍵詞  shell,腳本,中,case,條件,控制,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《shell腳本中case條件控制語(yǔ)句的一個(gè)bug分析》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于shell腳本中case條件控制語(yǔ)句的一個(gè)bug分析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    富源县| 白沙| 洞口县| 吉木萨尔县| 昌邑市| 建阳市| 保靖县| 饶平县| 盱眙县| 尚义县| 理塘县| 嘉祥县| 明光市| 西吉县| 闽侯县| 修文县| 佳木斯市| 巴南区| 高碑店市| 盐亭县| 盐池县| 门头沟区| 孟津县| 娱乐| 定日县| 昌都县| 瑞丽市| 澄城县| 内乡县| 邳州市| 建平县| 浮山县| 乐至县| 东莞市| 龙井市| 即墨市| 徐州市| 合江县| 潞城市| 正蓝旗| 宁安市|