Thursday, December 6, 2007

Setting $DISPLAY in bash and csh

For Bash, open the .bashrc file in you home directory in you favorite editor vi and append the following script at the end of it:

#
# Set the DISPLAY
#
if [ x"$DISPLAY" = x ]
then
TTYPORT=`tty`
if [ "$TTYPORT" = "/dev/console" ]
then
DISPLAY="localhost:0"
elif [ "$REMOTEHOST" = "" ]
then
TTYNAME=`echo $TTYPORT | cut -c6-`
REMOTEHOST=`who|grep "$TTYNAME"|awk '{print $5}'|sed 's/(//'|sed 's/)//'`
DISPLAY="${REMOTEHOST}:0"
else
DISPLAY="${REMOTEHOST}:0"
fi
fi


For Csh, open the .cshrc file in you home directory in you favorite editor vi and append the following script at the end of it:

#
# Set DISPLAY
#
if ( ! $?DISPLAY ) then
set TTYPORT=`tty`
if ( $TTYPORT == /dev/console ) then
setenv DISPLAY "localhost:0"
else if ( $?REMOTEHOST ) then
setenv DISPLAY "${REMOTEHOST}:0"
else
set TTYNAME=`echo $TTYPORT |cut -c6-`
set REMOTEHOST=`who|grep "$TTYNAME"|awk '{print $6}'|sed 's/(//'
|sed 's/)//'`
setenv DISPLAY "${REMOTEHOST}:0"
endif
endif

Note: The csh script is tested on Sun Solaris and the bash script is tested on Ubuntu and Fedora so is expected to work on other flavors of linux too. If you have a different *Nix system then check the output of 'who' and configure your awk accordingly.

No comments: