简体   繁体   English

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

[英]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.

相关问题 如何检查bash脚本目录中是否存在与正则表达式匹配的文件? - How to check if files matching a regex exist in a directory in bash script? Bash:检查脚本是否已在此计算机上运行 - Bash: Check if script already ran on this computer 如何编写检查 bash 命令结果的 function - How to write a function that check the result of a bash command 通过SSH在远程计算机上运行bash脚本时,如何在本地计算机上触发声音? - How do I get a sound to fire on local machine when I run a bash script on a remote computer via SSH? 如何在Linux bash中如何检查脚本文件是否存在并在一个衬套中执行 - How in Linux bash how to check if script file exist and execute it in one liner 如何检查 javascript 文件是否存在而不在 bash 或 python 中下载 - How can I check if a javascript file exist without downloading it in bash or python 如何通过名称在远程计算机上查找库的位置 - how to find the position of a library by name on a remote computer 通过ansible调用远程bash功能 - Call remote bash function via ansible Bash:如果文件不存在则创建一个文件,否则检查它是否可写 - Bash: Create a file if it does not exist, otherwise check to see if it is writeable Bash脚本无法正确检查目录和文件是否存在 - Bash Script does not Check Properly if Directory and File Exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM