Home > database >  how to check if bash function exist on remote computer
how to check if bash function exist on remote computer

Time:12-16

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 [email protected]]"
read ip
ip=${ip:[email protected]}
    
$(ssh $ip "[ '$(type -t $1)' = function ]")
if [ $? -eq 0 ]; then
  echo "function exist"
else
  echo 'function doesnt exist'
fi

CodePudding user response:

$(...)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

CodePudding user response:

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 [email protected]]"
  read ip
  ip=${ip:[email protected]}

  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"
  • Related