2

I am running a bash script, but when I try to run functions on a remote machine, it says

bash: keyConfig: command not found

Here is my script:

keyConfig() {
    sed -i.bak -r "/^$1/s/([^']+')([^']+)('.*)/\1$2\3/" $3
}

remoteExecution() {
    ssh ppuser@10.101.5.91 " keyConfig $1 $2 $4 "
}

remoteExecution
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
Madura Dissanayake
  • 8,309
  • 5
  • 25
  • 34
  • +1 for well formed question. You can send a function definition "over-the-wire" thru ssh, and then execute it, but it's not really for the faint-of-heart. You'll quickly run into issues with quoting. And your sed function, filled every shell meta-char and quoting char is a perfect example of a quoting nightmare. Its possible, but fragile, and time consuming to debug and get right. You could use `scp myFunc user@remotehost:/tmp/myFunc` (as a file), and execute from /tmp. Good luck. – shellter Apr 24 '14 at 09:27

3 Answers3

6

Simple work-around:

remoteExecution() {
    ssh ppuser@10.101.5.91 "`declare -f keyConfig`; keyConfig $1 $2 $4"
}

Here, keyConfig only calls sed command, which is available on remote system. If keyConfig had been calling any local function, then add that function also in declare -f's command line.

This way, the function keyConfig in the local shell gets defined in the remote shell spawned via ssh & then it gets called.

anishsane
  • 20,270
  • 5
  • 40
  • 73
  • Hello @anishsane, Thank You for your help... I tried with this but my problem is after declaring this function as u told, how to get the command line arguments from my local file? – Madura Dissanayake Apr 25 '14 at 05:25
  • Declare keyConfig function as you are declaring now. modify remoteExecution function as mentioned in my answer. After this do you see any error, when you run `remoteExecution` function as you desire? – anishsane Apr 25 '14 at 07:06
1

keyConfig is not define as a command in the host "10.101.5.91"

You could try to define a bash script which is called keyConfig in the host "10.101.5.91" and add that script in the ppuser PATH.

Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
1

Resone for the error:

When you do

ssh ppuser@10.101.5.91 " keyConfig $1 $2 $4 "

You are actually trying to execute a command keyConfig on remote machine 10.101.5.91 Which is certainly not there:

2 Solution for the problem

1) Make a script on remotehost which contains keyConfig code with same name

OR

2) Execute following instead of function

ssh ppuser@10.101.5.91 "sed -i.bak -r "/^$1/s/([^']+')([^']+)('.*)/\1$2\3/" $3"

Please note you may have to add few escape depending on the sed syntax you are using

PradyJord
  • 2,136
  • 12
  • 19
  • Thank You for your help... it worked, but in your 2nd solution this part ("sed -i.bak -r "/^$1/s/([^']+')([^']+)('.*)/\1$2\3/" $3") was not worked then I made some changes because of the double quotes @middle, so I added some aditional back slashes(\) to that. Here is this :[ "sed -i.bak -r \"/^$1/s/([^']+')([^']+)('.*)/\1$2\3/\" $3" ] then it worked. Thank You... :) – Madura Dissanayake Apr 24 '14 at 11:13
  • just tried with the escape near to the double quoates, but it did not work. Any idea where to put the escape you have told on the answer latter part? – Madura Dissanayake Apr 24 '14 at 11:37
  • give me $1 $2 $3 and exact ssh line with sed to work with, please try and conclude in your next comment as SO doesn't allow long conversations in comment – PradyJord Apr 24 '14 at 11:43
  • That's way I said to add some back slashes to the sed command. I said like that because I tested with that. Then no need to worry about $1, $2, $3, those are commandline arguments. check my 1st comment. Thank You... – Madura Dissanayake Apr 24 '14 at 11:48