Web lists-archives.com

Re: VT2 for Xorg?




On Sun, Jul 08, 2018 at 01:08:59PM +1000, Zenaan Harkness wrote:
> With a bash fn like:
>   ttyGetNum () { return $(tty | grep -oE '[0-9]+$') ; }
> 
> Then something like this will id your current tty:
>   ttyGetNum;  export Xorg_vt=vt$?

Ewwwww!

The return status of a shell function is NOT intended to be used this way.
It's analogous to the exit status of a shell command.  It's a single
byte, unsigned, so you can only return 0 to 255.  It's intended to convey
success or failure, not to carry an actual data payload.

I recommend that you switch to using a regular variable for this purpose.
I happen to like "r".  With a regular variable, you're not restricted to
integers from 0 to 255, and you retain the ability to convey succss/failure
through the return code.

As an aside, you can save the call to GNU grep by simply stripping the
leading "/dev/tty" from the output of tty using a shell parameter
expansion:

  ttyGetNum() {
    local t
    t=$(tty) || return 1
    [[ $t = /dev/tty+([0-9]) ]] || return 1
    r=${t#/dev/tty}
  }

  if ttyGetNum; then export Xorg_vt=vt"$r"; fi