简体   繁体   中英

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_script.sh 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 my_hash.to_json , the resulted string has non-escaped double quotes, How do I escape them?

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

#!/bin/bash

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM