简体   繁体   中英

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. I would like to check if that function exists in that file. Just to add that the script constantly reports that there is a specified function on the remote computer even though it is not. 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. Now, the change you suggested reports that there is no function on the remote computer even though it exists. More complete function that I run on the local machine is:

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:

$ appendFunction_to_remotePC "test"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM