Alexander Gromnitsky's Blog

My configs are generally robust

Latest update:

So I had this

# There is a silly bug here.

# Filter-out dup lines from stdin & print the result to stdout
hist.read() {
    local -A cache
    local -a arr
    local n=0

    # a subshell, for we're altering IFS
    {
        IFS=$'\n'
        while read -r line; do
            [ -z "$line" ] && continue
            if [ "${cache[$line]}" = "" ] ; then
                cache[$line]=1
                arr[$((n++))]=$line
            fi
        done

        echo "${arr[*]}"
    }
}

# bash in macOS is ancient & doesn't support associative arrays
[ ${BASH_VERSINFO[0]} -ge 4 ] && {
    hist.read < $HISTFILE > $HISTFILE.tmp
    mv $HISTFILE.tmp $HISTFILE
}

in my ~/.bashrc for a quite some time.

Then, one day I notice a subtle difference in the behaviour of read(), between my machine & all the others.

facepalm.jpg

Kids! Watch your braces & parentheses!


Tags: ойті
Authors: ag