Class: Ukiryu::Shell::Sh

Inherits:
UnixBase show all
Defined in:
lib/ukiryu/shell/sh.rb

Overview

POSIX sh shell implementation

sh uses the same quoting and escaping rules as Bash.

Constant Summary collapse

SHELL_NAME =
:sh
EXECUTABLE =
'sh'

Constants inherited from UnixBase

UnixBase::PLATFORM

Constants inherited from Base

Base::PLATFORM, Base::SPECIAL_CHARS_PATTERN, Base::WHITESPACE_PATTERN

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from UnixBase

#execute_command, #execute_command_with_stdin, #shell_executable

Methods inherited from Base

#capabilities, #encoding, #environment_to_h, #execute_command, #execute_command_with_stdin, #format_environment, #format_path, #headless_environment, #needs_quoting?, #supports?

Class Method Details

.detect_alias(command_name) ⇒ Hash?

Detect if a command is a POSIX sh alias

POSIX sh has ‘type’ but may not have alias detection in all implementations.

Parameters:

  • command_name (String)

    the command to check

Returns:

  • (Hash, nil)

    “…”, target: “…” or nil if not an alias



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ukiryu/shell/sh.rb', line 18

def self.detect_alias(command_name)
  # POSIX sh has 'type' but may not have alias detection
  result = `type #{command_name} 2>/dev/null`
  return nil unless result

  if result =~ /^#{command_name} is aliased to `(.*)'`$/
    { definition: result.strip,
      target: ::Regexp.last_match(1) }
  end
  nil
end

Instance Method Details

#env_var(name) ⇒ String

Format an environment variable reference

Parameters:

  • name (String)

    the variable name

Returns:

  • (String)

    the formatted reference ($VAR)



55
56
57
# File 'lib/ukiryu/shell/sh.rb', line 55

def env_var(name)
  "$#{name}"
end

#escape(string) ⇒ Object

sh uses the same escaping as Bash



42
43
44
# File 'lib/ukiryu/shell/sh.rb', line 42

def escape(string)
  string.to_s.gsub("'") { "'\\''" }
end

#join(executable, *args) ⇒ String

Join executable and arguments into a command line

Parameters:

  • executable (String)

    the executable path

  • args (Array<String>)

    the arguments

Returns:

  • (String)

    the complete command line



64
65
66
# File 'lib/ukiryu/shell/sh.rb', line 64

def join(executable, *args)
  [quote(executable), *args.map { |a| quote(a) }].join(' ')
end

#nameObject



30
31
32
# File 'lib/ukiryu/shell/sh.rb', line 30

def name
  :sh
end

#quote(string) ⇒ Object

sh uses the same quoting as Bash



47
48
49
# File 'lib/ukiryu/shell/sh.rb', line 47

def quote(string)
  "'#{escape(string)}'"
end

#shell_commandString

Get the sh command name to search for

Returns:

  • (String)

    the sh command name



37
38
39
# File 'lib/ukiryu/shell/sh.rb', line 37

def shell_command
  'sh'
end