shell: support ':' separator for multiple args

It becomes a pain to remove all colons just to add a single argument.
This commit is contained in:
Peter Wu
2014-04-05 18:27:16 +02:00
parent c3a263ff97
commit 955b9feb9e

27
shell
View File

@@ -4,7 +4,7 @@
# Author: Peter Wu <lekensteyn@gmail.com>
hidw() {
local hiddev arg bytes fillchar length oIFS
local hiddev arg bytes fillchar length oIFS actualArgs
hiddev=$1; shift
bytes=()
@@ -24,11 +24,10 @@ In this case, the message is padded with '0xff'.
The data can also be interleaved with chars by starting an argument
with a underscore (_). For example, "_foo" is equivalent to "66 6f 6f".
A colon can also be used as separator, the previous hidw example is
equivalent to:
If only a single argument is given, the commands may be separated by
colons. The previous hidw example is equivalent to:
hidw /dev/hidraw0 10:ff:81:ff..
hidw /dev/hidraw0 10:ff 81:ff..
This function does not show replies, use the 'read-dev-usbmon' program
instead.
@@ -41,11 +40,19 @@ HELP
return 1
fi
# Support ":" as separator if there is only a single argument. This makes
# it easier to work with tshark output, and does not break "_:" arguments.
if [ $# -eq 1 ]; then
oIFS="$IFS"; IFS=:; set -- $*; IFS="$oIFS"
fi
# Support ":" as separator, but ignore string arguments (_...).
actualArgs=()
for arg; do
if [[ $arg != _* ]] && [[ $arg == *:* ]]; then
# Strip leading+trailing such that "11:02: 83 01" is still valid.
arg="${arg##:}"
arg="${arg%%:}"
oIFS="$IFS"; IFS=:; actualArgs+=( $arg ); IFS="$oIFS"
else
actualArgs+=( "$arg" )
fi
done
set -- "${actualArgs[@]}"
for arg; do
# clear the fill char, makes only sense as last argument