[英]Understanding linux shell script expression
嗨,我有一個小腳本(腳本名稱是:test1.sh),看起來像這樣
PRG=$0
data=`expr $PRG : '.*\/.*'`
echo $data
當我運行這個時,我看到輸出為
10
我無法理解腳本第二行中寫的正則表達式。 那是什么意思?
If the match succeeds the `:' expression returns the number of characters matched.
所以10很可能
./test1.sh ^^^^^^^^^^ ||||||||| \ 123456789 10
如果腳本的相對文件名中有/
,則表達式返回非零值( $0 in sh
)。 如果你執行這樣的腳本: sh ../../script.sh
,它輸出15,這是“../../script.sh”的總長度。 它將“../../”與'.*\\/
匹配, script.sh
與.*
部分匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.