CBLFS Bash Startup Files

From CLFS-HINTS

Jump to: navigation, search

Contents

Enhancing Bash

After completing your CLFS system, you may choose to continue with CBLFS to add functionality. The CBLFS Wiki provides some Bash startup scripts to help ensure that your environment is properly established for the instructions found in CBLFS. Here is some additional information that you may find useful for personalizing these scripts for your use.

Bash Colors

The file 50-prompt.sh sets the colors of the Bash prompt and the file 50-dircolors.sh sets the colors used when displaying directory contents. This script will output a table of all the different foreground/background color combinations that you can use in these files as well as any other Bash script. I put mine in /usr/bin because I can't remember all 162 combinations. When you're writing a Bash script and want a little color, you can just execute the script to pick the combination that works best.

cat > /usr/bin/listcolors << "EOF"
#!/bin/bash
#
# This file echoes a bunch of color codes to the terminal to demonstrate what's available.
#Each line is the color code of one foreground color out of 17 (default + 16 escapes),
# followed by a test use of that color on all nine background colors (default + 8 escapes).

T="tUx!"

echo -e "\n                  40m      41m      42m      43m      44m      45m      46m      47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' '1;32m' '  33m' '1;33m'\
           '  34m' '1;34m' '  35m' '1;35m' '  36m' '1;36m' '  37m' '1;37m';

    do FG=${FGs// /}
        echo -en " $FGs \033[$FG  $T  "
        for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
            do echo -en "$EINS \033[$FG\033[$BG  $T  \033[0m";
    done
    echo;
done
echo
EOF

If you are following the CBLFS Wiki to create your Bash startup files, you will generate a system wide color configuration file used by the ls command with this command:

dircolors -p > /etc/dircolors

Here is a part of the output of this file:

# image formats
.jpg 01;35
.jpeg 01;35
.gif 01;35

You can see that it uses the Bash color codes to determine what color is used to display a file based on its extension. In this example, image files are displayed in light purple (1;35 code). Suppose you want your .gif images to standout from the other image types. You could change the above example to this to display gif images in light cyan instead:

# image formats
.jpg 01;35
.jpeg 01;35
.gif 01;36

Bash Functions

You can add functions to your .bashrc script that will enhance the functionality or just do something "neat." Here are some functions that fall into one of these categories.

Command Line Stock Quotes

This function is in the "neat" category. It will allow you to retrieve quote summaries for equities; it doesn't work for funds. It can be executed from the command line with the syntax:

$ stock rht
echo >> $HOME/.bashrc << "EOF"
stock () {

    stockname=`links -dump http://finance.yahoo.com/q?s=${1} | grep -i ":${1})" | sed -e 's/Delayed.*$//'`
    stockadvise="${stockname} - delayed quote."

    declare -a STOCKINFO
    STOCKINFO=(` links -dump http://finance.yahoo.com/q?s=${1} | egrep -i "Last Trade:|Change:|52wk Range:"`)

    stockdata=`echo ${STOCKINFO[@]}`

    if [[ ${#stockname} != 0 ]]; then
        echo "${stockadvise}"
        echo "${stockdata}"
    else
        stockname2=${1}
        lookupsymbol=`links -dump -nolist http://finance.yahoo.com/lookup?s="${1}" | grep -A 1 -m 1 "Portfolio" \
        | grep -v "Portfolio" | sed 's/\(.*\)Add/\1 /'`

        if [[ ${#lookupsymbol} != 0 ]]; then
            echo "${lookupsymbol}"
        else
            echo "Sorry $USER, I can not find ${1}."
        fi
    fi
}
EOF

Bash References

Bash Prompt HOWTO
Advanced Bash Scripting
Personal tools