Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby equivalent to "exec > $logfile 2>&1" in sh script?

Reply
Thread Tools

Ruby equivalent to "exec > $logfile 2>&1" in sh script?

 
 
Phil Rhoades
Guest
Posts: n/a
 
      12-01-2006
People,

I quite frequently have something like:

exec > $logfile 2>&1

at the top of my shell scripts to output everything that follows (including
errors) into a log file - is there some way of doing the equivalent in a Ruby
script?

Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +610)411-185-652
Fax: +610)2-8221-9599
E-mail: http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      12-01-2006
On Nov 30, 2006, at 19:24 , El Gato wrote:
> Phil Rhoades wrote:
>> I quite frequently have something like:
>>
>> exec > $logfile 2>&1
>>
>> at the top of my shell scripts to output everything that follows
>> (including errors) into a log file - is there some way of doing
>> the equivalent in a Ruby script?

>
> This seems to work:
>
> outfile = File.open("output.txt", "w")
> $stdout.reopen outfile
> $stderr.reopen outfile
>
> puts "hello world!"
> system("dir no_exist")


Not really.

$ ruby
outfile = File.open("output.txt", "w")
$stdout.reopen outfile
$stderr.reopen outfile

0.upto 10 do |v| (v%2==0 ? STDOUT : STDERR).puts v; end
$ cat output.txt
1
3
5
7
9
0
2
4
6
8
10


--
Eric Hodel - (E-Mail Removed) - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!


 
Reply With Quote
 
 
 
 
Rick Tessner
Guest
Posts: n/a
 
      12-01-2006
Hi all,

Adding $stdout.sync = true; $stderr.sync = true should solve the problem
of stdout and stderr being output of of sync.

See in-line edit below.

On Fri, 2006-01-12 at 16:10 +0900, Eric Hodel wrote:

> $ ruby
> outfile = File.open("output.txt", "w")
> $stdout.reopen outfile
> $stderr.reopen outfile


$stdout.sync = true
$stderr.sync = true

> 0.upto 10 do |v| (v%2==0 ? STDOUT : STDERR).puts v; end



--
Rick
(E-Mail Removed)


 
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
Any equivalent to Ruby's 'hpricot' html/xpath/css selector package? Kenneth McDonald Python 6 12-30-2008 01:38 PM
ruby %w equivalent Antoine De Groote Python 24 09-30-2006 10:15 AM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Ruby on Rails Job Site -- Is there a Python equivalent? Ray Python 3 12-02-2005 03:13 PM
Ruby on Rails or Perl's Maypole..is there a Python equivalent Gary Nutbeam Python 12 03-07-2005 04:40 PM



Advertisments