您好我有一个小脚本(脚本名称为:test1.sh),看起来像这样
PRG=$0
data=`expr $PRG : '.*\/.*'`
echo $data
当我运行时,我将输出视为
10
我无法理解脚本第二行中写的正则表达式。 那是什么意思?
答案 0 :(得分:2)
If the match succeeds the `:' expression returns the number of characters matched.
所以10可能是
./test1.sh ^^^^^^^^^^ ||||||||| \ 123456789 10
答案 1 :(得分:1)
如果脚本(/
)的相对文件名中有$0 in sh
,则表达式返回非零值。如果你执行这样的脚本:sh ../../script.sh
,它输出15,这是“../../script.sh”的总长度。它将“../../”与'.*\/
匹配,并将script.sh
与.*
部分匹配。