在 bash shell 中,即使脚本由源、链接、./... 等调用,我也可以获得脚本的完整路径。这些神奇的 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 shell 中获取相同条件下的脚本路径?这些“魔法线”是什么?
PS这不是这个问题和类似问题的重复。我知道$0
。