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 . .
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 . .
-----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 . .
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 . .
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 . .
-----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 . .
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 . .
-----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 . .
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.
-----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 . .
-----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 . .
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.
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 . .
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 . .
-----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 . .
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.
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 . .
$ 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" `
$ 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.
$ 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.
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: