[英]how to check if bash function exist on remote computer
I read various answers on similar topic, but I still can't deal with my problem.我阅读了有关类似主题的各种答案,但我仍然无法解决我的问题。 Namely, on the remote computer I have a.bashrc file with a bunch of custom made functions.
即,在远程计算机上,我有一个 .bashrc 文件,其中包含一堆自定义功能。 I would like to check if that function exists in that file.
我想检查该文件中是否存在 function 。 Just to add that the script constantly reports that there is a specified function on the remote computer even though it is not.
只是要补充一点,该脚本不断报告远程计算机上存在指定的 function,即使它不是。 This is what I have done so far:
这是我到目前为止所做的:
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
$(...)
is expanded localy inside "
quotes. Reseach difference between single and double quotes. $(...)
在"
引号内局部扩展。研究单引号和双引号之间的区别。
the_function_you_want_to_check=something
ssh "$ip" '[ "$(type -t "'$the_function_you_want_to_check'")" = function ]'
Do not use $?
不要使用
$?
. . Just:
只是:
if ssh stuff...; then
echo yes
else
echo no
fi
Thank you for your prompt response.感谢您的及时回复。 Please note that $1 is actually the first parameter of the bash functions that I run on my local computer.
请注意,$1 实际上是我在本地计算机上运行的 bash 函数的第一个参数。 Now, the change you suggested reports that there is no function on the remote computer even though it exists.
现在,您建议的更改报告远程计算机上没有 function 即使它存在。 More complete function that I run on the local machine is:
我在本地机器上运行的更完整的 function 是:
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
}
I call the function on the local computer in the usual way:我以通常的方式在本地计算机上调用 function:
$ appendFunction_to_remotePC "test"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.