[comp.unix.questions] get terminal speed from shell script

sun@me.utoronto.ca (Andy Sun Anu-guest) (08/13/90)

Hi Net,

The question I have is:

Is there a way to get the terminal speed from a (sh or csh) script?

I used to be able to do the following in a Bourne shell script:

			speed=`stty speed`

and got the terminal speed assigned to variable speed. As various OS
gets updated (e.g. Ultrix 3.1 and SUN OS 4.0.3), this won't work anymore
because all stty outputs are being sent to stderr, not stdout, thus no
piping or redirection is possible. Is there any similar commands that can 
get terminal speed inside a shell script?

Andy

_______________________________________________________________________________
Andy Sun                            | Internet: sun@me.utoronto.ca
University of Toronto, Canada       | UUCP    : ...!utai!me!sun
Dept. of Mechanical Engineering     | BITNET  : sun@me.utoronto.BITNET

ruhtra@turing.toronto.edu (Arthur Tateishi) (08/13/90)

In artcle <90Aug12.135618edt.18763@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:

>Is there a way to get the terminal speed from a (sh or csh) script?
>I used to be able to do the following in a Bourne shell script:
>			speed=`stty speed`
>and got the terminal speed assigned to variable speed. As various OS
>gets updated (e.g. Ultrix 3.1 and SUN OS 4.0.3), this won't work anymore
>because all stty outputs are being sent to stderr, not stdout, thus no
>piping or redirection is possible. Is there any similar commands that can 
>get terminal speed inside a shell script?

I find it odd that the SUN man pages still specify option info is output
on stdout...  Maybe I shouldn't be surprised.

However, I since stty seems to go bonkers when stdout goes to something
other than a proper tty device, I came up with the following.
		speed=`stty speed 3>&2 2>&1 1>&3` 
This essentially swaps stdout and stderr with a dangling file descriptor 3
which could be closed with 3>&- however it shouldn't matter. Be warned,
if stderr has been modified to be a socket going somewhere, it will likely
blow up with 'stty: Operation not supported on socket.' so you could use
		speed=`stty speed 2>&1 1>/dev/tty` 
which also has its drawbacks. 
Sorry, I can't confirm this for Ultrix. 

-- 
``Sex and drugs? They're nothing compared with a good proof!''
                                - A Cambridge student  (r.h.f)
Arthur Tateishi                 g9ruhtra@zero.cdf.utoronto.edu

sun@me.utoronto.ca (Andy Sun Anu-guest) (08/13/90)

In article <1990Aug13.005849.23223@jarvis.csri.toronto.edu> ruhtra@turing.toronto.edu (Arthur Tateishi) writes:
>In artcle <90Aug12.135618edt.18763@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
>
>>Is there a way to get the terminal speed from a (sh or csh) script?
>>I used to be able to do the following in a Bourne shell script:
>>			speed=`stty speed`
>>and got the terminal speed assigned to variable speed. As various OS
>>gets updated (e.g. Ultrix 3.1 and SUN OS 4.0.3), this won't work anymore
>>because all stty outputs are being sent to stderr, not stdout, thus no
>>piping or redirection is possible. Is there any similar commands that can 
>>get terminal speed inside a shell script?
>
>I find it odd that the SUN man pages still specify option info is output
>on stdout...  Maybe I shouldn't be surprised.
>

I guess it depends on which version of SUN OS. "speed=`stty speed`" works
for a Sun 3/60 running (I think) SUN OS 3.x. So it does went through stdout.
I tried the same thing on a SPARCstation running Sun OS 4.0.3c and it
gave me "Operation not supported on socket". What you suggested below, i.e,
"speed=`stty speed 2>&1 1>/dev/tty` works on the SPARCstation.

>However, I since stty seems to go bonkers when stdout goes to something
>other than a proper tty device, I came up with the following.
>		speed=`stty speed 3>&2 2>&1 1>&3` 
>This essentially swaps stdout and stderr with a dangling file descriptor 3
>which could be closed with 3>&- however it shouldn't matter. Be warned,
>if stderr has been modified to be a socket going somewhere, it will likely
>blow up with 'stty: Operation not supported on socket.' so you could use
>		speed=`stty speed 2>&1 1>/dev/tty` 
>which also has its drawbacks. 
>Sorry, I can't confirm this for Ultrix. 

I confirmed it, neither works :-)
speed=`stty speed` gave me "Not a typewriter" and
speed=`stty speed  2&1 1>/dev/tty` echoes the speed on the screen but assigns
nothing to the variable speed. I don't know why it works for SPARCstation
because I think /dev/tty is not the same as stdout. If in a C program you
specify /dev/tty as your output stream instead of stdout, everything echoes
on the screen in both cases but you cannot use redirection with /dev/tty.

>
>-- 
>``Sex and drugs? They're nothing compared with a good proof!''
>                                - A Cambridge student  (r.h.f)
>Arthur Tateishi                 g9ruhtra@zero.cdf.utoronto.edu


Will they ever home on a standard on this one? (sighed)

Andy

_______________________________________________________________________________
Andy Sun                            | Internet: sun@me.utoronto.ca
University of Toronto, Canada       | UUCP    : ...!utai!me!sun
Dept. of Mechanical Engineering     | BITNET  : sun@me.utoronto.BITNET

jimr@hp-lsd.COS.HP.COM (Jim Rogers) (08/13/90)

If your version of stty writes only to stderr then you can redirect
that in a useful manner:

	speed=`stty speed 2>&1`

This will place the the speed of your terminal in the variable speed even
if stty writes to stderr.


Jim Rogers
Hewlett Packard Company

george@hls0.hls.oz (George Turczynski) (08/14/90)

On SunOS 4.0.3 (and probably the rest too), if you have installed
/usr/5bin then use:

			speed=`/usr/5bin/stty speed`

in your shell scripts.  This works.

By the way, the SUN manuals point out quite clearly that

	"the settings are reported on the standard error."

for /bin/stty, and

	"the settings are reported on the standard output"

for /usr/5bin/stty.

Be sure to re-read the manual to check that the different versions
apply to the device you want.  ie /bin/stty sets/reports the stdout device,
whilst /usr/5bin/stty sets/reports the stdin device.

Hope this is of some use to you...

-- 
| George P. J. Turczynski.          |---------------------------------------------------- 
| Computer Systems Engineer.        | ACSnet: george@highland.oz | I can't speak for the |
| Highland Logic Pty. Ltd.          | Phone: +61 48 683490       | company, I can barely |
| Suite 1, 348-354 Argyle St        | Fax:   +61 48 683474       | speak for myself...   |
| Moss Vale. NSW. Australia. 2577   |---------------------------------------------------- 

bob@wyse.wyse.com (Bob McGowen x4312 dept208) (08/14/90)

In article <90Aug12.135618edt.18763@me.utoronto.ca> sun@hammer.me.UUCP (Andy Sun Anu-guest) writes:
  >Hi Net,
  >
  >The question I have is:
  >
  >Is there a way to get the terminal speed from a (sh or csh) script?
  >
  >I used to be able to do the following in a Bourne shell script:
  >
  >			speed=`stty speed`
  >
  >and got the terminal speed assigned to variable speed. As various OS
  >gets updated (e.g. Ultrix 3.1 and SUN OS 4.0.3), this won't work anymore
  >because all stty outputs are being sent to stderr, not stdout, thus no
  >piping or redirection is possible. Is there any similar commands that can 
You CAN still cause redirection to occur.
  >get terminal speed inside a shell script?

I just tried the following:

   date>date.data
   echo 'echo date.data >&2 # sends file name to standard error' > errout
   chmod +x errout
   cat `errout` # date.data appeared on screen, cat read terminal
   		# this is the expected result
   cat `errout 2>&1` # the date stored in the file was cat'ed to the screen

Of course, this is the Bourne shell, not csh.  I cannot vouch for how or if
this is possible with csh.

Bob McGowan  (standard disclaimer, these are my own ...)
Product Support, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com

gerry@jts.com (Gerry Roderick Singleton ) (08/15/90)

In article <90Aug13.095129edt.18647@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
>In article <1990Aug13.005849.23223@jarvis.csri.toronto.edu> ruhtra@turing.toronto.edu (Arthur Tateishi) writes:
>>In artcle <90Aug12.135618edt.18763@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
>>
>>>Is there a way to get the terminal speed from a (sh or csh) script?
>>>I used to be able to do the following in a Bourne shell script:
>>>			speed=`stty speed`

[lines deleted]

>
>I guess it depends on which version of SUN OS. "speed=`stty speed`" works
>for a Sun 3/60 running (I think) SUN OS 3.x. So it does went through stdout.

[more lines deleted]

>>other than a proper tty device, I came up with the following.
>>		speed=`stty speed 3>&2 2>&1 1>&3` 

[more lines deleted]


I like to add my two penny's worth,  to query the speed of the tty device
shouldn't one accept input from the device rather than perform output
to it before getting the answer?  The question is rhetorcal so don't bother
answering it.  It apppears to me to be valid in this case for BSD4.3 running
on an ISIv24.  Here's my little sample:


#! /bin/sh

speed=`stty speed </dev/tty 2>&1`
echo $speed


and the results:

 /usr/local/src >%[530] sh -vx foo
 #! /bin/sh
  
  speed=`stty speed </dev/tty 2>&1`
  + stty speed 
  speed=9600
  echo $speed
  + echo 9600 
  9600
   /usr/local/src >%[531] 

I believe this satisfies Andy's requirements and I hope it's what he wanted.
Anyways it'll fuel the discussion.

Cheers,
ger
-- 
--
G. Roderick Singleton, System and Network Administrator, JTS Computers 
	{uunet | geac | torsqnt}!gerry@jtsv16.jts.com

gt0178a@prism.gatech.EDU (BURNS,JIM) (08/15/90)

in article <1990Aug14.181010.29571@jts.com>, gerry@jts.com (Gerry Roderick Singleton ) says:
> #! /bin/sh
> 
> speed=`stty speed </dev/tty 2>&1`
> echo $speed

Nope, on SunOS 4.0, you get:

{richsun12:/usr}
[196] t=`stty speed </dev/tty 2>&1`
{richsun12:/usr}
[197] echo $t
stty: Operation not supported on socket
{richsun12:/usr}
[198]
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

lew@gsg.UUCP (Paul Lew) (08/16/90)

bob@wyse.wyse.com (Bob McGowen x4312 dept208) writes:

>In article <90Aug12.135618edt.18763@me.utoronto.ca> sun@hammer.me.UUCP (Andy Sun Anu-guest) writes:
>  >Is there a way to get the terminal speed from a (sh or csh) script?

>Of course, this is the Bourne shell, not csh.  I cannot vouch for how or if
>this is possible with csh.

Try:	set speed = `(stty speed > /dev/tty) |& cat`

Layers of I/O redirections usually can solve these kind of problems.
-- 
Paul Lew (lew@gsg.gsg.com)		UUCP:		oliveb---+
						uunet---samsung--+
General Systems Group, 5 Manor Parkway			harvard--+--gsg--lew
Salem, NH 03079	(603) 893-1000				decvax---+

gerry@jts.com (Gerry Roderick Singleton ) (08/17/90)

In article <12584@hydra.gatech.EDU> gt0178a@prism.gatech.EDU (BURNS,JIM) writes:
>in article <1990Aug14.181010.29571@jts.com>, gerry@jts.com (Gerry Roderick Singleton ) says:
>> #! /bin/sh
>> 
>> speed=`stty speed </dev/tty 2>&1`
>> echo $speed
>
>Nope, on SunOS 4.0, you get:
>
>{richsun12:/usr}
>[196] t=`stty speed </dev/tty 2>&1`
>{richsun12:/usr}
>[197] echo $t
>stty: Operation not supported on socket
>{richsun12:/usr}
>[198]
>-- 

Hmm, that's true when you're in a window.  I did not interpret the
original question as being window specific but the more general
case of working with ttys and pttys.  The script DOES work for real
ttys and ti even works on pttys over RPC links.  Here's the output
under these circumstances as executed with /bin/sh -vx foo, where foo
is the four lines above:

#! /bin/sh

speed=`stty speed </dev/tty 2>&1`
+ stty speed 
speed=9600
echo $speed
+ echo 9600 
9600

I have no window system solution, so I hope one of the window system
gurus can help.

ger
-- 
--
G. Roderick Singleton, System and Network Administrator, JTS Computers 
	{uunet | geac | torsqnt}!gerry@jtsv16.jts.com

gt0178a@prism.gatech.EDU (BURNS,JIM) (08/17/90)

in article <1990Aug17.034401.12720@jts.com>, gerry@jts.com (Gerry Roderick Singleton ) says:
> Hmm, that's true when you're in a window.  I did not interpret the
> original question as being window specific but the more general
> case of working with ttys and pttys.  The script DOES work for real

I was on a 'screen' window on one host, doing an rlogin to the SUN. I was
not in a window as far as the Sun was concerned, although rlogin was
probably using a socket. This saved me the trouble of being physically at
the Sun. Are you saying this is stll justified behavior for the Sun? Rlogin-
ing to a Mac A/UX, or even back to my host, does not cause problems w/stty.

> ttys and ti even works on pttys over RPC links.  Here's the output
> under these circumstances as executed with /bin/sh -vx foo, where foo
> is the four lines above:

> #! /bin/sh

> speed=`stty speed </dev/tty 2>&1`
> + stty speed 
> speed=9600
> echo $speed
> + echo 9600 
> 9600

> I have no window system solution, so I hope one of the window system
> gurus can help.

The previously posted solutions of

speed=`stty speed 3>&1 1>&2 2>&3`

or

speed=`/usr/5bin/stty speed`

work.
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (08/19/90)

And here's yet another thread that pty gives an easy answer to:

sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
> Is there a way to get the terminal speed from a (sh or csh) script?
  [ speed=`stty speed` doesn't work with newer stty versions ]

speed=`pty stty speed`. 'Nuff said.

---Dan

gerry@jts.com (Gerry Roderick Singleton ) (08/20/90)

In article <12584@hydra.gatech.EDU> gt0178a@prism.gatech.EDU (BURNS,JIM) writes:
>in article <1990Aug14.181010.29571@jts.com>, gerry@jts.com (Gerry Roderick Singleton ) says:
>> #! /bin/sh
>> 
>> speed=`stty speed </dev/tty 2>&1`
>> echo $speed
>
>Nope, on SunOS 4.0, you get:
>
>{richsun12:/usr}
>[196] t=`stty speed </dev/tty 2>&1`
>{richsun12:/usr}
>[197] echo $t
>stty: Operation not supported on socket
>{richsun12:/usr}
>[198]
>-- 

Yup, you sure do but the following works on SunOS3.5, SunOS 4.0.3x, BSD4.3(isi)
and SPARC 4.1:

#! /bin/sh
speed= eval 'stty speed'
echo $speed


Have fun,
ger
-- 
--
G. Roderick Singleton, System and Network Administrator, JTS Computers 
	{uunet | geac | torsqnt}!gerry@jtsv16.jts.com

guy@auspex.auspex.com (Guy Harris) (08/29/90)

>By the way, the SUN manuals point out quite clearly that
>
>	"the settings are reported on the standard error."

Yes, but they also point out equally clearly that

  Special Requests
     speed       The terminal speed alone is printed on the stan-
                 dard output.

     size        The terminal (window) sizes are printed  on  the
                 standard output, first rows and then columns.

                 size and speed always report on the settings  of
                 /dev/tty,  and always report the settings to the
                 standard output.

so "stty speed" and "stty size", in *both* environments, report the
speed and size to the standard output.

The problem is that the BSD-environment "stty" normally fetches a bunch
of mode settings from the standard output when it starts up, and if that
fails, it quite reasonably reports this fact. 

However, the failure is of no consequence if you're just doing "stty
speed" or "stty size", since the only data it uses comes from its
"ioctl" on "/dev/tty", so it shouldn't worry about the "ioctl"s on the
standard output (or shouldn't even do them in the first place!).

I think this bug is fixed in 4.1.