2

I would like to write a function in bash, then export that function and execute it over ssh. Is that possible, and if yes, how?

I tried

#!/bin/bash

function myfunc() {
    echo $1
}        
export -f myfunc

but this doesn't seem to work.

  • Can you explain it more? Are you trying to execute the function via calling the script remotely? – coffeMug Dec 2 '13 at 18:43
  • I want to use the script in GNU parallel. I saw a solution on superuser.com/questions/620782/… but I don't seem to get that to work. – MaVe Dec 2 '13 at 19:28
3

In the example that you mention in your comment it is parallel that transfers the function to the remote environment (and it works only bash). So you have to use parallel to try it. After defining and exporting (as per Q), you should:

function myfunc() {
    echo $1
}        
export -f myfunc
parallel --env myfunc -S server 'myfunc abc' ::: bar

There is a part in the tutorial about that.

The bash function forwarding feature with --env has been available starting with parallel version 20130722.

  • Yes, I know, but that doesn't work for me when I try it in bash. I each time get "bash: line 2: command not found". – MaVe Dec 2 '13 at 20:09
  • 1
    You should update your question with what you actually type into bash, and not a half finished script which doesn't show what you do with parallel. (as without parallel it will not work). – Zelda Dec 2 '13 at 20:14
  • I update the answer with the full 5 lines that work if cut-and-pasted into bash. The only thing you need to change is the "server" to your servername (or login@servername) – Zelda Dec 2 '13 at 20:16
  • If I try your suggested code, I still get > bash: line 2: myfunc: command not found – MaVe Dec 2 '13 at 20:49
  • 2
    If you look in the NEWS file in the gnu-parallel distribution you can read that the --env can transfer bash functions starting with parallel 20130722. You need to upgrade! – Zelda Dec 3 '13 at 8:01
0

You may use something similar to this,

file myfunc.sh

function myfunc() {
    echo $1
} 

Then source the function within your shell like,

source myfunc.sh

Then you may execute the "myfunc" on the remote node like so,

ssh user@ip "$(declare -f myfunc; myfunc 'cmdlinearg' )"

where "cmdlinearg" is the value of 1st command line variable that shall be echoed within the function.

Ref url: https://www.youtube.com/watch?v=uqHjc7hlqd0 towards the end

0

not sure if it works for all cases, it's kind of naughty:

function remote-function() {
   local param1=$1
   echo $param1
}
ssh_connection=someone@somewhere
echo $(type remote-cleanup | tail -n+2 | sed 's#^}#; }#g')'; remote-function hello '  | ssh ${ssh_connection} bash
0

This should work if your environment is not huge:

#!/bin/bash

myvar="This is my var: "
alias myecho=echo

function myfunc() {
    myecho $myvar $1
}        

env_parallel -S server myfunc ::: a b c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.