Class: Ukiryu::Shell::Sh
- 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
Constants inherited from Base
Base::PLATFORM, Base::SPECIAL_CHARS_PATTERN, Base::WHITESPACE_PATTERN
Class Method Summary collapse
-
.detect_alias(command_name) ⇒ Hash?
Detect if a command is a POSIX sh alias.
Instance Method Summary collapse
-
#env_var(name) ⇒ String
Format an environment variable reference.
-
#escape(string) ⇒ Object
sh uses the same escaping as Bash.
-
#join(executable, *args) ⇒ String
Join executable and arguments into a command line.
- #name ⇒ Object
-
#quote(string) ⇒ Object
sh uses the same quoting as Bash.
-
#shell_command ⇒ String
Get the sh command name to search for.
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.
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
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
64 65 66 |
# File 'lib/ukiryu/shell/sh.rb', line 64 def join(executable, *args) [quote(executable), *args.map { |a| quote(a) }].join(' ') end |
#name ⇒ Object
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_command ⇒ String
Get the sh command name to search for
37 38 39 |
# File 'lib/ukiryu/shell/sh.rb', line 37 def shell_command 'sh' end |