bash シェルでは、スクリプトがソース、リンク、./... などによって呼び出された場合でも、スクリプトのフル パスを取得できます。これらの魔法の bash 行は次のとおりです。
#Next lines just find the path of the file.
#Works for all scenarios including:
#when called via multiple soft links.
#when script called by command "source" aka . (dot) operator.
#when arg $0 is modified from caller.
#"./script" "/full/path/to/script" "/some/path/../../another/path/script" "./some/folder/script"
#SCRIPT_PATH is given in full path, no matter how it is called.
#Just make sure you locate this at start of the script.
SCRIPT_PATH="${BASH_SOURCE[0]}";
if [ -h "${SCRIPT_PATH}" ]; then
while [ -h "${SCRIPT_PATH}" ]; do SCRIPT_PATH=`readlink "${SCRIPT_PATH}"`; done
fi
pushd `dirname ${SCRIPT_PATH}` > /dev/null
SCRIPT_PATH=`pwd`;
popd > /dev/null
TCSH シェルで同じ条件でスクリプト パスを取得するにはどうすればよいですか? これらの「魔法の線」とは何ですか?
PSこれと同様の質問の複製ではありません。私は知ってい$0
ます。