我有一个来自脚本的函数,它调用另一个脚本。我想在其他几个脚本中调用它,这些脚本位于不同的目录中。
[nir@dev nir]$ ll -R
.:
total 8
-rwxr-xr-x 1 nir nir 44 Jul 2 08:41 a.sh
drwxrwxr-x 2 nir nir 18 Jul 2 08:41 b
drwxrwxr-x 2 nir nir 20 Jul 2 08:42 lib
-rwxr-xr-x 1 nir nir 13 Jul 2 08:39 run_me.sh
./b:
total 4
-rwxr-xr-x 1 nir nir 45 Jul 2 08:41 a.sh
./lib:
total 4
-rwxr-xr-x 1 nir nir 46 Jul 2 08:42 lib.sh
[nir@dev nir]$ pwd
/tmp/nir
[nir@dev nir]$ cat run_me.sh
echo working
[nir@dev nir]$ cat lib/lib.sh
run_me(){
echo $(realpath $0)
./run_me.sh
}
[nir@dev nir]$ cat a.sh
cd ` dirname "$0" `
. ./lib/lib.sh
run_me
[nir@dev nir]$ cat b/a.sh
cd ` dirname "$0" `
. ../lib/lib.sh
run_me
[nir@dev nir]$ ./a.sh
/tmp/nir/a.sh
working
[nir@dev nir]$ ./b/a.sh
realpath: ‘./b/a.sh’: No such file or directory
../lib/lib.sh: line 4: ./run_me.sh: No such file or directory
除了在run_me.sh
中移动函数外,我在lib.sh
中修复它的选项是什么?
是我在lib.sh中修复它的选项吗?
相对于lib.sh
调用它。例如:
run_me() {
"${BASH_SOURCE%/*}"/run_me.sh
}
您也可以将lib.sh
的路径添加到PATH
,然后让Bash-serach为其指定路径