了解linux shell脚本表达式

时间:2013-05-13 20:57:32

标签: linux shell

您好我有一个小脚本(脚本名称为:test1.sh),看起来像这样

PRG=$0
data=`expr $PRG : '.*\/.*'`
echo $data

当我运行时,我将输出视为

  

10

我无法理解脚本第二行中写的正则表达式。 那是什么意思?

2 个答案:

答案 0 :(得分:2)

From

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.*部分匹配。