class Object

Public Instance Methods

bash_double_quoted(variable = nil)
bash_double_quoted!(variable = nil)
bash_escape_arg() click to toggle source

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
bash_escape_arg!() click to toggle source

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
bash_escape_file_path() click to toggle source

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
bash_escape_file_path!() click to toggle source

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
bash_escape_substitute() click to toggle source

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
bash_escape_substitute!() click to toggle source

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
bash_single_quoted(replacement = '_')
bash_single_quoted!(replacement = '_')
bash_unescape_file_path() click to toggle source

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
bash_unescape_file_path!() click to toggle source

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
bourne_double_quoted(variable = nil) click to toggle source

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
Also aliased as: bash_double_quoted
bourne_double_quoted!(variable = nil) click to toggle source

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
Also aliased as: bash_double_quoted!
bourne_escape_arg() click to toggle source

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
bourne_escape_arg!() click to toggle source

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
bourne_escape_file_path() click to toggle source

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
bourne_escape_file_path!() click to toggle source

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
bourne_escape_substitute() click to toggle source

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
bourne_escape_substitute!() click to toggle source

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
bourne_single_quoted(replacement = '_') click to toggle source

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
Also aliased as: bash_single_quoted
bourne_single_quoted!(replacement = '_') click to toggle source

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
Also aliased as: bash_single_quoted!
bourne_unescape_file_path() click to toggle source

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
bourne_unescape_file_path!() click to toggle source

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
csh_double_quoted(variable = nil) click to toggle source

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
csh_double_quoted!(variable = nil) click to toggle source

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
csh_escape_arg() click to toggle source

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
csh_escape_arg!() click to toggle source

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
csh_escape_file_path() click to toggle source

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
csh_escape_file_path!() click to toggle source

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
csh_escape_substitute() click to toggle source

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
csh_escape_substitute!() click to toggle source

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
csh_single_quoted(replacement = '_') click to toggle source

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
csh_single_quoted!(replacement = '_') click to toggle source

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
csh_unescape_file_path() click to toggle source

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
csh_unescape_file_path!() click to toggle source

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