麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 開發(fā) > Linux Shell > 正文

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

2020-07-27 19:21:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在shell腳本中,發(fā)現(xiàn)case語(yǔ)句的一個(gè)問(wèn)題。
就是指定小寫字母[a-z]和大寫字母[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)輸入大小寫字母都會(huì)輸出“Lowercase letter”

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

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

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

當(dāng)輸入大寫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)事。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 精品国产视频一区二区三区 | 蜜桃成品人免费视频 | 久久亚洲精品视频 | 久久99精品久久久久久秒播蜜臀 | 久久久久亚洲国产精品 | 亚洲成人福利在线 | 人人舔人人舔 | 国产羞羞视频在线观看免费应用 | 黄色毛片免费看 | 久久久久久久91 | 亚洲成人福利在线观看 | 91香蕉影视 | 制服丝袜日日夜夜 | 国产亚洲精品综合一区 | 俄罗斯16一20sex牲色另类 | 久久国产在线观看 | 亚洲四播房 | 国产日韩一区二区三区在线观看 | 亚洲一区久久久 | 日本免费一区二区三区四区 | 99视频有精品视频高清 | 亚洲视频在线观看免费 | 久久久久久久久免费 | 毛片视频网站 | 国产精品麻豆91 | 国产精品9191 | 国产女厕一区二区三区在线视 | 国产午夜亚洲精品理论片大丰影院 | 成人在线免费视频播放 | 久啪视频| 羞羞的动漫在线观看 | 精品亚洲va在线va天堂资源站 | 欧美一级高潮 | 精品国产一区二区三区天美传媒 | 91麻豆精品国产91久久久更新资源速度超快 | 欧美性生活区 | 国产日韩精品欧美一区视频 | 精品免费在线视频 | 牛牛热这里只有精品 | 龙床上的呻吟高h | 国产精品av久久久久久久久久 |