我有三个脚本:
-- bin
------ start.sh
-- scripts
------ directory_a
------------ directory_b
------------------ script_1.sh
------------------ script_2.sh
start.sh 运行 script_1.sh ,然后 script_1.sh 运行 script_2.sh
问题在于路径:
start.sh:
path_to_script="../scripts/directory_a/directory_b/script_1.sh";
command="$path_to_script param1 param2";
result=`$command`;
script_1.sh:
path_to_script="./script_2.sh";
command="$path_to_script param1 param2";
result=`$command`;
问题是 script_1.sh 正在错误的目录中查找 script_2.sh : 的 /bin/script_2.sh
我不想通过传递这样的完整路径来修复它(因为'directory_a'名称可以改变):
script_1.sh:
path_to_script="../scripts/directory_a/directory_b/script_2.sh";
command="$path_to_script param1 param2";
result=`$command`;
答案 0 :(得分:0)
BASEDIR =
脚本start.sh上的执行:
basedir= $(dirname $0)
pushd $basedir
absoluteBasedir=$(pwd)
popd
export absoluteBasedir
script1=$(absoluteBasedir)/scripts/directory_a/directory_b/script1.sh
脚本1上的if [[ -z $absoluteBasedir ]]
then
pushd ../../../
absoluteBasedir=$(pwd)
popd
end
script2=$(absoluteBasedir)/scripts/directory_a/directory_b/script2.sh
一切都已完成;)