Shell Script 中 Regular Expression - Linux

By Puput
at 2008-11-21T17:02
at 2008-11-21T17:02
Table of Contents
※ 引述《godman362 (Shizuru)》之銘言:
: 我是最近才開始寫shell script
: 所以若是問題太過簡單還請各位見諒
: 我想在script中用if去判斷輸入的檔案名稱的副檔名
: 在根據副檔名做不同的處理
: 我目前大概是這樣想的,大致如下:
: if [ $1 =~ [a-zA-Z]\.cpp ]; then
regex 寫不好, 只有 match 到 {a..z}.cpp 和 {A..Z}.cpp
: echo "CPP"
: fi
: 但是判別出來一直是錯誤的
: 所以想請教一下,該如何在if判斷句中做修正
在 bash, 用 =~, 應該要用 [[ ]]
shopt -sq nocasematch
case ${1##.*} in
CPP)
echo CPP;;
*)
esac
--
: 我是最近才開始寫shell script
: 所以若是問題太過簡單還請各位見諒
: 我想在script中用if去判斷輸入的檔案名稱的副檔名
: 在根據副檔名做不同的處理
: 我目前大概是這樣想的,大致如下:
: if [ $1 =~ [a-zA-Z]\.cpp ]; then
regex 寫不好, 只有 match 到 {a..z}.cpp 和 {A..Z}.cpp
: echo "CPP"
: fi
: 但是判別出來一直是錯誤的
: 所以想請教一下,該如何在if判斷句中做修正
在 bash, 用 =~, 應該要用 [[ ]]
shopt -sq nocasematch
case ${1##.*} in
CPP)
echo CPP;;
*)
esac
--
Tags:
Linux
All Comments
Related Posts
請問routing的真正運作方式

By Agatha
at 2008-11-21T16:03
at 2008-11-21T16:03
Shell Script 中 Regular Expression

By Wallis
at 2008-11-21T15:50
at 2008-11-21T15:50
Shell Script 中 Regular Expression

By Edith
at 2008-11-21T15:31
at 2008-11-21T15:31
Shell Script 中 Regular Expression

By Tracy
at 2008-11-21T14:55
at 2008-11-21T14:55
ld的問題

By Megan
at 2008-11-21T12:41
at 2008-11-21T12:41