Module: Sox::Shell

Included in:
Cmd, Combiner::BaseStrategy
Defined in:
lib/sox/shell.rb

Overview

Provides methods to run the command in the /bin/sh and /bin/bash interpreters. Ruby’s system method runs /bin/sh which doesn’t support the process substitution feature. So sometimes we need to use bash with process substitution in order to avoid disk IO operations.

Also this module takes care of error handling and raises Error when a failure message is returned by the shell command.

See en.wikipedia.org/wiki/Process_substitution

Constant Summary collapse

BASH_PATH =

Path to the bash interpreter:

'/bin/bash'

Instance Method Summary collapse

Instance Method Details

#bash(command) ⇒ Boolean

Run bash command.



40
41
42
43
# File 'lib/sox/shell.rb', line 40

def bash(command)
  bash_command = "#{BASH_PATH} -c #{Shellwords.escape(command)}"
  sh(bash_command)
end

#sh(command) ⇒ Boolean

Run a shell command.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sox/shell.rb', line 20

def sh(command)
  _, _, err_io, thread = Open3.popen3(command)
  thread.join

  process_status = thread.value
  if process_status.success?
    true
  else
    raise Error, err_io.read
  end
rescue Errno::ENOENT => err
  msg = "#{err.message}. Do you have `#{SOX_COMMAND}' installed?"
  raise Error, msg
end