使用相对路径从另一个脚本调用一个shell脚本

Pro*_*mer 6 csh

我有一个这样的脚本:

#!/bin/csh
echo "This is the main programme"
./printsth
Run Code Online (Sandbox Code Playgroud)

我想printsth使用相对路径从此脚本中调用脚本.有办法吗?通过相对路径,我的意思是相对于我的调用脚本所在的路径.

Joe*_*e Z 7

您可以使用$cwd. 因此,如果您想printsth使用相对于当前工作目录的路径进行调用,请以$cwd.

例如,如果要调用printsth当前目录中的 ,请说:

$cwd/printsth
Run Code Online (Sandbox Code Playgroud)

如果要调用printsth上面的一个目录:

$cwd/../printsth
Run Code Online (Sandbox Code Playgroud)

确保它是一个csh脚本(即第一行是#!/bin/csh)。如果是shorbash脚本,则需要使用$PWD(对于“当前工作目录”),而不是$cwd.

编辑:

如果你想要一个相对于脚本目录的目录,而不是当前工作目录,那么你可以这样做:

setenv SCRIPTDIR `dirname $0`
$SCRIPTDIR/printsth
Run Code Online (Sandbox Code Playgroud)

这将设置$SCRIPTDIR为与原始脚本相同的目录。然后,您可以构建相对于该路径的路径。