Bash script <=> Ruby sctipt

People,

I regularly grab information from the system using Ruby's backtick and system call facilities for getting values into variables in the Ruby script but sometimes it would be good to go the other way - if my shell script is too big and painful to rewrite in Ruby it would be good to be able to call a Ruby function to do something and then pass the results back to the shell script. I have been messing around for a few hours now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.

···

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Take a look at the command line options for the ruby command. Ruby is really good for this sort of thing. Much of it was borrowed from Perl.

Is there something specific you want to do?

···

-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of Philip Rhoades
Sent: 12 December 2017 11:49
To: Ruby users
Subject: Bash script <=> Ruby sctipt

People,

I regularly grab information from the system using Ruby's backtick and system call facilities for getting values into variables in the Ruby script but sometimes it would be good to go the other way - if my shell script is too big and painful to rewrite in Ruby it would be good to be able to call a Ruby function to do something and then pass the results back to the shell script. I have been messing around for a few hours now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.
--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Andy,

Take a look at the command line options for the ruby command. Ruby is
really good for this sort of thing. Much of it was borrowed from Perl.

I did but CLI don't help - I guess this is really a bash question . .

Is there something specific you want to do?

eg

#!/bin/sh
# t.sh

x=1234

y=`echo ${x} | t.rb` (fails)

#!/usr/bin/env ruby
# t.rb

puts ARGV.shift / 2

P.

···

On 2017-12-12 22:57, Andy Jones wrote:

-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of
Philip Rhoades
Sent: 12 December 2017 11:49
To: Ruby users
Subject: Bash script <=> Ruby sctipt

People,

I regularly grab information from the system using Ruby's backtick and
system call facilities for getting values into variables in the Ruby
script but sometimes it would be good to go the other way - if my
shell script is too big and painful to rewrite in Ruby it would be
good to be able to call a Ruby function to do something and then pass
the results back to the shell script. I have been messing around for
a few hours now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.
--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

IMO, when you reach that point, just stop writing shell and start to do
everything in ruby.

cheers
ralf

···

On 12/12/2017 12:49 PM, Philip Rhoades wrote:

People,

I regularly grab information from the system using Ruby's backtick and
system call facilities for getting values into variables in the Ruby
script but sometimes it would be good to go the other way - if my shell
script is too big and painful to rewrite in Ruby it would be good to be
able to call a Ruby function to do something and then pass the results
back to the shell script. I have been messing around for a few hours
now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.

If you want to use a ruby program as a pipe, that can be done. The ruby program would have to call `popen()`, I think?

But for your example, you could do something like this:

    y=`ruby -e 'puts ARGV.shift / 2' ${x}`

There are a bunch of command line options to make that easier, or use a separate file instead of passing the ruby code as a string. But that's the simplest example.

Try them out on the command line before putting them in your code, maybe. I usually find that's the best way myself.

(Disclaimer: busy day, I didn't test this code.)

···

-----Original Message-----
From: Philip Rhoades [mailto:phil@pricom.com.au]
Sent: 12 December 2017 12:50
To: Andy Jones
Cc: 'Ruby users'
Subject: Re: Bash script <=> Ruby sctipt

Andy,

On 2017-12-12 22:57, Andy Jones wrote:

Take a look at the command line options for the ruby command. Ruby is
really good for this sort of thing. Much of it was borrowed from Perl.

I did but CLI don't help - I guess this is really a bash question . .

Is there something specific you want to do?

eg

#!/bin/sh
# t.sh

x=1234

y=`echo ${x} | t.rb` (fails)

#!/usr/bin/env ruby
# t.rb

puts ARGV.shift / 2

P.

-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of
Philip Rhoades
Sent: 12 December 2017 11:49
To: Ruby users
Subject: Bash script <=> Ruby sctipt

People,

I regularly grab information from the system using Ruby's backtick and
system call facilities for getting values into variables in the Ruby
script but sometimes it would be good to go the other way - if my
shell script is too big and painful to rewrite in Ruby it would be
good to be able to call a Ruby function to do something and then pass
the results back to the shell script. I have been messing around for
a few hours now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.
--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Unsubscribe:
<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclai
>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Andy,

If you want to use a ruby program as a pipe, that can be done. The
ruby program would have to call `popen()`, I think?

It is more like I want to use it as a function or procedure from the bash script.

But for your example, you could do something like this:

    y=`ruby -e 'puts ARGV.shift / 2' ${x}`

Fails too.

There are a bunch of command line options to make that easier, or use
a separate file instead of passing the ruby code as a string. But
that's the simplest example.

Try them out on the command line before putting them in your code,
maybe. I usually find that's the best way myself.

Yep, that is what usually do . .

(Disclaimer: busy day, I didn't test this code.)

Clearly . .

Thanks,

Phil.

···

On 2017-12-12 23:58, Andy Jones wrote:

-----Original Message-----
From: Philip Rhoades [mailto:phil@pricom.com.au]
Sent: 12 December 2017 12:50
To: Andy Jones
Cc: 'Ruby users'
Subject: Re: Bash script <=> Ruby sctipt

Andy,

On 2017-12-12 22:57, Andy Jones wrote:

Take a look at the command line options for the ruby command. Ruby is
really good for this sort of thing. Much of it was borrowed from Perl.

I did but CLI don't help - I guess this is really a bash question . .

Is there something specific you want to do?

eg

#!/bin/sh
# t.sh

x=1234

y=`echo ${x} | t.rb` (fails)

#!/usr/bin/env ruby
# t.rb

puts ARGV.shift / 2

P.

-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of
Philip Rhoades
Sent: 12 December 2017 11:49
To: Ruby users
Subject: Bash script <=> Ruby sctipt

People,

I regularly grab information from the system using Ruby's backtick and
system call facilities for getting values into variables in the Ruby
script but sometimes it would be good to go the other way - if my
shell script is too big and painful to rewrite in Ruby it would be
good to be able to call a Ruby function to do something and then pass
the results back to the shell script. I have been messing around for
a few hours now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.
--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Unsubscribe:
<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclai
>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Now, see, if you are going say things like "fails too" without bothering to give me, say, an error message? (Which you would certainly have got if you had tried it on the command line.) And then get sarcastic? Makes me wonder why I bothered.

Here's a nice little blog post from someone that Ruby Weekly linked to: http://blog.honeybadger.io/ruby-unix-command-line/. Good luck, hope you figure it out.

···

-----Original Message-----
From: Philip Rhoades [mailto:phil@pricom.com.au]
Sent: 12 December 2017 13:05
To: Andy Jones
Cc: 'Ruby users'
Subject: Re: Bash script <=> Ruby sctipt

Andy,

On 2017-12-12 23:58, Andy Jones wrote:

If you want to use a ruby program as a pipe, that can be done. The
ruby program would have to call `popen()`, I think?

It is more like I want to use it as a function or procedure from the bash script.

But for your example, you could do something like this:

    y=`ruby -e 'puts ARGV.shift / 2' ${x}`

Fails too.

There are a bunch of command line options to make that easier, or use
a separate file instead of passing the ruby code as a string. But
that's the simplest example.

Try them out on the command line before putting them in your code,
maybe. I usually find that's the best way myself.

Yep, that is what usually do . .

(Disclaimer: busy day, I didn't test this code.)

Clearly . .

Thanks,

Phil.

-----Original Message-----
From: Philip Rhoades [mailto:phil@pricom.com.au]
Sent: 12 December 2017 12:50
To: Andy Jones
Cc: 'Ruby users'
Subject: Re: Bash script <=> Ruby sctipt

Andy,

On 2017-12-12 22:57, Andy Jones wrote:

Take a look at the command line options for the ruby command. Ruby is
really good for this sort of thing. Much of it was borrowed from Perl.

I did but CLI don't help - I guess this is really a bash question . .

Is there something specific you want to do?

eg

#!/bin/sh
# t.sh

x=1234

y=`echo ${x} | t.rb` (fails)

#!/usr/bin/env ruby
# t.rb

puts ARGV.shift / 2

P.

-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of
Philip Rhoades
Sent: 12 December 2017 11:49
To: Ruby users
Subject: Bash script <=> Ruby sctipt

People,

I regularly grab information from the system using Ruby's backtick and
system call facilities for getting values into variables in the Ruby
script but sometimes it would be good to go the other way - if my
shell script is too big and painful to rewrite in Ruby it would be
good to be able to call a Ruby function to do something and then pass
the results back to the shell script. I have been messing around for
a few hours now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.
--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Unsubscribe:
<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclai
>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Andy,

Take a look at the command line options for the ruby command. Ruby is
really good for this sort of thing. Much of it was borrowed from Perl.

I did but CLI don't help - I guess this is really a bash question . .

Is there something specific you want to do?

eg

#!/bin/sh
# t.sh

x=1234

y=`echo ${x} | t.rb` (fails)

#!/usr/bin/env ruby
# t.rb

puts ARGV.shift / 2

P.

ARGV is for args, not piped data. To do that you'd want the shell script to
look more like:

···

On 12 Dec. 2017 10:51 pm, "Philip Rhoades" <phil@pricom.com.au> wrote:
On 2017-12-12 22:57, Andy Jones wrote:

~~~
y=`t.rb "$x"`
~~~

You'd also have to fix your ruby script:

~~~
puts ARGV.shift.to_i / 2
~~~

ARGV is a sequence of Strings, which don't divide the way you might think
they do.

Alternatively, if you want to keep piping in bash, you need to make the
ruby script read from stdin:

~~~
puts gets.to_i / 2
~~~

Or, to inline it conveniently in your bash script:

~~~
y=`echo "$x" | /usr/bin/env ruby -ne 'puts $_.to_i / 2'`
~~~

If there are logical or typographic errors in any of the above, it's
because it's 11:30 at night, I'm on my phone, and I don't really care all
that much, but hopefully you get the idea.

Cheers
--
Matthew Kerwin

Andy,

Now, see, if you are going say things like "fails too" without
bothering to give me, say, an error message? (Which you would
certainly have got if you had tried it on the command line.) And then
get sarcastic? Makes me wonder why I bothered.

Sorry, I didn't mean to sound sarcastic - I thought you would just try yourself it when you had the chance . .

Here's a nice little blog post from someone that Ruby Weekly linked
to: http://blog.honeybadger.io/ruby-unix-command-line/. Good luck,
hope you figure it out.

Thanks but it is still the same problem - OK from the CLI - not alright from within a bash script as far as I can see . .

Regards,

Phil.

···

On 2017-12-13 00:13, Andy Jones wrote:

-----Original Message-----
From: Philip Rhoades [mailto:phil@pricom.com.au]
Sent: 12 December 2017 13:05
To: Andy Jones
Cc: 'Ruby users'
Subject: Re: Bash script <=> Ruby sctipt

Andy,

On 2017-12-12 23:58, Andy Jones wrote:

If you want to use a ruby program as a pipe, that can be done. The
ruby program would have to call `popen()`, I think?

It is more like I want to use it as a function or procedure from the
bash script.

But for your example, you could do something like this:

    y=`ruby -e 'puts ARGV.shift / 2' ${x}`

Fails too.

There are a bunch of command line options to make that easier, or use
a separate file instead of passing the ruby code as a string. But
that's the simplest example.

Try them out on the command line before putting them in your code,
maybe. I usually find that's the best way myself.

Yep, that is what usually do . .

(Disclaimer: busy day, I didn't test this code.)

Clearly . .

Thanks,

Phil.

-----Original Message-----
From: Philip Rhoades [mailto:phil@pricom.com.au]
Sent: 12 December 2017 12:50
To: Andy Jones
Cc: 'Ruby users'
Subject: Re: Bash script <=> Ruby sctipt

Andy,

On 2017-12-12 22:57, Andy Jones wrote:

Take a look at the command line options for the ruby command. Ruby is
really good for this sort of thing. Much of it was borrowed from Perl.

I did but CLI don't help - I guess this is really a bash question . .

Is there something specific you want to do?

eg

#!/bin/sh
# t.sh

x=1234

y=`echo ${x} | t.rb` (fails)

#!/usr/bin/env ruby
# t.rb

puts ARGV.shift / 2

P.

-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of
Philip Rhoades
Sent: 12 December 2017 11:49
To: Ruby users
Subject: Bash script <=> Ruby sctipt

People,

I regularly grab information from the system using Ruby's backtick and
system call facilities for getting values into variables in the Ruby
script but sometimes it would be good to go the other way - if my
shell script is too big and painful to rewrite in Ruby it would be
good to be able to call a Ruby function to do something and then pass
the results back to the shell script. I have been messing around for
a few hours now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.
--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Unsubscribe:
<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclai
>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Click here to view Company Information and Confidentiality
Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

Matthew,

Andy,

Take a look at the command line options for the ruby command.
Ruby is
really good for this sort of thing. Much of it was borrowed from
Perl.

I did but CLI don't help - I guess this is really a bash question .
.

Is there something specific you want to do?

eg

#!/bin/sh
# t.sh

x=1234

y=`echo ${x} | t.rb` (fails)

#!/usr/bin/env ruby
# t.rb

puts ARGV.shift / 2

P.

ARGV is for args, not piped data. To do that you'd want the shell
script to look more like:

~~~
y=`t.rb "$x"`

~~~

You'd also have to fix your ruby script:

~~~
puts ARGV.shift.to_i / 2

Sorry - it was a typo but I would have worked that out anyway but even with your changes I still get no output.

ARGV is a sequence of Strings, which don't divide the way you might
think they do.

Alternatively, if you want to keep piping in bash, you need to make
the ruby script read from stdin:

~~~
puts gets.to_i / 2

~~~

Or, to inline it conveniently in your bash script:

~~~
y=`echo "$x" | /usr/bin/env ruby -ne 'puts $_.to_i / 2'`

No, like I said in another reply - what I really want is something like Ruby functions or procedures that can be called from bash scripts . .

If there are logical or typographic errors in any of the above, it's
because it's 11:30 at night, I'm on my phone, and I don't really care
all that much, but hopefully you get the idea.

Thanks,

Phil.

···

On 2017-12-13 00:28, Matthew Kerwin wrote:

On 12 Dec. 2017 10:51 pm, "Philip Rhoades" <phil@pricom.com.au> wrote:

On 2017-12-12 22:57, Andy Jones wrote:

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

I wrote this little snippet, I hope it helps.

Beware, the only difficulties are in quoting, you must take into
account Shell quoting and Ruby quoting principles.

For this reason i used %q and %Q which are very useful in this
occasions. Ruby borrows Perl power, wise choice !

···

---------------
#!/bin/bash

x=hello

y=`ruby -e "v=%q{$x}; puts v.upcase" `

z=`ruby -e "v=%q{$y}; w =%Q{ #{v} World}; puts w" `

echo $z
---------------

On 12/12/2017 12:49, Philip Rhoades wrote:

People,

I regularly grab information from the system using Ruby's backtick and system call facilities for getting values into variables in the Ruby script but sometimes it would be good to go the other way - if my shell script is too big and painful to rewrite in Ruby it would be good to be able to call a Ruby function to do something and then pass the results back to the shell script. I have been messing around for a few hours now and I can't see a nice way of doing it . .

Suggestions?

Thanks,

Phil.

I'd rather pass arguments via the command line than fiddling with two
levels of quoting:

x=hello
y=$(ruby -e 'puts ARGV.shift.upcase' "$x")
echo "$y"

Reason:

$ cat x
x='he}llo'

y=`ruby -e "v=%q{$x}; puts v.upcase" `
echo "y=$y"

z=$(ruby -e 'puts ARGV.shift.upcase' "$x")
echo "z=$z"

$ sh x
-e:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input
v=%q{he}llo}; puts v.upcase
           ^
y=
z=HE}LLO

In other words: it is like with SQL injection, you have to make sure
everything in $x is escaped properly when "manipulating" it into the
command. OTOH when passing on the command line you just need to use
regular shell quoting and be done.

Kind regards

robert

···

On Tue, Dec 12, 2017 at 3:22 PM, Nicola Mingotti <nmingotti@gmail.com> wrote:

I wrote this little snippet, I hope it helps.

Beware, the only difficulties are in quoting, you must take into
account Shell quoting and Ruby quoting principles.

For this reason i used %q and %Q which are very useful in this
occasions. Ruby borrows Perl power, wise choice !

---------------
#!/bin/bash

x=hello

y=`ruby -e "v=%q{$x}; puts v.upcase" `

z=`ruby -e "v=%q{$y}; w =%Q{ #{v} World}; puts w" `

echo $z
---------------

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Robert,

I wrote this little snippet, I hope it helps.

Beware, the only difficulties are in quoting, you must take into
account Shell quoting and Ruby quoting principles.

For this reason i used %q and %Q which are very useful in this
occasions. Ruby borrows Perl power, wise choice !

---------------
#!/bin/bash

x=hello

y=`ruby -e "v=%q{$x}; puts v.upcase" `

z=`ruby -e "v=%q{$y}; w =%Q{ #{v} World}; puts w" `

echo $z
---------------

I'd rather pass arguments via the command line than fiddling with two
levels of quoting:

x=hello
y=$(ruby -e 'puts ARGV.shift.upcase' "$x")

Ah! That's the syntax that works for me!

Thanks Robert and other people,

Phil.

···

On 2017-12-13 02:38, Robert Klemme wrote:

On Tue, Dec 12, 2017 at 3:22 PM, Nicola Mingotti <nmingotti@gmail.com> > wrote:

echo "$y"

Reason:

$ cat x
x='he}llo'

y=`ruby -e "v=%q{$x}; puts v.upcase" `
echo "y=$y"

z=$(ruby -e 'puts ARGV.shift.upcase' "$x")
echo "z=$z"

$ sh x
-e:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input
v=%q{he}llo}; puts v.upcase
           ^
y=
z=HE}LLO

In other words: it is like with SQL injection, you have to make sure
everything in $x is escaped properly when "manipulating" it into the
command. OTOH when passing on the command line you just need to use
regular shell quoting and be done.

--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: phil@pricom.com.au

I am happy you found a syntax you like to make it !

I am going on just out of academic curiosity;)

about the case proposed by Robert, you can fix it like this:

···

----------------------------------
x="he{llo"
y=`ruby -e "v=%q|$x|; puts v.upcase" `
echo $y
----------------------------------

"q|Q" delimters are flexible, you choose them according
to what you need to solve your problem.

If you have no idea what "x" string contains then
you could go with "here documents" as :

-----------------------
x="he{llo "
y=`ruby -e "v = <<HERE
$x
HERE
puts v.upcase" `
echo $y
-----------------------

Anyway Robert syntax is probably the nicest
for the problem you propose. I will copy it myself
into my Ruby notes;)

bye
n.

On 12/12/2017 17:39, Philip Rhoades wrote:

Robert,

On 2017-12-13 02:38, Robert Klemme wrote:

On Tue, Dec 12, 2017 at 3:22 PM, Nicola Mingotti >> <nmingotti@gmail.com> wrote:

I wrote this little snippet, I hope it helps.

Beware, the only difficulties are in quoting, you must take into
account Shell quoting and Ruby quoting principles.

For this reason i used %q and %Q which are very useful in this
occasions. Ruby borrows Perl power, wise choice !

---------------
#!/bin/bash

x=hello

y=`ruby -e "v=%q{$x}; puts v.upcase" `

z=`ruby -e "v=%q{$y}; w =%Q{ #{v} World}; puts w" `

echo $z
---------------

I'd rather pass arguments via the command line than fiddling with two
levels of quoting:

x=hello
y=$(ruby -e 'puts ARGV.shift.upcase' "$x")

Ah! That's the syntax that works for me!

Thanks Robert and other people,

Phil.

echo "$y"

Reason:

$ cat x
x='he}llo'

y=`ruby -e "v=%q{$x}; puts v.upcase" `
echo "y=$y"

z=$(ruby -e 'puts ARGV.shift.upcase' "$x")
echo "z=$z"

$ sh x
-e:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input
v=%q{he}llo}; puts v.upcase
^
y=
z=HE}LLO

In other words: it is like with SQL injection, you have to make sure
everything in $x is escaped properly when "manipulating" it into the
command. OTOH when passing on the command line you just need to use
regular shell quoting and be done.

I am going on just out of academic curiosity;)

about the case proposed by Robert, you can fix it like this:
----------------------------------
x="he{llo"
y=`ruby -e "v=%q|$x|; puts v.upcase" `
echo $y
----------------------------------

"q|Q" delimters are flexible, you choose them according
to what you need to solve your problem.

I am sorry, but this is not a fix because:

If you have no idea what "x" string contains then

The whole point of using a variable is to process varying inputs. The
content probably even comes from outside the script (i.e. user input
via keyboard or a file). If the text is fixed, there is no point in
doing this stunt because then you just write the text into the command
line. By that time you will realize that the program always returns
the same output for the same input and replace the invocation of ruby
completely by the constant result.

you could go with "here documents" as :
-----------------------
x="he{llo "
y=`ruby -e "v = <<HERE
$x
HERE
puts v.upcase" `
echo $y
-----------------------

That approach is vulnerable to the same issue because you do not do
any escaping of "risky" sequences in $x. Anybody can craft an input
that will make the script fail - or delete your home directory.

Cheers

robert

···

On Tue, Dec 12, 2017 at 7:00 PM, Nicola Mingotti <nmingotti@gmail.com> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/