欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

shell腳本中case條件控制語句的一個bug分析

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

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

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

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

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

當(dāng)輸入大寫Z的時候,終于出現(xiàn)了我們想要的結(jié)果:Uppercase letter
后來在man bash文檔中也沒有關(guān)于"-"代表范圍的說明,值說想匹配"-",就把"-"放到[]中最前面或者最后面。
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 ~]#

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

相關(guān)文章

  • 在shell腳本中激活conda虛擬環(huán)境的方法總結(jié)

    在shell腳本中激活conda虛擬環(huán)境的方法總結(jié)

    在Anaconda中conda可以理解為一個工具,也是一個可執(zhí)行命令,其核心功能是包管理與環(huán)境管理,下面這篇文章主要給大家介紹了關(guān)于如何在shell腳本中激活conda虛擬環(huán)境的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • linux shell命令行參數(shù)用法詳解

    linux shell命令行參數(shù)用法詳解

    本文介紹了linux shell命令行參數(shù)的具體用法,用戶登錄到Linux系統(tǒng)時,可以看到一個shell提示符,標(biāo)識了命令行的開始。用戶可以在提示符后面輸入任何命令及參數(shù)。
    2014-04-04
  • 更改linux用戶登錄shell的操作方法

    更改linux用戶登錄shell的操作方法

    這篇文章主要為大家分享了更改linux用戶登錄shell的方法,感興趣的朋友可以參考下
    2013-11-11
  • Linux find命令及實(shí)用示例詳解

    Linux find命令及實(shí)用示例詳解

    Linux系統(tǒng)中的find命令是用于搜索文件和執(zhí)行操作的強(qiáng)大工具,通過指定搜索路徑和條件,用戶可以查找特定文件名、類型、權(quán)限等,并執(zhí)行如打印路徑、刪除文件等操作,文章通過多個示例,展示了find命令在實(shí)際應(yīng)用中的用法,感興趣的朋友一起看看吧
    2024-10-10
  • Shell腳本配合iptables屏蔽來自某個國家的IP訪問

    Shell腳本配合iptables屏蔽來自某個國家的IP訪問

    這篇文章主要介紹了Shell腳本配合iptables屏蔽來自某個國家的IP訪問,本文利用IPdeny的IP數(shù)據(jù),然后用Shell腳本導(dǎo)入iptables實(shí)現(xiàn)屏蔽IP訪問,需要的朋友可以參考下
    2015-04-04
  • Shell腳本中的echo命令使用介紹

    Shell腳本中的echo命令使用介紹

    這篇文章主要為大家介紹了Shell腳本中的echo命令使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • shell腳本快速創(chuàng)建格式化磁盤與詳細(xì)操作步驟

    shell腳本快速創(chuàng)建格式化磁盤與詳細(xì)操作步驟

    這篇文章主要介紹了shell腳本快速創(chuàng)建格式化磁盤與詳細(xì)操作步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • shell字符串比較判斷是否為數(shù)字

    shell字符串比較判斷是否為數(shù)字

    本文闡述:shell中整數(shù)比較方法及字符串的比較方法,如等于,不等于,大于,大于等于,小于,等等
    2013-01-01
  • linux shell 腳本實(shí)現(xiàn)tcp/upd協(xié)議通訊(重定向應(yīng)用)

    linux shell 腳本實(shí)現(xiàn)tcp/upd協(xié)議通訊(重定向應(yīng)用)

    這篇文章主要介紹了linux shell 腳本實(shí)現(xiàn)tcp/upd協(xié)議通訊(重定向應(yīng)用),需要的朋友可以參考下
    2015-10-10
  • shell腳本ssh遠(yuǎn)程執(zhí)行命令給變量賦值的問題解決

    shell腳本ssh遠(yuǎn)程執(zhí)行命令給變量賦值的問題解決

    本文主要介紹了shell腳本ssh遠(yuǎn)程執(zhí)行命令給變量賦值的問題解決,就是從A機(jī)器通過SSH方式到B機(jī)器,并執(zhí)行相關(guān)的命令,具有一定的參考價值,感興趣的可以了解一下
    2023-07-07

最新評論