Ruby and linux shell are examples of common programming.

Source: Internet
Author: User

Ruby and linux shell are examples of common programming.

What if I want ruby if I have shell? This is not the case. It is very convenient to use ruby for some small functions. For example, it is troublesome to use shell for string inversion and 1 function. str = "123456", we define a small method of plus 1 after inversion:

2.1.5 :020 > class String2.1.5 :021?>   def r_add_12.1.5 :022?>     replace reverse.each_byte.map {|byte|byte+1}.pack("c*")2.1.5 :023?>     end2.1.5 :024?>   end

After execution, str becomes:

2.1.5: 026> "123456". r_add_1

=> "765432"

Let's write a script to replace r_add_1 in shell:

#!/opt/local/bin/bashexport id="abc123456789"  #justcat <<EOF | ruby |read retclass Stringdef r_add_1replace reverse.each_byte.map {|b|b+1}.pack("c*")endendputs ENV["id"].dup.r_add_1EOF

Run the following command:

Apple @ kissAir: ruby_src $./read. sh

Apple @ kissAir: ruby_src $ echo $ ret


Apple @ kissAir: ruby_src $


Empty: Is the Mao ret variable null? The principle is simple: Pipeline 2 is two different processes, and the main bash process, a total of three processes. You don't have to expect the variables created by the read ret process to take effect in the main bash process! One solution is to use parentheses to force read ret and echo to execute in a process to obtain the returned value: cat <EOF | ruby | (read ret; echo $ ret ):

Apple @ kissAir: ruby_src $./read. sh

: 98765432dcb

Parentheses must be added. Otherwise, read ret and echo are still in two processes, which means useless work! You can do this:

Apple @ kissAir: ruby_src $ my_val = $ (./read. sh)

Apple @ kissAir: ruby_src $ echo $ my_val

: 98765432dcb

In this way, the returned results are saved in the my_val variable in the main bash process. Or you can handle it yourself in the while LOOP, because the while loop and read are also in the same process. The modified shell code is as follows:

#! /Opt/local/bin/bashexport id = "abc123456789" # this is just an example. You can dynamically obtain the id value cat <EOF | ruby | while read ret; do echo ">>>>" $ ret; doneclass Stringdef r_add_1replace reverse. each_byte.map {| B + 1 }. pack ("c *") endendputs ENV ["id"]. dup. r_add_1EOF

If you think the cat line is too long, you can put do... done behind the end of EOF, and the effect is the same:

#! /Opt/local/bin/bashexport id = "abc123456789" # this is just an example. You can dynamically obtain the id value cat <EOF | ruby | while read retclass Stringdef r_add_1replace reverse. each_byte.map {| B + 1 }. pack ("c *") endendputs ENV ["id"]. dup. r_add_1EOFdo echo ">>>>" $ retdone

The last read. sh execution will show:

Apple @ kissAir: ruby_src $./read. sh

>>>>: 98765432dcb

Everything is possible! In the end, you will certainly ask: for Mao, I don't need $ my_val = $ (ruby-e "xxx"), which is a simpler method of pure ruby, what about the complex method of using shell + ruby ?? The answer is: I am not giving a ruby + shell example !? Haha

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.