Help with setting LANG conditionally based on terminal type

Help with setting LANG conditionally based on terminal type

Post by Jeffrey J. Kosows » Thu, 19 Dec 2002 14:25:51



I would like to set the LANG conditionally depending on whether I am
at the console (where UTF encoding works) or whether I am logging in
remotely (e.g., putty over SSH or cygwin) where UTF is not properly
supported.

Currently, I am using the following code in my .bashrc file:

if [[ !( `tty` == /dev/tty* || $DISPLAY == :0 || $DISPLAY == :0.0 ) ]]; then
        export LANG=C
fi

This is a bit of a hack and is not as precise as I would like.
For example, it seems that the extglob shopt doesn't work in .bashrc,
so I can't use the more precise regexp /dev/tty+[0-9]. Similarly, I
would prefer to use a single regexp for the display matching, such as :0?(.0)

Any suggestions on better ways of doing this?

Thanks,
Jeff

 
 
 

Help with setting LANG conditionally based on terminal type

Post by * Tong » Sat, 21 Dec 2002 07:08:48



Quote:> I would like to set the LANG conditionally depending on whether I am
> at the console (where UTF encoding works) or whether I am logging in
> remotely (e.g., putty over SSH or cygwin) where UTF is not properly
> supported.

> Currently, I am using the following code in my .bashrc file:

> if [[ !( `tty` == /dev/tty* || $DISPLAY == :0 || $DISPLAY == :0.0 ) ]]; then
>    export LANG=C
> fi

> This is a bit of a hack and is not as precise as I would like.
> For example, it seems that the extglob shopt doesn't work in .bashrc,
> so I can't use the more precise regexp /dev/tty+[0-9]. Similarly, I
> would prefer to use a single regexp for the display matching, such as :0?(.0)

Use 'expr'. will solve all your problem. beside, it will be /bin/sh
compatible also (your '[[' will cease to work when bash is not
normally available, say Solaris).

A simple example to get you started:

$ v="version1234."

$ expr $v : '.*n\([0-9][0-9]*\).*'
1234

--
Tong (remove underscore(s) to reply)
  *niX Power Tools Project: http://xpt.sourceforge.net/
  - All free contribution & collection

 
 
 

Help with setting LANG conditionally based on terminal type

Post by Chris F.A. Johnso » Sat, 21 Dec 2002 16:08:20




>> I would like to set the LANG conditionally depending on whether I am
>> at the console (where UTF encoding works) or whether I am logging in
>> remotely (e.g., putty over SSH or cygwin) where UTF is not properly
>> supported.

>> Currently, I am using the following code in my .bashrc file:

>> if [[ !( `tty` == /dev/tty* || $DISPLAY == :0 || $DISPLAY == :0.0 ) ]]; then
>>        export LANG=C
>> fi

>> This is a bit of a hack and is not as precise as I would like.
>> For example, it seems that the extglob shopt doesn't work in .bashrc,
>> so I can't use the more precise regexp /dev/tty+[0-9]. Similarly, I
>> would prefer to use a single regexp for the display matching, such as :0?(.0)

> Use 'expr'. will solve all your problem. beside, it will be /bin/sh
> compatible also (your '[[' will cease to work when bash is not
> normally available, say Solaris).

    With a POSIX-type shell such as bash, there is little reason to
    use expr. Most versions of Unix, including Solaris, will have a
    POSIX shell, usually ksh on commercial *nixes. Besides, Jeffrey
    wants to put it in .bashrc.

    In this instance, I wouldn't try to cram all the tests into a
    single line; it will make the script much harder to modify or
    debug should that become necessary.

    I'd probably use 2 case statements that make the logic clear
    (adjust to taste):

case `tty` in
   /dev/tty[0-9]*) ;;
   *) LANG=C ;;
esac

case $DISPLAY in
   :0|:0.0) ;;
   *) LANG=C ;;
esac

export LANG

    This also has the advantage that it will run on any Bourne-type
    shell (/bin/sh) should the need arise.

Quote:> A simple example to get you started:

> $ v="version1234."

> $ expr $v : '.*n\([0-9][0-9]*\).*'
> 1234

    Most string manipulation can be done in a POSIX shell without
    needing any external command.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Help with setting LANG conditionally based on terminal type

Post by Jeffrey J. Kosows » Sat, 21 Dec 2002 22:25:44



Quote:> case `tty` in
>    /dev/tty[0-9]*) ;;
>    *) LANG=C ;;
> esac

> case $DISPLAY in
>    :0|:0.0) ;;
>    *) LANG=C ;;
> esac

> export LANG

Thanks!
 
 
 

Help with setting LANG conditionally based on terminal type

Post by Jeffrey J. Kosows » Sat, 21 Dec 2002 22:50:53



Quote:> case `tty` in
>    /dev/tty[0-9]*) ;;
>    *) LANG=C ;;
> esac

> case $DISPLAY in
>    :0|:0.0) ;;
>    *) LANG=C ;;
> esac

> export LANG

Ooops - just realized that this doesn't quite reproduce the logic of
the below statement
if [[ !( `tty` == /dev/tty[0-9]* || $DISPLAY == :0 || $DISPLAY == :0.0 ) ]]; then

i.e. NOT (A or B ) = (NOT A) AND (NOT B)
while case statement gives (NOT A) OR (NOT B)

 
 
 

Help with setting LANG conditionally based on terminal type

Post by Chris F.A. Johnso » Sun, 22 Dec 2002 06:32:30




>> case `tty` in
>>    /dev/tty[0-9]*) ;;
>>    *) LANG=C ;;
>> esac

>> case $DISPLAY in
>>    :0|:0.0) ;;
>>    *) LANG=C ;;
>> esac

>> export LANG

> Ooops - just realized that this doesn't quite reproduce the logic of
> the below statement
> if [[ !( `tty` == /dev/tty[0-9]* || $DISPLAY == :0 || $DISPLAY == :0.0 ) ]]; then

> i.e. NOT (A or B ) = (NOT A) AND (NOT B)
> while case statement gives (NOT A) OR (NOT B)

    Which is why I said "adjust to taste".

case `tty` in
   /dev/tty[0-9]*) ;;
   *) case $DISPLAY in
        :0|:0.0) ;;
        *) LANG=C ;;
      esac
esac

   More readable and extensible:

nottty=0
nice=0
case `tty` in
   /dev/tty[0-9]*) ;;
   *) nottty=1 ;;
esac

case $DISPLAY in
   :0|:0.0) ;;
   *) nice=1 ;;
esac

if [ $(( $nottty + $nice )) -eq 2 ]
then
    LANG=C
fi

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. Can't set terminal type of pseudo terminals

I am trying to set the terminal type of a dumb terminal connecting to my
redhat 5.2 box via a telnet session. I suspect the improper terminal
type is messing with my inability to Backspace.

At  the moment the TERM environment variable is set to "network" but I
want it to be a vt420. The connections are seen as pseudo tty's e.g.
ttyp0, ttyp1, etc

I have tried putting the following in /etc/inittab:-
p0:23:once:/sbin/getty 9600 ttyp0 vt420

This didn't make any difference, however.

Any suggestions appreciated.

Gary Goozee

2. Question on Makefiles

3. Help: setting a terminal type without a shell program.

4. whats better.. freebsd or linux?

5. Problem setting terminal type for serial ports (Help!)

6. How to kill message in Usenet?

7. telnetd refuses connections based on terminal type: what to do

8. undefined reference during kernel recompile?

9. apache: setting handler based on mime type only?

10. Apache : Setting response headers conditionally

11. make: How to set macro variable conditionally at runtime

12. How do I set my terminal type?

13. Sol2.x: setting terminal type in login