0

I have a script test.sh and I am trying to ssh and call that scrip't function in the same script:

#!/bin/sh

testme()
{
   echo "hello world"
}

ssh myserver "/opt/scripts/test.sh; testme"

But I keep getting testme command not found

What is the correct way of calling a function from a script after ssh?

codec
  • 7,978
  • 26
  • 71
  • 127
  • 2
    have you looked at this? https://stackoverflow.com/q/305035/2055887 – m79lkm Mar 11 '19 at 15:56
  • 1
    You need to *source* the script, not execute it, so that the function is defined in the current shell. – chepner Mar 11 '19 at 15:58
  • Also, is the script actually *on* the remote host, or just the local host? In general, you can't "send" function definitions over the `ssh` connection. – chepner Mar 11 '19 at 15:59
  • try ssh remoteServer '/bin/bash -x -s ' < <(cat /path/on/local/server/test.sh; echo "testme") – Lety Mar 11 '19 at 16:10

1 Answers1

1

If you use Bash on both sides, you can have it serialize the function for you:

#!/bin/bash

testme()
{
   echo "hello world"
}

ssh myserver "$(declare -f testme); testme"

If you need sh compatibility, this is not an option.

that other guy
  • 116,971
  • 11
  • 170
  • 194