Jeffrey C Jeffrey C - 1 year ago 74
Bash Question

How to call multiple bash command and ssh in Ruby?

I want to do the following commands in ruby.

  1. ssh into another computer using
    ssh example@example

  2. set source file
    source ~/.profile

  3. cd to/some/folder

  4. call my shell script with parameters, a json formatted string ,
    ./ my_hash.to_json

However I am facing these problems:
I call them in one line using backticks, it works, but it is a very bad practice in my opinion because it is not readable nor it is maintainable.

On the other hand, when I call
, the resulted string has non-escaped double quotes, How do I escape them?

Answer Source

I would recommend to view this tutorial for ssh with ruby. then make a shell script and move it to server and then execute like a single command.

create a single shell script file for example script1 and then execute it at once instead of executing each command individually.

open file script1 using any editor.

copy all commands to script1 (each command in new line).

script1 file should look like this


ssh example@example
source ~/.profile
cd to/some/folder

save file

make this file executable using chmod +x script

execute it in ruby like this [backtick]./script1[backtick]

note: copy script1 to usr/bin to avoid "./" and then try command only script1.

Reference for passing arguments in shell script is here.