class Object
Public Instance Methods
Similar to +bash_escape_arg!!+ but non-destructive.
# File lib/shell_quotes/bash.rb, line 79 def bash_escape_arg clone = self.clone clone.bash_escape_arg! end
Produces -> String
Modifies a string to represent a Bash shell-style escaped command-line argument.
This method does not take account of any shell variables. If these are included in the original string, they will be escaped, which is probably not what you want.
In case the original string is surrounded by backticks, single or double quotes then it will be passed on to bash_escape_substitute!
, bash_escape_single_quoted!
, or bash_escape_double_quoted!
respectively.
# File lib/shell_quotes/bash.rb, line 63 def bash_escape_arg! unless self =~ %r{^(`|"|').*(`|"|')$} self.gsub!( %r{(!|\{|\}#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\)})\ { |match| '\\' + match } || self else self[0] == '`'\ ? self[1..-2].bash_escape_substitute! : self[0] == "'"\ ? self[1..-2].bash_escape_single_quoted!\ : self[1..-2].bash_escape_double_quoted! end end
Similar to bash_escape_file_path!
but non-destructive.
# File lib/shell_quotes/bash.rb, line 26 def bash_escape_file_path clone = self.clone clone.bash_escape_file_path! end
Produces -> String
Modifies a string to represent a file path at the Bash shell command-line.
# File lib/shell_quotes/bash.rb, line 15 def bash_escape_file_path! raise ShellQuotes::SQError, 'The NULL character is disallowed inside a Unix file path.'\ if self =~ %r/\0/ self.gsub!(%r{(!|#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\|\s)})\ { |match| '\\' + match } || self end
Similar to bash_escape_substitute!
but non-destructive.
# File lib/shell_quotes/bash.rb, line 105 def bash_escape_substitute clone = self.clone clone.bash_escape_substitute! end
Produces -> String
Modifies a string to represent Bash shell-style command substitution. Also takes care of nested command substitution by escaping backtick characters where appropriate.
# File lib/shell_quotes/bash.rb, line 98 def bash_escape_substitute! (self.gsub!(%r/(`.+`)/) { |match| '$(' + match[1..-2] + ')' } || self)\ .sub!(%r{.*}) { |match| "$(" + match + ")" } end
Similar to bash_unescape_file_path!
but non-destructive.
# File lib/shell_quotes/bash.rb, line 44 def bash_unescape_file_path clone = self.clone clone.bash_unescape_file_path! end
Produces -> String
Similar to bash_escape_file_path
, but modifies a string which represents an escaped file path into an unescaped string.
# File lib/shell_quotes/bash.rb, line 36 def bash_unescape_file_path! self.gsub!( %r{\\(!|#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\|\s)})\ { |match| match[-1] } || self end
Produces -> String
similar to bourne_double_quoted!
but non-destructive: Returns a string to represent a Bourne shell-style double-quoted string.
# File lib/shell_quotes/bourne.rb, line 181 def bourne_double_quoted(variable = nil) clone = self.clone clone.bourne_double_quoted!(variable = variable) end
Produces -> String
Modifies a string to represent a Bourne shell-style double-quoted string.
Takes account of Bourne Shell special variables, your own defined variables as specified in the variable
argument, and anything that is surrounded by backticks (command substitution).
For shell special varibles: see README.
-
Arguments
variable
-> Takes a string or an array of strings representing variables for interpolation inside the double quoted string. Ignored if not set. Should be String
or Array
. Optional. By default: nil
.
# File lib/shell_quotes/bourne.rb, line 140 def bourne_double_quoted!(variable = nil) vars = String.new illegal = %r/([^[a-z]|[A-Z]|_])/ msg = 'Illegal character(s) in argument.' if variable.is_a?(String) variable.slice!(0) if variable =~ %r/^\$/ raise ShellQuotes::SQError, msg if variable =~ illegal vars = "|#{variable}" elsif variable.is_a?(Array) variable.each { |item| item.slice!(0) if item =~ %r/^\$/ raise ShellQuotes::SQError, msg\ if item =~ illegal vars << "|#{item}" if item.class == String } end #-- Bourne shell special vars: # $$ $PPID $? $_ $! $PATH $IFS $HOME $UID $USER $# $@ $* $- $PWD #++ ((self.gsub!( %r/(\$(\$|\?|_|!|PATH|IFS|HOME|UID|USER|#|\*|-|PWD#{vars})?|"|\\|`(.*`)?)/ ) { |match| unless match =~ %r/^(\$.+|`(.*`))/ '\\' + match else ; match ; end } || self)\ .gsub!(%r/`.+`/) { |match| match[1..-2].bourne_escape_substitute! } || self)\ .sub!(%r/.*/) { |match| '"' + match + '"' } end
Similar to bourne_escape_arg!
but non-destructive.
# File lib/shell_quotes/bourne.rb, line 81 def bourne_escape_arg clone = self.clone clone.bourne_escape_arg! end
Produces -> String
Modifies a string to represent a Bourne shell-style escaped command-line argument.
This method does not take account of any shell variables. If these are included in the original string, they will be escaped, which is probably not what you want.
In case the original string is surrounded by backticks, single or double quotes then it will be passed on to bourne_escape_substitute!
, bourne_escape_single_quoted!
, or bourne_escape_double_quoted!
respectively.
# File lib/shell_quotes/bourne.rb, line 66 def bourne_escape_arg! unless self =~ %r{^(`|"|').*(`|"|')$} self.gsub!(%r{(#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\)})\ { |match| '\\' + match } || self else self[0] == '`'\ ? self[1..-2].bourne_escape_substitute! : self[0] == "'"\ ? self[1..-2].bourne_escape_single_quoted!\ : self[1..-2].bourne_escape_double_quoted! end end
Similar to bourne_escape_file_path!
but non-destructive.
# File lib/shell_quotes/bourne.rb, line 29 def bourne_escape_file_path clone = self.clone clone.bourne_escape_file_path! end
Produces -> String
Modifies a string to represent a file path at the Bourne shell command-line.
# File lib/shell_quotes/bourne.rb, line 17 def bourne_escape_file_path! raise ShellQuotes::SQError, 'The NULL character is disallowed inside a Unix file path.'\ if self =~ %r/\0/ self.gsub!(%r{(#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\|\s)})\ { |match| '\\' + match } || self end
similar to bourne_escape_substitute!
but non-destructive:
# File lib/shell_quotes/bourne.rb, line 199 def bourne_escape_substitute clone = self.clone clone.bourne_escape_substitute! end
Produces -> String
Modifies a string to represent Bourne shell-style command substitution. Also takes care of nested command substitution by escaping backtick characters where appropriate.
# File lib/shell_quotes/bourne.rb, line 192 def bourne_escape_substitute! (self.gsub!('`') { |match| '\\' + match } || self)\ .sub!(%r{.*}) { |match| "`" + match + "`" } end
Similar to bourne_single_quoted!
but non-destructive.
# File lib/shell_quotes/bourne.rb, line 117 def bourne_single_quoted(replacement = '_') clone = self.clone clone.bourne_single_quoted!(replacement = replacement) end
Produces -> String
Modifies a string to represent a Bourne shell-style single-quoted string. A Bourne shell single quoted string can’t take a straight single quote (or apostrophe) inside it, even if it is escaped. We therefore use a replacement character (by default, an underscore: _).
-
Arguments
replacement
-> Character to use to replace straight single quotes. By default: an underscore (_). Use any single charater except a backslash () or a single straight quote (‘).
A ShellQuotes::SQError
is raised if the replacement
argument is not as described above.
# File lib/shell_quotes/bourne.rb, line 103 def bourne_single_quoted!(replacement = '_') raise ShellQuotes::SQError, "Argument [replacement] should be String, maximum one character length.\n Argument can't be a backslash (\) or straight single quote (') character."\ if (not replacement.nil?) && (replacement.length > 1 ||\ (not replacement.is_a?(String)) || replacement =~ %r/(\\|')/) ((self.gsub!('\\') { |match| '\\' + match } || self)\ .gsub!("'") { |match| replacement } || self)\ .sub!(%r{.*}) { |match| "'" + match + "'" } end
Similar to bourne_unescape_file_path!
but non-destructive.
# File lib/shell_quotes/bourne.rb, line 47 def bourne_unescape_file_path clone = self.clone clone.bourne_unescape_file_path! end
Produces -> String
Similar to bourne_escape_file_path
, but modifies a string which represents an escaped file path into an unescaped string.
# File lib/shell_quotes/bourne.rb, line 39 def bourne_unescape_file_path! self.gsub!( %r{\\(#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\|\s)})\ { |match| match[-1] } || self end
Similar to csh_double_quoted!
but non-destructive.
# File lib/shell_quotes/csh.rb, line 177 def csh_double_quoted(variable = nil) clone = self.clone clone.csh_double_quoted!(variable = variable) end
Produces -> String
Modifies a string to represent a C shell-style double-quoted string.
Takes account of C Shell special variables, your own defined variables as specified in the variable
argument, and anything that is surrounded by backticks (command substitution).
For shell special varibles: see README.
-
Arguments
variable
-> Takes a string or an array of strings representing variables for interpolation inside the double quoted string. Ignored if not set. Should be String
or Array
. Optional. By default: nil
.
# File lib/shell_quotes/csh.rb, line 140 def csh_double_quoted!(variable = nil) vars = String.new illegal = %r/([^[a-z]|[A-Z]|_])/ msg = 'Illegal character(s) in argument.' if variable.is_a?(String) variable.slice!(0) if variable =~ %r/^\$/ raise ShellQuotes::SQError, msg if variable =~ illegal vars = "|#{variable}" elsif variable.is_a?(Array) variable.each { |item| item.slice!(0) if item =~ %r/^\$/ raise ShellQuotes::SQError, msg\ if item =~ illegal vars << "|#{item}" if item.class == String } end #-- C shell special vars: # $$ $? $! $PATH $HOME $USER $# $* $PWD #++ ((self.gsub!( %r/(\$(\$|\?|!|PATH|HOME|USER|#|\*|PWD#{vars})?|"|\\|`(.*`)?)/ ) { |match| unless match =~ %r/^(\$.+|`(.*`))/ '\\' + match else ; match ; end } || self)\ .gsub!(%r/`.+`/) { |match| match[1..-2].csh_escape_substitute! } || self)\ .sub!(%r/.*/) { |match| '"' + match + '"' } end
Similar to csh_escape_arg!
but non-destructive.
# File lib/shell_quotes/csh.rb, line 110 def csh_escape_arg clone = self.clone clone.csh_escape_arg! end
Produces -> String
Modifies a string to represent a C shell-style escaped command-line argument.
This method does not take account of any shell variables. If these are included in the original string, they will be escaped, which is probably not what you want.
In case the original string is surrounded by backticks, single or double quotes then it will be passed on to csh_escape_substitute!
, csh_escape_single_quoted!
, or csh_escape_double_quoted!
respectively.
# File lib/shell_quotes/csh.rb, line 65 def csh_escape_arg! unless self =~ %r{^(`|"|').*(`|"|')$} self.gsub!( %r{(!|\{|\}#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\)})\ { |match| '\\' + match } || self else self[0] == '`'\ ? self[1..-2].csh_escape_substitute! : self[0] == "'"\ ? self[1..-2].csh_escape_single_quoted!\ : self[1..-2].csh_escape_double_quoted! end end
Similar to csh_escape_file_path!
but non-destructive.
# File lib/shell_quotes/csh.rb, line 28 def csh_escape_file_path clone = self.clone clone.csh_escape_file_path! end
Produces -> String
Modifies a string to represent a file path at the C shell command-line.
# File lib/shell_quotes/csh.rb, line 16 def csh_escape_file_path! raise ShellQuotes::SQError, 'The NULL character is disallowed inside a Unix file path.'\ if self =~ %r/\0/ self.gsub!( %r/(!|\{|\}#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\|\s)/ ) { |match| '\\' + match } || self end
Similar to csh_escape_substitute!
but non-destructive.
# File lib/shell_quotes/csh.rb, line 192 def csh_escape_substitute clone = self.clone clone.csh_escape_substitute! end
Modifies a string to represent C shell-style command substitution.
Notice that nested command substitution does not work in csh.
# File lib/shell_quotes/csh.rb, line 186 def csh_escape_substitute! self.sub!(%r{.*}) { |match| "`" + match + "`" } end
Similar to csh_single_quoted!
but non-destructive.
# File lib/shell_quotes/csh.rb, line 117 def csh_single_quoted(replacement = '_') clone = self.clone clone.csh_single_quoted!(replacement = replacement) end
Produces -> String
Modifies a string to represent a C shell-style single-quoted string. A C shell single quoted string can’t take a straight single quote (or apostrophe) inside it, even if it is escaped. We therefore use a replacement character (by default, an underscore: _).
-
Arguments
replacement
-> Character to use to replace straight single quotes. By default: an underscore (_). Use any single charater except a backslash () or a single straight quote (‘).
A ShellQuotes::SQError
is raised if the replacement
argument is not as described above.
# File lib/shell_quotes/csh.rb, line 96 def csh_single_quoted!(replacement = '_') raise ShellQuotes::SQError, "Argument [replacement] should be String, maximum one character length.\n Argument can't be a backslash (\) or straight single quote (') character."\ if (not replacement.nil?) && (replacement.length > 1 ||\ (not replacement.is_a?(String)) || replacement == '!') ((self.gsub!(%r/(!|\n)/) { |match| '\\' + match } || self)\ .gsub!("'") { |match| replacement } || self) .sub!(%r{.*}) { |match| "'" + match + "'" } end
Similar to csh_unescape_file_path!
but non-destructive.
# File lib/shell_quotes/csh.rb, line 46 def csh_unescape_file_path clone = self.clone clone.csh_unescape_file_path! end
Produces -> String
Similar to csh_escape_file_path
, but modifies a string which represents an escaped file path into an unescaped string.
# File lib/shell_quotes/csh.rb, line 38 def csh_unescape_file_path! self.gsub!( %r/\\(!|\{|\}#|&|\*|\?|\[|\]|\(|\)|\=|\||\^|;|<|>|`|\$|"|'|\\|\s)/ ) { |match| match[-1] } || self end