Module: Scripto::RunCommands
Defined Under Namespace
Classes: CommandLine, Error
Instance Method Summary collapse
-
#run(command, args = nil) ⇒ Object
Run an external command.
-
#run_capture(command, args = nil) ⇒ Object
Run a command and capture the output like backticks.
-
#run_fails?(command, args = nil) ⇒ Boolean
Returns true if the command fails.
-
#run_quietly(command, args = nil) ⇒ Object
Run a command and suppress output by redirecting to /dev/null.
-
#run_succeeds?(command, args = nil) ⇒ Boolean
Returns true if the command succeeds.
-
#shellescape(str) ⇒ Object
Escape str if necessary.
Instance Method Details
#run(command, args = nil) ⇒ Object
Run an external command. Raise Error if something goes wrong. The command will be echoed if verbose?.
Usage is similar to Kernel#system. If args
is nil, command
will be passed to the shell. If args
are included, the command
and args
will be run directly without the shell.
16 17 18 19 20 |
# File 'lib/scripto/run_commands.rb', line 16 def run(command, args = nil) cmd = CommandLine.new(command, args) vputs(cmd) cmd.run end |
#run_capture(command, args = nil) ⇒ Object
Run a command and capture the output like backticks. See #run for details.
24 25 26 |
# File 'lib/scripto/run_commands.rb', line 24 def run_capture(command, args = nil) CommandLine.new(command, args).capture end |
#run_fails?(command, args = nil) ⇒ Boolean
Returns true if the command fails. See #run for details.
44 45 46 |
# File 'lib/scripto/run_commands.rb', line 44 def run_fails?(command, args = nil) !run_succeeds?(command, args) end |
#run_quietly(command, args = nil) ⇒ Object
Run a command and suppress output by redirecting to /dev/null. See #run for details.
30 31 32 33 |
# File 'lib/scripto/run_commands.rb', line 30 def run_quietly(command, args = nil) cmd = CommandLine.new(command, args) run("#{cmd} > /dev/null 2> /dev/null") end |
#run_succeeds?(command, args = nil) ⇒ Boolean
Returns true if the command succeeds. See #run for details.
36 37 38 39 40 41 |
# File 'lib/scripto/run_commands.rb', line 36 def run_succeeds?(command, args = nil) run_quietly(command, args) true rescue Error false end |
#shellescape(str) ⇒ Object
Escape str if necessary. Useful for passing arguments to a shell.
49 50 51 |
# File 'lib/scripto/run_commands.rb', line 49 def shellescape(str) Shellwords.escape(str) end |