Как получить путь к файлу сценария в самом сценарии при вызове через символьную ссылку

Об этой известной ошибке сообщается здесь -> https://code.google.com/p/android/issues/detail?id=188627

Обходной путь на Mac перезагрузить компьютер.

8
задан Charles 11 February 2012 в 21:54
поделиться

3 ответа

if [ -L $0 ] ; then
    DIR=$(dirname $(readlink -f $0)) ;
else
    DIR=$(dirname $0) ;
fi ;
15
ответ дан 5 December 2019 в 04:58
поделиться

You really need to read the following BashFAQ article:

The truth is, the $0 hacks are NOT reliable, and they will fail whenever applications are started with a zeroth argument that is not the application's path. For example, login(1) will put a - in front of your application name in $0, causing breakage, and whenever you invoke an application that's in PATH $0 won't contain the path to your application but just the filename, which means you can't extract any location information out of it. There are a LOT more ways in which this can fail.

Bottom line: Don't rely on the hack, the truth is you cannot figure out where your script is, so use a configuration option to set the directory of your config files or whatever you need the script's path for.

15
ответ дан 5 December 2019 в 04:58
поделиться

Use readlink for that:

readlink -f "$0"
6
ответ дан 5 December 2019 в 04:58
поделиться
Другие вопросы по тегам:

Похожие вопросы: