Skip to main content


Modern shell #scripting is weird. :P #programming #bash #linux

#!/usr/bin/env bash

function parse_options () {
    declare -n result="$1"
    shift 1
    result['verbosity']='4'
    result['memory']='24G'
    result['volumes']=''
    result['device']='/dev/st0'
    result['write-rate']='250M'

    local opt
    local OPTARG
    while getopts 'vm:n:i:' opt
    do
        case $opt in
            v)
                result['verbosity']=6
                ;;
            m)
                result['memory']="$OPTARG"
                ;;
            n)
                result['volumes']="$OPTARG"
                ;;
            i)
                result['device']="$OPTARG"
                ;;
            r)
                result['write-rate']="$OPTARG"
                ;;
        esac
    done

    if [[ -z "${result['volumes']}" ]]; then
        echo "-i VOLUMES is required"
        exit 1
    fi
    return 0
}


declare -A CONFIG
parse_options CONFIG "$@" || exit 1

exec mbuffer \
    -v "${CONFIG['verbosity']}" \
    -m "${CONFIG['memory']}" \
    -n "${CONFIG['volumes']}" \
    -p 30 \
    -s 65536 \
    -A 'sh /usr/local/bin/tape_wait.sh' \
    -f \
    -R "${CONFIG['write-rate']}" \
    -i "${CONFIG['device']}"

reshared this

in reply to Neil E. Hodges

thanks, I am not sure I ever used declare -n , but that is pretty nice to know. I have to experiment with it

Do you have any advise/warning about it? Seems like a huge step to introduce something resembling references into #bash

#bash
This entry was edited (4 weeks ago)
in reply to Neil E. Hodges

Yeah, but I think that script is asking a lot from bash with buffers. New languages are around (ecmascript / C# and others) that are more ready for modern hardware and newer goals. Gotta remember that a lot of things have changed since bash
in reply to Neil E. Hodges

what's weird is everything that could go wrong without all that. I'm not sure it's particularly modern though
in reply to Neil E. Hodges

Buffers? I see no buffers. It's a relatively trivial little script to translate one command-line API into another. I would use typeset instead of declare to make it portable to Korn Shell, and get rid of the unnecessary local OPTARG. I would get rid of the unnecessary local-reference trick with result -- it is not a sin to modify a well named global variable in a shell script. It could easily be translated into entirely POSIX-compatible syntax by using multiple global variables.

As for using another language, well that would be both silly, and far more complex. The shell is always already there and quite capable.