Shell Script 中 Regular Expression - Linux

Puput avatar
By Puput
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

--
Tags: Linux

All Comments

請問routing的真正運作方式

Agatha avatar
By Agatha
at 2008-11-21T16:03
請教高手一下 假設有一台機器是pppoe上網然後去撥接某個pptp server 那麼封包在機器內各介面的流程就變成 ppp1(pptp) --andgt; ppp0(pppoe) --andgt; eth0 --andgt; 網路 那麼 routing 每兩個介面之間就會查一下嗎? 還有 iptable ...

Shell Script 中 Regular Expression

Wallis avatar
By Wallis
at 2008-11-21T15:50
※ 引述《appleboy46 (小惡魔)》之銘言: : ※ 引述《godman362 (Shizuru)》之銘言: : : 我是最近才開始寫shell script : : 所以若是問題太過簡單還請各位見諒 : : 我想在script中用if去判斷輸入的檔案名稱的副檔名 : : 在根據副檔名做不同的處理 : ...

Shell Script 中 Regular Expression

Edith avatar
By Edith
at 2008-11-21T15:31
※ 引述《godman362 (Shizuru)》之銘言: : 我是最近才開始寫shell script : 所以若是問題太過簡單還請各位見諒 : 我想在script中用if去判斷輸入的檔案名稱的副檔名 : 在根據副檔名做不同的處理 : 我目前大概是這樣想的,大致如下: : if [ $1 =~ [a-zA- ...

Shell Script 中 Regular Expression

Tracy avatar
By Tracy
at 2008-11-21T14:55
我是最近才開始寫shell script 所以若是問題太過簡單還請各位見諒 我想在script中用if去判斷輸入的檔案名稱的副檔名 在根據副檔名做不同的處理 我目前大概是這樣想的,大致如下: if [ $1 =~ [a-zA-Z]\.cpp ]; then echo and#34;CPPa ...

ld的問題

Megan avatar
By Megan
at 2008-11-21T12:41
我寫了一個簡單的pthread problem 通常是要用 $andgt;cc -lpthread pthread.c 來產生executable 那如果我想link時用ld的話 $andgt;cc -c pthread.c $andgt;ld --entry=main -lpthread pthre ...