Examples of common programming for Ruby and Linux shells

Source: Internet
Author: User
Tags pack

With the shell for Fur and ruby? Words can not say so, some small functions with Ruby is very convenient, such as the reversal of the string and add 1 function with Shell to write is more troublesome. str= "123456", we define a small way to reverse +1:

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 that r_add_1 the variables in the 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

Take a look at:

[Email protected]: ruby_src$./read.sh

[Email protected]: Ruby_src$echo $ret


[Email protected]: ruby_src$


Why, for the hair RET variable is null AH? The truth is simple: The pipeline 2 side is 2 different processes, plus the main bash process, is a total of 3 processes. You don't have to expect the variables created by the read RET process to take effect in the main bash process! One workaround is to force the read RET and Echo to execute in a process to get the return value with parentheses: Cat <<eof|ruby| (Read Ret;echo $ret):

[Email protected]: ruby_src$./read.sh

: 98765432DCB

Must be curly braces Oh, otherwise read RET and Echo still in 2 process, equal to do not work Ah! You can do this:

[Email protected]: ruby_src$my_val=$ (./read.sh)

[Email protected]: Ruby_src$echo $my _val

: 98765432DCB

This saves the returned results in the MY_VAL variable in the main bash process. Or you can handle it yourself in the while loop, because while loop and read are also in a process, the rewritten shell code is as follows:

#!/opt/local/bin/bashexport id= "abc123456789" #只是示例, you can actually get the value of the ID dynamically. Cat <<eof | Ruby | While read ret;do echo ">>>>" $ret;d oneclass stringdef r_add_1replace reverse.each_byte.map {|b|b+1}.pack (" c* ") endendputs env[" id "].dup.r_add_1eof

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

#!/opt/local/bin/bashexport id= "abc123456789" #只是示例, you can actually get the value of the ID dynamically. Cat <<eof | Ruby | While read Retclass stringdef r_add_1replace reverse.each_byte.map {|b|b+1}.pack ("c*") endendputs env["id"].dup.r_add_ 1EOFdo echo ">>>>" $retdone

The last execution read.sh will show:

[Email protected]: ruby_src$./read.sh

>>>>: 98765432DCB

Everything is possible! You will finally be asked: For Mao I do not $my_val=$ (ruby-e "xxx") This pure Ruby simple method, but to use Shell+ruby mixed complex method?? The answer is: I'm not lifting the Ruby+shell example!? Oh

Examples of common programming for Ruby and Linux shells

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.