Подстановка параметров сценария оболочки - PullRequest
3 голосов
/ 11 августа 2010

Может кто-нибудь сказать мне, в чем разница между этими двумя соглашениями для получения имени запущенного в данный момент скрипта?

#!/bin/sh

echo $0
echo ${0##*/}

Вторая версия удаляет путь из имени скрипта?

Спасибо!

Ответы [ 2 ]

2 голосов
/ 11 августа 2010

Ваше предположение верно. Из bash документов на моей машине:

   ${parameter#word}
   ${parameter##word}
          The  word  is  expanded to produce a pattern just as in pathname
          expansion.  If the pattern matches the beginning of the value of
          parameter,  then  the  result  of  the expansion is the expanded
          value of parameter with the shortest matching pattern (the ``#''
          case) or the longest matching pattern (the ``##'' case) deleted.
          If parameter is @ or *, the pattern removal operation is applied
          to  each  positional parameter in turn, and the expansion is the
          resultant list.  If parameter is an array  variable  subscripted
          with  @  or  *, the pattern removal operation is applied to each
          member of the array in turn, and the expansion is the  resultant
          list.
1 голос
/ 11 августа 2010

Да, вторая версия использует расширение bash, чтобы вырезать самый длинный префикс в $ 0, который соответствует * / regexp, поэтому, если $ 0 это "/ bin / bash", то $ {0 ## * /} будет "bash". Вы можете использовать `basename $ 0` для достижения того же результата.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...