Run a shell script from ruby

I am testing iOS using Frank. It is a ruby ​​pearl that also uses cucumbers. I have a β€œgiven” step that checks if the application is running or if it crashed. If my step finds that it crashed, I would like to start the application again. I launch the application using a shell script that is stored somewhere next to the eyepiece files.

How can I call a script from this step definition?

+4
source share
3 answers

You can do this in several ways.

Kernel.system "command" %x[command] `command` 
+3
source

Like the other answers, there are many ways to execute shell scripts from Ruby, but they are not all created equal. I will try to explain all the methods that I know in more detail.

Backticks

 `command arg1 arg2` %x(command arg1 arg2) 

Runs a command in a subshell and returns the result of the command. The command and its arguments are provided as a string limited to reverse windows. An alternative syntax is %x(...) , which helps to avoid escaping problems, for example. when you want to execute a command containing inverse elements. The brackets can be replaced with other delimiters, such as [] , {} , !! , ... to be able to get around almost any shielding problem.

Stderr is printed as usual, stdout is suppressed. Returns the output of the stdout command. This means that you can use backward exposure to get the output of the command into a variable for further processing.

Kernel.exec

 exec("command arg1 arg2") exec("command", "arg1", "arg2") 

Replaces the current process by running the command. The command and its arguments are provided as a regular line or as a list of lines separated by commas. It may be useful if you already have an array of arguments. The output remains as it is, i.e. It will be printed on the console as if the command was being executed directly.

Kernel.system

 system("command arg1 arg2") system("command","arg1 arg2") 

Like Kernel.exec , but starts up again in a subshell. Returns true if the process completed correctly (status 0), false otherwise. This works fine inside if -statements.

Kernel.spawn

 pid = spawn("command") # do other stuff Process.wait(pid) 

Similar to Kernel.system , but spawns a child process to execute the specified command. Thus, the parent process does not Process.wait for the completion of the command if Process.wait is not used. The return value is the PID of the spawned process.

IO.popen

 io = IO.popen("command") IO.popen("command") {|io| ... } 

Repeats a command in a child process, but allows more efficient management of IO. The child processes stdout and stdin are associated with an IO object that is either available as a return value or a block parameter. If obtained via the return value, the IO object should be closed after use with io.close .

+1
source

Here are some good ways . Backticks are probably the least intrusive. But be careful: as the tadman pointed out, exec terminates the calling process, which can be prevented by creating a child process or using system .

0
source

Source: https://habr.com/ru/post/1494125/


All Articles