Useful Bash functions to determine OS and more

In a number of my Bash aliases I need to check two constants: what sort of OS I’m on and whether it’s a production box or not. I use the former for aliases that allow me to install and search for packages across distributions, as described here. The latter I use to determine whether I am allowed to use aliases for turning off the machine and the like. The code I used to determine these before was repetitive and hard to work with. So I created functions to determine the values, allowing all the aliases to reference a single source.

The first function determines the OS I am on, in the general categories of “debian” and “redhat”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function cur_os {
    # Attempts to check whether the local box is running Debian/Ubuntu
    # (considered together as "debian") or Red Hat/CentOS (considered
    # together as "redhat".
    # Make sure we have egrep
    EGREP_VER=`egrep --version | head -n 1`
    if [[ "${EGREP_VER:0:8}" != "GNU grep" ]] ; then
        echo "egrep isn't installed, sorry."
        exit 1
    fi
    # Make sure we have /etc/issue
    if [[ ! -r '/etc/issue' ]] ; then
        echo "/etc/issue isn't readable."
        exit 1
    fi
    # Run checks
    DEB_OS=`egrep -i 'Ubuntu|Debian' /etc/issue`
    RH_OS=`egrep -i 'CentOS|Red Hat' /etc/issue`
    if [[ ${#DEB_OS} -gt 0 ]] ; then
        CUR_OS="debian"
    elif [[ ${#RH_OS} -gt 0 ]] ; then
        CUR_OS="redhat"
    else
        CUR_OS="unknown"
    fi
    echo $CUR_OS
}

I chose these two categories because the package handler is the same within each. This could of course be expanded to more systems, these just happen to be the flavors I am always on. Just after I define this function I also include:

1
cur_os > /dev/null

This way the variable CUR_OS is populated each time Bash loads but the output doesn’t dirty up the prompt. Having this variable allows the definition of fairly elegant functions such as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function inst () {
    # Installs passed packages.
    case "$CUR_OS" in
    'debian')
        sudo apt-get install $@
        ;;
    'redhat')
        sudo yum install $@
        ;;
    *)
        echo "You are not on a supported OS."
        ;;
    esac
}

This next function determines whether the current machine is one I consider non-production:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function local_box {
    # Checks if the current machine is one of my own machines (and thus
    # less risky to run various commands on).
    LOCAL_BOXES=( 'ZenSam' 'B74kb0x' 'MediaServer' 'shuckins-alienware' 'laptop-shuckins' )
    CURRENTBOX=`hostname`
    CUR_BOX_LOCAL=0
    for i in ${LOCAL_BOXES[@]}; do
        if [[ $i == ${CURRENTBOX} ]] ; then
            CUR_BOX_LOCAL=1
        fi;
    done
    echo $CUR_BOX_LOCAL
}
# So that CUR_BOX_LOCAL is set initially:
local_box > /dev/null

The idea is that LOCAL_BOXES holds hostnames of machines that aren’t as sensitive to things like shutting down. Then when you create aliases for such potentially destructive actions you define a safe form for the potentially sensitive boxes:

1
2
3
4
5
6
7
if [[ $CUR_BOX_LOCAL == 1 ]] ; then
    alias off="sudo shutdown -h now"
    alias restart="sudo shutdown -r now"
else
    alias off="echo 'You are on a remote box!'"
    alias restart="echo 'You are on a remote box!'"
fi

Post to Twitter Post to Delicious Post to Digg Post to Reddit

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in CLI, Programming and tagged , . Bookmark the permalink.

One Response to Useful Bash functions to determine OS and more

  1. Pingback: Multi-OS package installation and search functions | tail -f findings.out

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>