最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2013年11月07日 17:38:26   作者:  
在shell腳本中,發(fā)現(xiàn)case語(yǔ)句的一個(gè)問(wèn)題。就是指定小寫字母[a-z]和大寫字母[A-Z]的這種方法不管用了

在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)事。

相關(guān)文章

  • shell編程基礎(chǔ)之認(rèn)識(shí)與學(xué)習(xí)BASH

    shell編程基礎(chǔ)之認(rèn)識(shí)與學(xué)習(xí)BASH

    本文介紹下,shell基礎(chǔ)編程中有關(guān)bash的相關(guān)知識(shí),有需要的朋友參考學(xué)習(xí)下
    2013-11-11
  • Shell腳本解壓rpm軟件包

    Shell腳本解壓rpm軟件包

    這篇文章主要介紹了Shell腳本解壓rpm軟件包,用來(lái)解壓后提取某個(gè)包中文件,需要的朋友可以參考下
    2014-06-06
  • shell一鍵部署Zabbix的實(shí)現(xiàn)步驟

    shell一鍵部署Zabbix的實(shí)現(xiàn)步驟

    本文主要介紹了shell一鍵部署Zabbix的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-02-02
  • linux?top命令基本實(shí)戰(zhàn)

    linux?top命令基本實(shí)戰(zhàn)

    top命令的功能是用于實(shí)時(shí)顯示系統(tǒng)運(yùn)行狀態(tài),包含處理器、內(nèi)存、服務(wù)、進(jìn)程等重要資源信息,這篇文章主要介紹了linux?top命令?實(shí)戰(zhàn),需要的朋友可以參考下
    2023-02-02
  • shell腳本中取消重定向的方法實(shí)例

    shell腳本中取消重定向的方法實(shí)例

    這篇文章主要介紹了shell腳本中取消重定向的方法實(shí)例,本文直接給出代碼實(shí)例,需要的朋友可以參考下
    2015-03-03
  • crontab每10秒執(zhí)行一次的實(shí)現(xiàn)方法

    crontab每10秒執(zhí)行一次的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇crontab每10秒執(zhí)行一次的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • Linux Shell函數(shù)返回值

    Linux Shell函數(shù)返回值

    這篇文章主要介紹了Linux Shell函數(shù)返回值,需要的朋友可以參考下
    2016-11-11
  • 一個(gè)簡(jiǎn)單的linux命令 pwd

    一個(gè)簡(jiǎn)單的linux命令 pwd

    這篇文章主要介紹了一個(gè)簡(jiǎn)單的linux命令pwd,pwd命令用于查看當(dāng)前工作目錄的完整路徑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Linux常用命令與命令縮寫整理

    Linux常用命令與命令縮寫整理

    這篇文章介紹了Linux的常用命令與命令縮寫,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Shell腳本獲取進(jìn)程的運(yùn)行時(shí)間

    Shell腳本獲取進(jìn)程的運(yùn)行時(shí)間

    這篇文章主要介紹了Shell腳本獲取進(jìn)程的運(yùn)行時(shí)間,需要的朋友可以參考下
    2014-06-06

最新評(píng)論

汉川市| 高州市| 嘉祥县| 大连市| 浦北县| 南华县| 松江区| 兴国县| 竹溪县| 定日县| 东乡族自治县| 尉犁县| 金昌市| 靖远县| 获嘉县| 金门县| 静乐县| 西贡区| 金昌市| 红原县| 黎平县| 介休市| 德江县| 汉寿县| 沈阳市| 泸西县| 天门市| 镇安县| 丹凤县| 芦溪县| 青铜峡市| 雷州市| 崇文区| 湘阴县| 霍林郭勒市| 积石山| 济宁市| 六枝特区| 吴江市| 封开县| 元谋县|