Operating System or Shell commands

suggest change

Introduction

There are many ways to interact with the operating system. From within Ruby you can run shell/system commands or sub-processes.

Remarks

Exec:

Exec is very limited in functionality and when executed will exit the Ruby program and run the command.

The System Command:

Kernel#system runs the command in a subprocess and waits until it finishes. It returns true when the status is 0, false when the process exits non-zero, and nil when the command cannot be started. A non-zero exit does not raise. Standard output is not returned. $? holds the status.

The backticks (`):

The backtick character is usually on the same key as ~. Backticks, and %x{}, run the command in a subprocess and wait. They return standard output as a string. A non-zero exit does not raise. Check $? for the status.

IO.popen:

IO.popen runs in a sub-process. Here the sub-process standard input and standard output are connected to the IO object.

Popen3:

Popen3 allows you to access the standard input, standard output and standard error.

The subprocess’s standard input and output will be returned into IO objects.

$? (same as $CHILD_STATUS)

Can be used with the backticks, system() or %x{} operations and will give the status of the last system executed command.

This is useful for the exitstatus and pid of that status.

$?.exitstatus

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents