Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I currently use an alias in csh/tcsh to open shells with specific window colors.
Example: xterm -geometry 105x25 -font 8x13 -bc -bg #333 -fg #999 \!* &

What I'd like to do is have one command open a new shell with a color based on the level the shell is in a tree.

Example:

  1. %> xt
    Open shell A (white bg/black fg) - new shell
  2. %> xt
    Open shell A.a (black bg/grey fg) - child shell
  3. %> xt
    Open shell A.a.a (green bg/yellow fg) - grandchild shell

I have bash available, but I use tcsh or csh by default.
As stated, I would like one command/alias to open the parent and child shells and decide what color to use.

share|improve this question

migrated from stackoverflow.com Jul 28 '10 at 14:20

This question came from our site for professional and enthusiast programmers.

1  
What's the perl tag for? –  Dennis Williamson Jul 28 '10 at 22:59
add comment

2 Answers

Most shells (tcsh, bash, zsh, but not csh) increment the SHLVL environment variable when they start, so that $SHLVL indicates the level of nesting of your current shell. So do something like (for tcsh)

set xt_bg_colors=(white white black green)
set xt_fg_colors=(black black gray yellow)
alias xt "xterm -geometry 105x25 -font 8x13 -bc -bg $xt_bg_colors[$SHLVL] -fg $xt_fg_colors[$SHLVL] \!* &"
share|improve this answer
add comment

I would create an array variable with the list of color specifications in it (or two arrays - one for fg and one for bg). Then, in your alias select from that array using an index variable, then increment it.

Demo:

> set array=(apple banana cherry)
> @ index = 1
> @ index = ( ( $index ) % ${#array} + 1 ) ; echo $array[$index]

Each time you execute the last line (which represents your alias), the current item in the array is echoed, the index is incremented and then the index wraps around if it exceeds the length of the array.

You may need to use a file to keep track of the index instead of an in-memory variable.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.