Associative Arrays in BASH (how to do key:value pairing without Python)

Unfortunately this only works in BASH 4.x, which is a damn shame. Older RHEL installations don't have the latest, greatest version of BASH installed. If you're an admin of RHEL4/5 machines, this might be worth trying to update, otherwise just write your automation in Python. :)

[sourcecode language="bash …

Continue reading »

Trimming strings in BASH

I recently needed to trim the last N characters from a string in shell. I wrote this little function to handle the task.
This requires a newer distro that has updated coreutils that includes the 'fold' command (Feb 2010).

#!/bin/bash  
function trimChars() {  
    if [ $2 ]; then  
        TRIM_LAST=$2  
    elif …

Continue reading »

bash testing with if

I always seem to forget these and are oh so useful.

Expressions used with if

Continue reading »

horrible, ugly, bad BASH string operations...

I do and do not recommend learning this.

Advanced BASH Programming - String Operations

They certainly can come in handy though if you don't want to do some basic grep/awk work.

little example:

$ foo="my words" $ echo ${foo#my} words $ echo ${foo:1} y words $ echo ${foo:2} words $ echo …

Continue reading »

useful sed one liner in bash

I was about to upload some documents to my company's wiki that someone else wrote. Upload failed! For some odd reason they chose to use a '+' instead of a space or under bar in the filename. Here is how I fixed it.

for i in `ls -1`; do 
    old=${i …

Continue reading »