From 955b9feb9e7881f07896e19491ae1b5cc728c4d8 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sat, 5 Apr 2014 18:27:16 +0200 Subject: shell: support ':' separator for multiple args It becomes a pain to remove all colons just to add a single argument. --- shell | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'shell') diff --git a/shell b/shell index 5c1e28a..7c8f8fe 100755 --- a/shell +++ b/shell @@ -4,7 +4,7 @@ # Author: Peter Wu 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 -- cgit v1.2.1