Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ENV values

Reply
Thread Tools

ENV values

 
 
Frank Hofmann
Guest
Posts: n/a
 
      08-30-2006
Hi all,

I'm working on an ssh script where I'd like to read out console
dimensions to adjust the output window - the default value of 80x24
isn't that good.

So I thought it should work by simply reading out the environment
variables $COLUMNS and $LINES (Linux system). The results in irb were
what I needed:

===---===---===---===---===---===
irb(main):001:0> ENV['COLUMNS']
=> "99"
irb(main):002:0> ENV['LINES']
=> "32"
===---===---===---===---===---===

But in a ruby script these things didn't work anymore. A really simple
script:

===---===---===---===---===---===
#!/usr/bin/ruby -w

puts ENV['USER']
puts ENV['LINES']
===---===---===---===---===---===

This gave me the output:
frank
nil

Ok, that didn't work like I've expected it, so here is another way:

===---===---===---===---===---===
irb(main):001:0> lines = `echo $LINES`
=> "32\n"
irb(main):002:0> puts lines
32
=> nil
===---===---===---===---===---===

Now as ruby script:

===---===---===---===---===---===
!/usr/bin/ruby -w

lines = `echo $LINES`
puts lines
===---===---===---===---===---===

Well, also no success. :-/ It gave me only a blank line.

Does anyone have an idea what I could do to get column & line numbers of
the console window?

Thanks...

Frank

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

 
Reply With Quote
 
 
 
 
Thomas Beckett
Guest
Posts: n/a
 
      08-30-2006
On 8/30/06, Frank Hofmann <(E-Mail Removed)> wrote:
> Hi all,
>
> I'm working on an ssh script where I'd like to read out console
> dimensions to adjust the output window - the default value of 80x24
> isn't that good.
>
> So I thought it should work by simply reading out the environment
> variables $COLUMNS and $LINES (Linux system). The results in irb were
> what I needed:
>
> ===---===---===---===---===---===
> irb(main):001:0> ENV['COLUMNS']
> => "99"
> irb(main):002:0> ENV['LINES']
> => "32"
> ===---===---===---===---===---===
>
> But in a ruby script these things didn't work anymore. A really simple
> script:
>
> ===---===---===---===---===---===
> #!/usr/bin/ruby -w
>
> puts ENV['USER']
> puts ENV['LINES']
> ===---===---===---===---===---===
>
> This gave me the output:
> frank
> nil
>
> Ok, that didn't work like I've expected it, so here is another way:
>
> ===---===---===---===---===---===
> irb(main):001:0> lines = `echo $LINES`
> => "32\n"
> irb(main):002:0> puts lines
> 32
> => nil
> ===---===---===---===---===---===
>
> Now as ruby script:
>
> ===---===---===---===---===---===
> !/usr/bin/ruby -w
>
> lines = `echo $LINES`
> puts lines
> ===---===---===---===---===---===
>
> Well, also no success. :-/ It gave me only a blank line.
>
> Does anyone have an idea what I could do to get column & line numbers of
> the console window?
>
> Thanks...
>
> Frank
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


If you dont mind depending on ncurses then will something like this work?
you will need the ncurses-ruby package
-----------------------------------------------
require "ncurses.rb"

win = Ncurses.WINDOW.new()
win.getmaxyx(y=[], x=[])
win.delwin()
----------------------------------------------
This should open ncurses mode, get its dimensions - which are handled
correctly and then drop you back out of ncurses.

Note that I havent tried it at all, am a complete newb to Ruby and
found the bits via google, but for 4 lines its worth a try.

Tom

 
Reply With Quote
 
 
 
 
Frank Hofmann
Guest
Posts: n/a
 
      08-30-2006
Hi Paul, hi Tom,

thanks for your hints.
The ncurses solution didn't work, WINDOW method isn't known.
But no problem, I'm glad about every idea.

In the meanwhile I've solved the problem by using `stty`.

===---===---===---===---===---===
#!/usr/bin/ruby -w
c = `stty size`.chomp
c =~ /\s/
lines, columns = $`, $'
===---===---===---===---===---===

Frank

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

 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      08-31-2006
On Wednesday 30 August 2006 19:37, Frank Hofmann wrote:
> Hi Paul, hi Tom,
>
> thanks for your hints.
> The ncurses solution didn't work, WINDOW method isn't known.
> But no problem, I'm glad about every idea.
>
> In the meanwhile I've solved the problem by using `stty`.
>
> ===---===---===---===---===---===
> #!/usr/bin/ruby -w
> c = `stty size`.chomp
> c =~ /\s/
> lines, columns = $`, $'
> ===---===---===---===---===---===


or, to make it a little bit more readable (no offend, but yours looks like
perl

===---===---===---===---===---===
lines, columns = `stty size`.chomp.split
# ["81", "226"]
===---===---===---===---===---===

thanks for finding that out btw, i love it

MfG
manveru

>
> Frank


 
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
ENV['LD_LIBRARY_PATH'] not changing env TDR Ruby 3 08-31-2007 06:01 PM
WMI Python, writing remotely and retrieving env variables values Thierry Lam Python 2 01-13-2007 11:23 AM
SAXException: Attribute "xmlns:env" was already specified for element "env:Envelope' Ankit Mehta Java 1 09-29-2006 09:24 PM
MCSE - 2003 Env. jen MCSE 6 11-18-2003 09:34 PM
XmlValidatingReader throws exception for SAOP-ENV:encodingStyle attribute Himmat Dhange ASP .Net 0 08-26-2003 08:28 PM



Advertisments