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']}"
like this
reshared this
Stephane L Rolland-Brabant ⁂⧖⏚
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
headrift
in reply to Neil E. Hodges • • •😀🚲
in reply to Neil E. Hodges • • •Greg A. Woods
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 ofdeclare
to make it portable to Korn Shell, and get rid of the unnecessarylocal OPTARG
. I would get rid of the unnecessary local-reference trick withresult
-- 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.
Neil E. Hodges likes this.