0

I have a bash function which takes an array as an argument and it executes multiple commands.

Based on user input I want to run all the commands in this method locally or on remote machine. It has many quotes in the commands and echoing with "" will become ugly.

This is how I am invoking the function right now:
     run_tool_commands "${ARGS[@]}"

function run_tool_commands {
     ARGS=("$@")
     .. Loads of commands here
}

if [ case 1 ]; then 
  # run locally 
else 
  # run remotely
fi

This seems helpful, but this is possible if I have the method text piped to "here document".

Community
  • 1
  • 1
user1071840
  • 3,220
  • 7
  • 43
  • 65
  • copy your script to remote machine using `scp`. Then login using `ssh` and run the script. – sujin Jan 27 '14 at 10:49
  • @sujin, which script? I just want to run a bunch of commands and unfortunately it doesn't look like I can separate them out to a script. – user1071840 Jan 27 '14 at 10:59
  • `login` over remote pc using `ssh` then you can run a all the commands . – sujin Jan 27 '14 at 11:00

1 Answers1

0

If

  1. all the commands that are to be executed under run_tool_commands are present on remote system as well,
  2. All commands are executables, & not alias/functions
  3. All these excutables are in default paths. (No need to source .bashrc or any other file on remote.)

Then perhaps this code may work: (not tested):

{ declare -f run_tool_commands; echo run_tool_commands "${ARGS[@]}"; } | ssh -t user@host

OR

{ declare -f run_tool_commands;
  echo -n run_tool_commands;
  for arg in "${ARGS[@]}"; do
      echo -ne "\"$t\" ";
  done; } | ssh -t user@host

Using for loop, to preserve quotes around arguments. (may or may not be required, not tested.)

anishsane
  • 17,770
  • 5
  • 33
  • 64