run unix commands on ruby window - ruby

I have a shell script written by Unix commands, and now I want to run this script file (*.sh) on Ruby command prompt window. Anybody please guide me a solution ? many thanks.

Use backticks or the %x{...} syntax to execute external programs:
output = %x{./tests.sh}
See http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-60

Related

Ruby: How to open .exe file(that open CMD) and run command init

I am trying using ruby script to a task.
I have an .exe file that i want to run.
when opening this file it open in CMD and i can pass commands to it.
That file located in C:\temp\test.exe
I need to go to the directory and then open the file and then insert to it command like:
"getobject" task = "aa"
this program will give me the result to the CMD.
i will need to copy the result to text but i think i can handle it late.
I tried to search it online cant found anything.
Thanks
If you want to open an executable, usually you can use the `command` syntax in Ruby. So call:
`C:\temp\test.exe`
That should run the executable from the Ruby script. Then you can interact with that executable as if you ran it from a CMD instead of a Ruby file.
In order to run and capture the output of a command you'll need to use a system command. There are many system commands that you can use, but my preference is Open3:
require 'open3'
output, status = Open3.capture2("C:\temp\test.exe")
In the event that you want to pass command line arguments to capture2 you'll want to write that like this: Open3.capture2("C:\temp\test.exe", "arg1", "arg2"). That is the safest way to pass arguments.
I think what you are looking for is input/ output redirection
check redirection
Not tested
system 'C:\temp\test.exe < "\"getobject\" task = \"aa\""'

How to extract information from a command prompt using shell script?

I need a shell script using which I can fetch data from command prompt. I need to fetch data from the command prompt of a router. When I write commands in a shell script it goes the prompt but not executing the next command. So running the script just stuck in the prompt. Bellow is my script file
#!/bin/sh
ccli
rsc
where ccli is the command to enter the prompt and rsc is the command to fetch some infomation.
So please suggest some method.
If ccli reads commands from stdin (which I don't know), you might get further with
printf 'rsc\n' | ccli
For more complicated tasks I suggest you look into expect which was invented for the sole reason of driving interactive programs in a scripted way.

Converting Bash shell scripts to batch files

I am trying to convert a shell script to a batch file line by line and command by command. However, I cannot seem to get around the following line. Any help appreciated.
OPTS="%OPTS -Dlog4j.configuration=file:.\log4j.properties"
I don't know linux shell but I think the equivalent is this:
Set "OPTS=-Dlog4j.configuration=.\log4j.properties"
Then you can load the stored ...Options¿? like an argument:
Start Application.exe %OPTS%
".\" means the current directory of your script, ensure if "file:.\" means the same in linux OS.

how to send an external command to external program in bash?

I have an app installed on solaris which have its own specific commands,normally I have run below commands one by one in terminal:
eaw DDBSC1
(then the prompt will change to <)
< rldep:Cell=all;
I am looking to a script to put it in bash file and simply run it,
I have tried "expect" but it seems the bash script is getting stocked in "eaw DDBSC1" line and is not executing my second line,
Can anyone help?
Use a bash 'here document'. The commands following << ! will be interpreted by the shell until the following !end string.
eaw DDBSC1 << !end
rldep:Cell=all;
!end

Ruby: How to issue CLI commands on Windows

I'm new to Ruby, so this may be a pretty basic question.
I have a Windows batch file that I use all the time to interface with my source control system . The batch file issues various command-line commands supported by the source control system's command line interface (CLI).
I'd like to write a Ruby program that issues some of these commands. In general, how do you issue command-line commands from a Ruby program on Windows?
Thanks!
to run a system (command line) command in ruby wrap it with `
for example
puts `dir`
will run the cmd window dir command
if you need the return value (ERRORLEVEL) you can use the system command
for example system("dir") which return a true for success and false for failure the ERRORLEVEL value is stored at $?
task :build do
command_line = "gcc ..."
`#{command_line}`
end
http://rubyonwindows.blogspot.com/2007/05/automating-windows-shell-with-ruby.html
and for scripting:
http://en.wikipedia.org/wiki/Windows_PowerShell

Resources