mirror of
https://github.com/jbranchaud/til
synced 2026-03-04 06:58:45 +00:00
Add Check Return Status Of Running A Shell Command as a ruby til
This commit is contained in:
33
ruby/check-return-status-of-running-a-shell-command.md
Normal file
33
ruby/check-return-status-of-running-a-shell-command.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Check Return Status Of Running A Shell Command
|
||||
|
||||
There are [many
|
||||
ways](http://tech.natemurray.com/2007/03/ruby-shell-commands.html) to run a
|
||||
shell command in a Ruby context. One of the most common is to place the command
|
||||
inside backticks. It runs the command in a subshell.
|
||||
|
||||
The standard output (`stdout`) from running the command is the return value of
|
||||
the statement. Ruby also captures details about the previously run command in
|
||||
the global `$?` variable.
|
||||
|
||||
|
||||
```ruby
|
||||
> `true`
|
||||
=> ""
|
||||
> $?.exitstatus
|
||||
=> 0
|
||||
|
||||
> `false`
|
||||
=> ""
|
||||
> $?.exitstatus
|
||||
=> 1
|
||||
|
||||
> `echo 'hello' && exit 2`
|
||||
=> "hello\n"
|
||||
> $?
|
||||
=> #<Process::Status: pid 12284 exit 2>
|
||||
> $?.exitstatus
|
||||
=> 2
|
||||
```
|
||||
|
||||
The value captured in `$?` is a `Process::Status` object. It can tell us the
|
||||
exit status of the process with `#exitstatus`.
|
||||
Reference in New Issue
Block a user