My Profile Photo

Deviant Syndrome


coding, multimedia, gamedev, engineering


Never loose a Bash script again

Like most of us, I’ve a handful of bash scripts to automate my day to day tasks, related to work and personal activities. They all live somewhere along the lines of ~/myscripts and this entry is added to my PATH shell env.

The downside of this, is, of course, that you scripts directory become a bit of a mess, congested with all that obsolete and half-baked stuff, that also pollutes your autocomplete as well.

To fight this I came up with a simple convention for all the utility scripts I place In that magic folder. There should be two commented block after bash header in every script: first is a single string of space separated tags, then a short description of what this script does.

Example would be like:

#!/bin/bash
# blog util mp3
# Convert DAW rendered demo WAVs to MP3 and places them 
# into dedicated blog folder

Sticking to that convention, I was able to come up with a script that generates a summary of my utilities. It also filters by tag and flags the undocumented ones.

Lots of poor Bash gibberish
#!/bin/bash
# meta
# Lists my DIY Bash scripts
nodescr=""

files=($(ls ~/myscripts/. | grep -v '\.'))
tags=""
for f in ${files[*]}; do
    filename=$f
    descr=""
    skip="no"
    linecount=0
    while IFS= read -r line; do
        if [[ "$line" =~ ^#.*$ ]]; then
            let "linecount+=1"
            if ((linecount == 2)); then
                if ! [ -z "$1" ] || [ "$1" == "tags" ]; then
                    if [[ $line != *"$1"* ]]; then
                        skip="yes"
                    fi
                fi
                tags="$tags${line:1}"

            fi
            if ((linecount > 2)); then
                descr+="${line:1}"
            fi
        else
            break
        fi
    done <"$filename"
    if [ -z "$descr" ]; then
        nodescr="$nodescr $filename"
    else
        dd="$(printf '%-20s' $filename):"
        if [[ "$1" != "tags" ]]; then
            if [[ "$skip" == "no" ]]; then
                echo "$dd#$descr" | awk -f table.awk
                printf '\n'
            fi
        fi
    fi
done
echo $tags | xargs -n1 | sort -u | xargs
echo Missing description: $nodescr

Output looks really nice, though:

br34kp0int@lobsterblood~/» ./scriptz blog 
blogmp3             : Converts the files from Repulsive 
                      Recrods tech software demos to MP3 
                      and copies them to blog for 
                      educational purposes

blogthumbs          : Generates thumbnails for blog 
                      galleries

day2day             : Generates a blog entry with current 
                      data and some random title