From: "Jesús Gabriel y Galán" Date: 2010-11-28T19:54:52+09:00 Subject: Re: How to use Ruby like shell script? On Sun, Nov 28, 2010 at 9:50 AM, Yu-Hsuan Lai wrote: > On Sun, Nov 28, 2010 at 3:47 AM, Rilindo Foster wrote: > >> Yu, >> >> As much as I liked using Ruby, what exactly are you looking to do with ruby >> that you can't do with bash or ksh? >> On Nov 27, 2010, at 9:51 AM, Yu-Hsuan Lai wrote: >> >> > Can I use ruby like my linux shell script(e.x. bash)?(or on the other >> hand, >> > use some ruby in shell script) >> > For example, how to access environmental variables? Redirect stdi/o with >> '|' >> > ? >> > If it's valuable, are there some websites/books talking about this? >> > >> > -- >> > Lai, Yu-Hsuan >> >> >> > On Sun, Nov 28, 2010 at 4:16 AM, Stu wrote: > >> You also asked for a website resource. I feel this site is pretty >> venerable: >> http://www.shelldorado.com/ >> >> I'm sure there are many bourne *again* specific resources out there but I >> only have experience with pure sh (actually Almquist* *sh) >> >> Specific features that bash might give you that can't be expressed inside >> ruby( if any) should be launched with the bash interpreter... though if you >> get that far you may want to reevaluate your program. >> >> You can get path and other info from ENV >> >> of course ruby programs can be launched inside your #!/bin/sh scripts and >> as >> I pointed out the other way around. >> >> Also following the Unix philosophy: *Write programs that do one thing and >> do >> it well. Write programs to work together. Write programs to handle text >> streams, because that is a universal interface.* >> >> And my philosophy: AUTOMATE EVERYTHING =) >> >> happy hacking! >> >> On Sat, Nov 27, 2010 at 1:43 PM, Stu wrote: >> > backticks work very well as well and are in the bourne scripting >> > metaphor. Interpolation (i.e. #{self} will also work inside ruby's >> > back ticks. >> > >> > They will return string. to be used within the program. pipes and >> > redirection can be used inside as so: >> > >> > ps = "$2" >> > pid =  `ps aux | awk '{print #{ps}}'` >> > printf pid >> > >> > $$ gives you the ruby programs process id >> > $0 returns a string of the program that is running >> > $* will give you arg list >> > >> > here = <> >  will work as >> >  well which also >> >  include #{interpolation} >> > FILE >> > >> > Others can be found in a file called English.rb >> > >> > ~ >> > >> > > OH, sorry for my poor English. > Infact what I want to do is not "use ruby like shell script" but "use shell > in ruby". > For instant, I wonder if I can use "ls | grep" or "tar -xvf | grep" to get a > file list and store it in a ruby array, and handle them with io#gets. > > I don't know what "back ticks" are. You can execute shell commands via the following methods: Kernel#system: system("ls | grep something") Kernel#` `ls | grep something` (this is the backtick) %x: %x{ls | grep something} irb(main):002:0> puts `ls` applications [...] => nil irb(main):003:0> puts %x{ls} applications [...] => nil irb(main):004:0> puts system("ls") applications [...] true => nil Jesus.