如何检查远程计算机上是否存在bash函数



我读过关于类似主题的各种答案,但我仍然无法处理我的问题。也就是说,在远程计算机上,我有一个.bashrc文件,其中包含一堆自定义函数。我想检查该文件中是否存在该函数。只是为了补充一点,脚本不断报告远程计算机上有指定的函数,即使它不是。这就是我迄今为止所做的:

echo "Enter IP addres of the remote PC [def host@XX.XX.XX.XX]"
read ip
ip=${ip:-host@XX.XX.XX.XX}

$(ssh $ip "[ '$(type -t $1)' = function ]")
if [ $? -eq 0 ]; then
echo "function exist"
else
echo 'function doesnt exist'
fi

$(...)"引号内局部展开。重新研究单引号和双引号之间的区别。

the_function_you_want_to_check=something
ssh "$ip" '[ "$(type -t "'$the_function_you_want_to_check'")" = function ]' 

请勿使用$?。只是:

if ssh stuff...; then
echo yes
else
echo no
fi

感谢您的及时回复。请注意,$1实际上是我在本地计算机上运行的bash函数的第一个参数。现在,您建议的更改报告远程计算机上没有任何功能,即使它存在。我在本地机器上运行的更完整的功能是:

appendFunction_to_remotePC(){
echo "Enter the IP addres of the PC [def host@XX.XX.XX.XX]"
read ip
ip=${ip:-host@XX.XX.XX.XX}
if ssh "$ip" '[ "$(type -t "'$1'")" = function ]'; then
echo yes
else
echo no
fi

}

我用通常的方式调用本地计算机上的函数:

$ appendFunction_to_remotePC "test"

最新更新