Bash Shell Scripting Triple Play Part 2

1) Loop through a variable line by line and modify it (in my case, add spaces before it).

awk '{ print "   " $0 }' "${VAR}"

… or if you don’t just want to echo it …

LINE=$(awk '{ print "   " $0 }' "${VAR}")

2) Sometimes when you are editing in vim, and you attempt to put a pound sign in the front of a line (well, anywhere really, but I’m frequently putting it in the front to comment out a line) and you aren’t in insert mode, you end up highlighting all instances of the word your cursor was on. To get rid of that, press Ctrl+C then type :noh and press enter — voila.

3) A script that will look in your /etc/vpnc/ directory for configuration (*.conf) files and display them. Some info taken from here.

#!/bin/bash

CONF_DIR="/etc/vpnc/"
CONF_LST=$(find "${CONF_DIR}"*.conf)

bold='\033[1m'

red='\033[0;31;40m'
green='\033[0;32;40m'
blue='\033[0;34;40m'

rst='\033[0m'

function crst()
{
        tput sgr0
}

function cecho()
{
        # $1 = msg
        # $2 = color
        # cecho "Jim is cool" $bold"

        echo -ne "${2}"
        echo -e  "${1}"
        echo -ne "${rst}"
        crst
}

for CONF in ${CONF_LST[@]} ; do
        cecho "${CONF}" $green

        awk '{ print "   " $0 }' "${CONF}"
done

The output looks like:

$ vpncl
/etc/vpnc/default.conf
   #IPSec gateway my.vpn.gateway
   #IPSec ID my.ipsec.id
   #IPSec secret mysecret
   # your username goes here:
   #Xauth username
/etc/vpnc/work-remote.conf
   IPSec gateway my.gateway.net
   IPSec ID groupaccess
   IPSec secret grouppassword
   Xauth username myusername
/etc/vpnc/work-wireless.conf
   IPSec gateway wifi.gateway.net
   IPSec ID wifigroupaccess
   IPSec secret wifigrouppassword
   Xauth username myusername

4) *Bonus*: Modify the file from #3 to the script below, name it vpnc, and then place it in a higher priority path location (to find out higher path priority locations, type echo $PATH and pick one one of the first directories:

$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/username/bin

I choose /usr/local/bin:

$ ls /usr/local/bin
vpnc

vpnc

#!/bin/bash

CONF_DIR="/etc/vpnc/"
CONF_LST=$(find "${CONF_DIR}"*.conf)

#black='\E[30;47m'
#red='\E[31;47m'
#green='\E[32;47m'
#yellow='\E[33;47m'
#blue='\E[34;47m'
#magenta='\E[35;47m'
#cyan='\E[36;47m'
#white='\E[37;47m'

bold='\033[1m'

red='\033[0;31;40m'
green='\033[0;32;40m'
blue='\033[0;34;40m'

rst='\033[0m'

function crst()
{
        tput sgr0
}

function cecho()
{
        # $1 = msg
        # $2 = color
        # cecho "Jim is cool" $bold"

        echo -ne "${2}"
        echo -e  "${1}"
        echo -ne "${rst}"
        crst
}

#echo "Mod"

if [[ $# == 0 ]] ; then
        for CONF in ${CONF_LST[@]} ; do
                cecho "${CONF}" $green

                awk '{ print "   " $0 }' "${CONF}"
        done
else
        /usr/sbin/vpnc "$@"
fi

Now, when called without any arguments it will display the configuration files then exit (you could change this to not exit, and enter vpnc without arguments). However, when called with arguments, it simply passes those arguments to the real vpnc (located in /usr/sbin/vpnc on my install). You could extend this to hide the default.conf profile, enter the interactive mode after the profiles are displayed or when a command line switch was given, etc.

Posted Tuesday, October 6th, 2009 under shell scripting, tips and tricks.

Leave a Reply