Archive for July, 2010

colors: Xterm Text Attribute Display/Test

Posted on July 9, 2010. Filed under: Scripts |

A recent discussion in another forum reminded me of this little script I wrote.   While I don’t change text attributes very often, I got tired of tracking down this information every time I wanted to do that.

This script not only documents every possible Xterm text attribute escape sequence, but it acts as a test of each one — so there is no question as to what attributes are supported in the current environment.

While the script is currently set to run via ksh, it works perfectly fine under bash as well.


#!/usr/bin/ksh
#
# colors
#
# Everything you want to know about modifying the display of text: Bolding,
# underline, flash, reverse, color, background...
#
# This script contains all the various text attribute codes and shows how
# they look (and if they are available in this specific situation).
#
# MODIFICATION LOG (most recent first)
# DATE      WHO DESCRIPTION
# ========= === =============================================================
# 09-Jul-10 KRH Converted to work under most ksh and bash.
# 05-Mar-09 KRH Added attributes that are usually not supported.  This is,
#               after all, documentation.
# 05-Aug-08 KRH Initial creation.
# ========= === =============================================================

# Color codes, with names
i=0
COLOR[i]="30";          NAME[i]="Black";                (( i += 1 ))
COLOR[i]="31";          NAME[i]="Red";                  (( i += 1 ))
COLOR[i]="32";          NAME[i]="Green";                (( i += 1 ))
COLOR[i]="33";          NAME[i]="Yellow";               (( i += 1 ))
COLOR[i]="34";          NAME[i]="Blue";                 (( i += 1 ))
COLOR[i]="35";          NAME[i]="Magenta";              (( i += 1 ))
COLOR[i]="36";          NAME[i]="Cyan";                 (( i += 1 ))
COLOR[i]="37";          NAME[i]="White";                (( i += 1 ))

# Background color codes, with names
i=0
BGCOLOR[i]="40";        BGNAME[i]="Black";              (( i += 1 ))
BGCOLOR[i]="41";        BGNAME[i]="Red";                (( i += 1 ))
BGCOLOR[i]="42";        BGNAME[i]="Green";              (( i += 1 ))
BGCOLOR[i]="43";        BGNAME[i]="Yellow";             (( i += 1 ))
BGCOLOR[i]="44";        BGNAME[i]="Blue";               (( i += 1 ))
BGCOLOR[i]="45";        BGNAME[i]="Magenta";            (( i += 1 ))
BGCOLOR[i]="46";        BGNAME[i]="Cyan";               (( i += 1 ))
BGCOLOR[i]="47";        BGNAME[i]="White";              (( i += 1 ))

# Character attributes, with names
i=0
ATTR[i]=0;              ATTRNAME[i]="None";             (( i += 1 ))
ATTR[i]=1;              ATTRNAME[i]="Bold";             (( i += 1 ))
# "Faint" is usually = underline
ATTR[i]=2;              ATTRNAME[i]="Faint";            (( i += 1 ))
# "Italic" is usually = reverse
ATTR[i]=3;              ATTRNAME[i]="Italic";           (( i += 1 ))
ATTR[i]=4;              ATTRNAME[i]="Underline";        (( i += 1 ))
ATTR[i]=5;              ATTRNAME[i]="Blink";            (( i += 1 ))
# RapidBlink is usually is noop
ATTR[i]=6;              ATTRNAME[i]="RapidBlink";       (( i += 1 ))
ATTR[i]=7;              ATTRNAME[i]="Reverse";          (( i += 1 ))
# "Concealed" is usually is noop
ATTR[i]=8;              ATTRNAME[i]="Concealed";        (( i += 1 ))
# "Strikethru" is usually is noop
ATTR[i]=9;              ATTRNAME[i]="Strikethru";       (( i += 1 ))

# Select two backgrounds for color text display
BG1=7           # White
BG2=0           # Black

# Shorthand for the heading, underline and reset codes
H="33[1;4m"   # Header: Bold, underline
R="33[0m"     # Reset
U="33[4m"     # Underline

# Ensure echo supports escape syntax
echo "\c" | grep -q "c" && ECHO="echo -e" || ECHO="echo"

$ECHO "${H}Colors$R"
# Display the header line
printf "${U}On %-11s$R ${U} Code $R ${U}On %-11s$R  " \
    "${BGNAME[BG1]}" "${BGNAME[BG2]}"
printf "${U}On %-11s$R ${U} Code $R ${U}On %-11s$R\n" \
    "${BGNAME[BG1]}" "${BGNAME[BG2]}"

# Display the table of colors on the two backgrounds
i=0
while [[ $i -lt ${#COLOR[*]} ]]
do
    printf "33[${BGCOLOR[BG1]};${COLOR[i]}m %-12s $R%7s " \
        "${NAME[i]}" "(${COLOR[i]})"
    printf "33[${BGCOLOR[BG2]};${COLOR[i]}m %-12s $R  " "${NAME[i]}"
    # BOLD
    printf "33[${BGCOLOR[BG1]};1;${COLOR[i]}m %-12s $R%7s " \
        "${NAME[i]} Bold" "(1;${COLOR[i]})"
    printf "33[${BGCOLOR[BG2]};1;${COLOR[i]}m %-12s $R\n" "${NAME[i]} Bold"
    (( i += 1 ))
done

# Show all the backgrounds
$ECHO "${H}Backgrounds$R"
i=0
while [[ $i -lt ${#BGCOLOR[*]} ]]
do
    printf "33[${BGCOLOR[i]}m    33[0m %-8s (${BGCOLOR[i]})\t" ${BGNAME[i]}
    (( i += 1 ))
    # Show three per line
    [[ $(( i % 3 )) -eq 0 ]] && echo
done
# End previous line, if not done already
[[ $(( i % 3 )) -ne 0 ]] && echo

# Show how the other text attributes look
$ECHO "${H}Other Attributes$R"
$ECHO "These are normally supported:"
for i in 0 1 4 5 7
do
    printf "33[${ATTR[i]}m%-14s33[0m " "${ATTR[i]} (${ATTRNAME[i]})"
done
echo    # End prior display line
$ECHO "These are normally NOT fully supported:"
for i in 2 3 6 8 9
do
    printf "33[${ATTR[i]}m%-14s33[0m " "${ATTR[i]} (${ATTRNAME[i]})"
done
echo    # End prior display line

# A little tutorial
$ECHO "${H}Instructions$R"
$ECHO "Example command: background=black, text=bold+underline+white:"
$ECHO " $ECHO \"\\33[40;1;4;37m\\\c\""
$ECHO "Turn off all attributes with: $ECHO \"\\33[0m\\\c\""

# Show and then execute a demonstration command:
TEST_STR="\\tTest: \33[40;1;4;37mText\33[0m Done"
echo -E "Example test:  $ECHO \"$TEST_STR\""
$ECHO "$TEST_STR"

#
# End of colors.sh
#

Read Full Post | Make a Comment ( None so far )

Boolean Variables In Shell Scripts

Posted on July 6, 2010. Filed under: Tips |

Technically, there is no variable typing in shell scripts.  In most shells, variables are just text strings unless and until evaluated in a different context, most often a numeric context.

Some shells support limited variable typing via their “typeset” command, but that capability is far from universal.

Given that many scripts use variables as flags to indicate some condition was met or not met, it would make things easier if there was a type BOOLEAN.  Let’s look at a typical example:


FILE_FOUND=n
# ... some processing to locate the file
    # File found, set the flag
    FILE_FOUND=y
    # ...
if [[ $FILE_FOUND = y ]]
then
    # process the file...
fi

That’s actually just fine, but, as with other languages, I prefer to use boolean variables for cleaner code.

Even though there is, technically, no such thing, it is quite easy to implement.  It looks like this:


FILE_FOUND=false
# ... some processing to locate the file
    # If found, set the flag
    FILE_FOUND=true
    # ...
if $FILE_FOUND
then
    # process the file...
fi

This works because “true” and “false” are actually commands, so $FILE_FOUND executes the “true” or “false” command.  “true” always returns 0 and “false” always returns 1.  This technique works in all bourne-style shells.

Execution-wise, it really doesn’t make any difference, but I like the cleaner look to the code.

Kimball

Read Full Post | Make a Comment ( None so far )

Liked it here?
Why not try sites on the blogroll...