Script snippets

I recently needed to trim the last N characters from a string in shell. I wrote this little function to handle it.
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 ! [ $2 ]; then  
        TRIM_LAST=4  
    fi  
    if [ $1 ]; then  
        STRING="$1"  
    elif ! [ $1 ]; then  
        STRING="FooBar.xls"  
    fi  
    NEW_STRING=""  
    STRING_ARRAY=(`echo ${STRING} | fold -w1`)  
    NEW_STRLEN=$[ ${#STRING_ARRAY[*]} - ${TRIM_LAST} ]  
    for((i=0;i<${NEW_STRLEN};i++)); do  
        NEW_STRING="${NEW_STRING}${STRING_ARRAY[$i]}"  
    done  
    printf "String in:\t %s\nString out:\t%s\n" ${STRING}
    ${NEW_STRING}  
}

trimChars b0mbsgob3wn.me 3


My good friend Chris gave me this problem to test my python skills. given I had to do some googling around docs.python.org, it was all there.
Q: Write a single line that takes a CSV line [string] of directory paths and evaluates to True if they all exist and False if any of them do not exist.

>>> d1  
'/tmp, /Users'  
>>> d2  
'/tmp/tmp/tmp, /root'  
>>> all(map(lambda ii: os.path.exists(ii.strip()), d1.split(',')))  
True  
>>> all(map(lambda ii: os.path.exists(ii.strip()), d2.split(',')))  
False

And here are the two Chris came up with:

reduce(lambda v,d: False if not v else os.path.isdir(d),
    csv.split(','), True)  
all([os.path.isdir(d) for d in csv.split(',')])

I used this to add a nagios check to a few dozen servers. all config file names contained 'dev' and end in '.cfg'. the new command text was documented in the new-nagios-check-cmd file

for i in `ls *dev*.cfg`; do 
    host=$(echo $i | cut -d. -f1)
    sed -e 's/HOSTNAME/'$host'/g' new-nagios-check-cmd >> ${i}
done

when trying to determine the size of a directory using 'du' over NFS can take a looooooong time. Try this instead.

foo=$(ls -ltr | awk '{sum += $5} END {print sum}')  
echo $foo/1024/1024/1024 | bc -l  
7.83353263884782791137

This gives me the aggregate byte size of files in the directory which the command is run. echo the result divided by 1024(3x) and you get the GB size.

Display your current $PATH in a more readable format

echo $PATH | awk 'BEGIN{FS=":"} {for (i=1; i&lt;NF; i++) print $i}'

calculate total 'real' time from /usr/bin/time -p output from multiple files.

grep real /path/\*files | awk '{ total += $2 } END { print total }'

when trying to determine the size of a directory using 'du' over NFS can take a looooooong time. Try this instead.

foo=$(ls -ltr | awk '{sum += $5} END {print sum}')  
echo $foo/1024/1024/1024 | bc -l  
7.83353263884782791137

This gives the aggregate byte count of files in the directory which the command is run. echo the result divided by 1024(3x) and you get sum in GigaBytes.