[SLUG] Bash help

Gavin Baker gav at supercowpowers.org
Sun Sep 5 23:31:13 BST 2004


Hi Phil,

Phil Kershaw wrote:

><snip>
>
>So I have been trying to set up bashrc to test whether I have opened an
>aterm or an xterm. Depending which one is open I use a different file to
>set dircolors variable. The listing is :-
>
>If [ $TERM = aterm ]; then
>	eval `dircolors -b ~/.dircolorsa
>Elif [ $TERM = Xterm ]; then
>	eval `dircolors -b ~/.dircolorsx
>fi
><snip>
>  
>
$TERM is the terminal 'type', Its used to get information about your 
terminals capabilities.

Have a quick look at 'man 7 term' for the kind of things it's used for.

Most X-Terminal-Emulators set TERM to xterm.

I don't think either app has it's own $HOME/.(a|x)termrc so you can't 
just add it there :(

Here is one idea how to do it. The parent process ID of your shell will 
be the terminal emulator that started it. $PPID is the parent process 
ID, so you could just look at the $PPID's name.

    ps -A | grep $PPID

That will give you results like

    25325 pts/3    00:00:00 aterm

for example.

So if you want to, say, check if the current shell is running in an 
aterm, you could do

    ps -A | grep "$PPID.*aterm$"
    # grep returns a 0 if the regex matched, 1 if not.

    if [ $? -eq 0 ];then
        echo "We are an aterm"
        # Do atermy type things...
    fi

Where $? is the return code of the previously run command, and 
$PPID.*aterm$ means to look for a line (from ps -A) that contains the 
pid number of the parent process (bashs' $PPID), followed by any number 
of other characters (.*) followed by 'aterm', followed by the end of 
line ($).

That will work in your bashrc, but be careful about testing it in a 
script, as the scripts $PPID will be the bash process that ran it :)  
(you can 'source' it in a script though).

You could also just make scripts for xterm and aterm and add them to 
your $PATH in front of the systems of course, but I think the idea above 
is far cooler :)

Good luck!

Regards,
Gav






More information about the Scarborough mailing list