Ruby Escape Argv Argument or String as Argument to Shell Command

Ruby escape ARGV argument or string as argument to shell command

Use require 'shellwords' and Shellwords.escape, which will fix this sort of stuff for you:

http://apidock.com/ruby/Shellwords/shellescape

ruby unicode escapes as command line arguments

To unescape the unicode escaped command line argument and create a new file with the user supplied unicode string in the filename, I used @mu is too short's method of using pack and unpack, like so:

filetype  = ARGV[1]
unicode = ARGV[0].gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
path = unicode + "." + filetype
File.new(path, "w")

How to pass a ruby script's output as arguments to a shell command using command substitution?

The problem is in your bash command. Backtick is escaping the " and '

You can use xargs

ruby a_script.rb | xargs ruby a_script.rb

How do you pass the string *.* to ruby as a command line parameter?

You may need to put this into literal quotes:

test_argv "*.*"

The quotes should avoid having the command-line arguments get expanded on you prematurely.

How to pass array as an argument to a Ruby function from command line?

You clearly want to pass an array as the first parameter into start_services method, so you should do it like this:

$ ruby -r "./test.rb" -e "start_services ['dev','test','uat'],'developer','welcome123'"
# output:
dev
test
uat
developer
welcome123

What you've been trying so far was attempt to pass '[\'dev\',\'test\',\'uat\']' string, which was malformed, because you didn't escape ' characters.

Ruby string with quotes for shell command args?

Like many languages, Ruby needs a way of delimiting a quoted quote, and the enclosing quotes.

What you're seeing is the escape character which is a way of saying literal quote instead of syntactic quote:

foo = 'test="test"'
# => "test=\"test\""

The escape character is only there because double-quotes are used by default when inspecting a string. It's stored internally as a single character, of course. You may also see these in other circumstances such as a CR+LF delimited file line:

"example_line\r\n"

The \r and \n here correspond with carriage-return and line-feed characters. There's several of these characters defined in ANSI C that have carried over into many languages including Ruby and JavaScript.

When you output a string those escape characters are not displayed:

puts foo
test="test"

Make a string both valid JSON and shell escaped

Shells are for humans, not for machines. Having a machine produce shell commands is a code smell indicating that you're automating at the wrong layer.

Skip the shell, and just run your program with the required arguments:

data = [["305", "John Smith", "Amy Smith`", "10/11/2008", "Medical", {"page_count"=>4}]]
json_str = data.to_json
Open3.popen3("node", "myscript.js", json_str) do |stdin, stdout, stderr, wait_thr|
output = [stdout.read, stderr.read]
end

Since there is no shell involved, there's no human silliness like escaping to care about.

How do you parse a string into an array in the way similar to ARGV?

You can use the Shellwords module from ruby's standard library, which exists exactly for this purpose:

require 'shellwords'
Shellwords.shellwords 'apple orange "banana pear" pineapple\ apricot'
#=> ["apple", "orange", "banana pear", "pineapple apricot"]

As can be seen in the example above, this also allows you to escape spaces with backslashes the same way you can in the shell. And of course you can also use single quotes instead of double quotes and escape both kinds of quotes with a backslash to get a literal double or single quote.

passing array as a command argument

You can use eval although you might open a security hole:

require 'pp'

def foo(arr1, var, arr2, var2)
puts arr1.class
pp arr1
pp arr1[0]
puts arr2.class
pp arr2
pp arr2[0]
end

eval("foo " + ARGV.join(" "))

Result

Array
[1, 2]
1
Array
[5, 6]
5

Hope it helps



Related Topics



Leave a reply



Submit