Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > can ruby replace bash scripts for linux script

Reply
Thread Tools

can ruby replace bash scripts for linux script

 
 
Greg Hauptmann
Guest
Posts: n/a
 
      07-12-2008
hi,

I need to develop some backup scripts for my linux box. I'm ok with
Ruby but havn't done bash scripts before.

Is ruby up to replacing bash shell scripts for this? (eg calling
commands, eg rsynch, getting responses etc?

Thanks

 
Reply With Quote
 
 
 
 
ara.t.howard
Guest
Posts: n/a
 
      07-12-2008

On Jul 11, 2008, at 9:54 PM, Greg Hauptmann wrote:

> hi,
>
> I need to develop some backup scripts for my linux box. I'm ok with
> Ruby but havn't done bash scripts before.
>
> Is ruby up to replacing bash shell scripts for this? (eg calling
> commands, eg rsynch, getting responses etc?
>
> Thanks


yes. astoundingly.

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




 
Reply With Quote
 
 
 
 
Xeno Campanoli
Guest
Posts: n/a
 
      07-12-2008
Greg Hauptmann wrote:
> hi,
>
> I need to develop some backup scripts for my linux box. I'm ok with
> Ruby but havn't done bash scripts before.


Bash is a different kind of language, both because it's more devoted to
handling native unix executables, but also because it's a more primitive
and older language devoted to doing things in ways they needed to be
done before the present price/cost of resources.

There are a lot of things you can do much more efficiently in Bash, and
in far fewer lines. Also, in Sysadmin land, you may find more of the
people managing your production boxes will know Bash than Ruby.
Probably far more. In my shop, practically everyone is still refusing
to use Ruby and they want me to write anything I can in Bash, when it's
at all practical. To a certain extent I think this kind of request has
reasonable roots and should be taken into account.

That all being said, and despite the fact that Ruby programs can be
longer, Ruby programs can be much more readable, IMHO, than Bash
scripts, especially when the complexity of the program increases beyond
a few hundred lines of code. Now I have several Bash scripts that
include libraries of Bash functions and the entire things are well over
a thousand lines, and these are still reasonable pieces of software.
Most things that size make me think of Ruby as the best long term,
maintainable way to program. The reasons, again IMHO, include:

1. A lot of general facilities which allow for clean solutions
that would be ugly in Bash. For instance, I recently ran into a
Bash script that had sequences of over 10 sed commands to decode
%nn embedded strings from an HTTP query string. Ruby has native
methods for this that you can depend upon. When you write your
own, thats gonna be delicate code subject to getting trashed in
some quick vi edit.

2. Class organization, which I like to do when the programmed
activity is best illustrated with that kind of organization, is
nicely formalized in Ruby, and requires thought and jury-rigging
in Bash.

3. Loops are very flexible in Ruby. On the other hand, in Bash
they may sometimes be more concise.

4. Structures in Ruby are generally clearer and more readable:

if [[ x != 'y' ]]; then
somefunction $arg1 $arg2
else
echo "ERROR: blek $myevidence"
exit 1
fi

vs Ruby

if x != 'y' then
somemethod(arg1,arg2)
else
raise Exception, "ERROR: blek #{myevidence}"
end

The difference may not send you, but the extra little tick marks
and such can be a big time cost when it comes to debugging and
maintenance. Interpolation, as evidenced above, actually uses
more, but is more clear and flexible, and is always the same.


>
> Is ruby up to replacing bash shell scripts for this? (eg calling
> commands, eg rsynch, getting responses etc?


Yes, but it's up to you whether you want to spend the time. I have a
class I call FlatAccess which has methods like:

MYUSER=myusername
SUDOPART = "sudo -u #{MYUSER}"

def FlatAccess.sudo(shellcmd)
`#{SUDOPART} #{shellcmd}`
end

But of course you can just state the command directly like:
result = `sudo -u myusername #{shellcmd}`
---snip---

Now all that being said, there's also something called capistrano that
does this kind of thing in Ruby for you, so if you're doing that a lot,
it is worth looking into capistrano. Perhaps I'll do that myself some
time soon....

There are a lot of other facilities in native Ruby to do system stuff,
so the more encompassing answer is: yes.

One thing I did recently which was interesting was to use pipes and
threads to collect output from stderr and stdout at the same time from a
shell execution of curl.

xc



>
> Thanks
>
>



 
Reply With Quote
 
Marc Heiler
Guest
Posts: n/a
 
      07-12-2008
> 3. Loops are very flexible in Ruby. On the other hand, in Bash
> they may sometimes be more concise.



loop {}


But I am curious what you mean specifically in bash.

To be honest, maybe except for speed, I see 0 reason to use
bash scripts preferred over ruby scripts.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Greg Hauptmann
Guest
Posts: n/a
 
      07-12-2008
tks xc,

Given I'm a hobbiest programmer I think I'll try to write my linux
backup script in Ruby then. I don't get that much time so sticking to
Ruby for me would be great.

BTW: I'd also posted on the capistrano (which I use) group re this. I
guess if I'm using rsynch however it'll handle ssh under the bonnet
(cf cap's libraries for ssh etc)

Are there any tips re calling unix commands & receiving responses in ruby?

thanks


On 7/12/08, Xeno Campanoli <> wrote:
> Greg Hauptmann wrote:
>> hi,
>>
>> I need to develop some backup scripts for my linux box. I'm ok with
>> Ruby but havn't done bash scripts before.

>
> Bash is a different kind of language, both because it's more devoted to
> handling native unix executables, but also because it's a more primitive
> and older language devoted to doing things in ways they needed to be
> done before the present price/cost of resources.
>
> There are a lot of things you can do much more efficiently in Bash, and
> in far fewer lines. Also, in Sysadmin land, you may find more of the
> people managing your production boxes will know Bash than Ruby.
> Probably far more. In my shop, practically everyone is still refusing
> to use Ruby and they want me to write anything I can in Bash, when it's
> at all practical. To a certain extent I think this kind of request has
> reasonable roots and should be taken into account.
>
> That all being said, and despite the fact that Ruby programs can be
> longer, Ruby programs can be much more readable, IMHO, than Bash
> scripts, especially when the complexity of the program increases beyond
> a few hundred lines of code. Now I have several Bash scripts that
> include libraries of Bash functions and the entire things are well over
> a thousand lines, and these are still reasonable pieces of software.
> Most things that size make me think of Ruby as the best long term,
> maintainable way to program. The reasons, again IMHO, include:
>
> 1. A lot of general facilities which allow for clean solutions
> that would be ugly in Bash. For instance, I recently ran into a
> Bash script that had sequences of over 10 sed commands to decode
> %nn embedded strings from an HTTP query string. Ruby has native
> methods for this that you can depend upon. When you write your
> own, thats gonna be delicate code subject to getting trashed in
> some quick vi edit.
>
> 2. Class organization, which I like to do when the programmed
> activity is best illustrated with that kind of organization, is
> nicely formalized in Ruby, and requires thought and jury-rigging
> in Bash.
>
> 3. Loops are very flexible in Ruby. On the other hand, in Bash
> they may sometimes be more concise.
>
> 4. Structures in Ruby are generally clearer and more readable:
>
> if [[ x != 'y' ]]; then
> somefunction $arg1 $arg2
> else
> echo "ERROR: blek $myevidence"
> exit 1
> fi
>
> vs Ruby
>
> if x != 'y' then
> somemethod(arg1,arg2)
> else
> raise Exception, "ERROR: blek #{myevidence}"
> end
>
> The difference may not send you, but the extra little tick marks
> and such can be a big time cost when it comes to debugging and
> maintenance. Interpolation, as evidenced above, actually uses
> more, but is more clear and flexible, and is always the same.
>
>
>>
>> Is ruby up to replacing bash shell scripts for this? (eg calling
>> commands, eg rsynch, getting responses etc?

>
> Yes, but it's up to you whether you want to spend the time. I have a
> class I call FlatAccess which has methods like:
>
> MYUSER=myusername
> SUDOPART = "sudo -u #{MYUSER}"
>
> def FlatAccess.sudo(shellcmd)
> `#{SUDOPART} #{shellcmd}`
> end
>
> But of course you can just state the command directly like:
> result = `sudo -u myusername #{shellcmd}`
> ---snip---
>
> Now all that being said, there's also something called capistrano that
> does this kind of thing in Ruby for you, so if you're doing that a lot,
> it is worth looking into capistrano. Perhaps I'll do that myself some
> time soon....
>
> There are a lot of other facilities in native Ruby to do system stuff,
> so the more encompassing answer is: yes.
>
> One thing I did recently which was interesting was to use pipes and
> threads to collect output from stderr and stdout at the same time from a
> shell execution of curl.
>
> xc
>
>
>
>>
>> Thanks
>>
>>

>
>
>


 
Reply With Quote
 
Dave Bass
Guest
Posts: n/a
 
      07-12-2008
Greg Hauptmann wrote:
> Is ruby up to replacing bash shell scripts for this? (eg calling
> commands, eg rsynch, getting responses etc?


Yes, but this is exactly what Perl was developed for originally: a
scripting language superior to csh, awk, etc. Ruby has its roots in Perl
syntax, but whether you need its object orientation for system scripting
is debateable.

On WinXP it seems that Ruby's start-up time is much slower than Perl's.
I haven't used Ruby much on Linux but I suspect that Perl may be faster
there too, given that Perl has been well tuned for speed over the years.

Dave
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
unni.tallman
Guest
Posts: n/a
 
      07-12-2008
On Jul 12, 4:59 pm, Dave Bass <daveb...@musician.org> wrote:
> Greg Hauptmann wrote:
> > Is ruby up to replacing bash shell scripts for this? (eg calling
> > commands, eg rsynch, getting responses etc?

>
> Yes, but this is exactly what Perl was developed for originally: a
> scripting language superior to csh, awk, etc. Ruby has its roots in Perl
> syntax, but whether you need its object orientation for system scripting
> is debateable.
>
> On WinXP it seems that Ruby's start-up time is much slower than Perl's.
> I haven't used Ruby much on Linux but I suspect that Perl may be faster
> there too, given that Perl has been well tuned for speed over the years.
>
> Dave
> --
> Posted viahttp://www.ruby-forum.com/.


i.using back-quotes is the easiest way to get the output of Unix
commands from Ruby.

response = `ls`

response will be a string object now containing the result of the 'ls'
command.

ii. x = IO.popen("ls")
response = IO.read

read documentation for popen, it has options for reading Unix
program's STDOUT & STDERR separately.

iii. system("ls") - this call can be used to run Unix commands. but
their outputs cannot be captured.
 
Reply With Quote
 
عمر ملقب بالثانی
Guest
Posts: n/a
 
      07-12-2008
You may want to take a glance at André Ben Hamou, Practical Ruby for
System Administration, 2007, if your neighbourhood library happens to
have it.

It is helping me a lot in my Ruby learning process. It almost convinced
me that Ruby might be able to replace Perl for administration tasks
(huge amount of available good Perl code and documentation aside).

But you'll still need a solid understanding of administration tasks on
your system, hence a good command of its command line interpreter.

Kind regards, Ömer.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      07-12-2008
Dave Bass wrote:
> On WinXP it seems that Ruby's start-up time is much slower than Perl's.
> I haven't used Ruby much on Linux but I suspect that Perl may be faster
> there too, given that Perl has been well tuned for speed over the years.


How much of that delay is due to gems? Try setting RUBYOPT="" and
running ruby.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Xeno Campanoli
Guest
Posts: n/a
 
      07-12-2008
Marc Heiler wrote:
>> 3. Loops are very flexible in Ruby. On the other hand, in Bash
>> they may sometimes be more concise.

>
>
> loop {}


for d in $(ls $MYDIR/*)
do
echo "trace: $d"
done

versus

Dir["#{MYDIR}/*"].each do |d|
puts "trace: $d"
end

So, given the difference isn't much, I'd assert the first one is a
little clearer. There are a lot of such examples.

I think another aspect of where I'm at is that everybody knows Bash, and
I did allude to that before. If you know people are gonna have to hack
stuff, and your in a business, you try to provide the best environment
for them to do so.

I can certainly see it being reasonable to do practically everything in
Ruby, however, and I wouldn't mind doing that if the work environment
made it a reasonable way to go.

xc

>
>
> But I am curious what you mean specifically in bash.
>
> To be honest, maybe except for speed, I see 0 reason to use
> bash scripts preferred over ruby scripts.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Terminate a python script from linux shell / bash script Gros Bedo Python 0 07-13-2008 06:59 PM
Re: Terminate a python script from linux shell / bash script norseman Python 0 07-10-2008 09:44 PM
RE: Terminate a python script from linux shell / bash script Gros Bedo Python 1 07-10-2008 08:50 PM
Terminate a python script from linux shell / bash script Gros Bedo Python 2 07-10-2008 08:42 AM
(patch for Bash) GTK+2 + Bash William Park XML 3 07-14-2005 12:09 AM



Advertisments