[DTrace-devel] [PATCH 19/20] test: allow test options to be set to "-e"

Nick Alcock nick.alcock at oracle.com
Wed May 11 21:13:01 UTC 2022


The curse of echo caught us again.  Using printf keeps the -e, but even
then we have to doble-quote the argument because one layer of quotes
will be eaten by the eval, and printf with more args than format string
args reuses the format string over and over again: if it's %s, this
effectively squashes the args together with intervening spaces removed.

Signed-off-by: Nick Alcock <nick.alcock at oracle.com>
---
 runtest.sh | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/runtest.sh b/runtest.sh
index 387907a8df4a..c9a23ba0ad08 100755
--- a/runtest.sh
+++ b/runtest.sh
@@ -177,14 +177,17 @@ extract_options()
 
             _pid='$_pid'
 
-            # This trick is because printf squashes out spaces, echo -e -e foo
-            # prints 'foo', and echo -e -- "foo" prints "-- foo".
+            # This trick is because printf with a straight eval ends up called
+            # with more args than format string items and squashes out spaces,
+            # echo -e -e foo prints 'foo', and bash's builtin echo -e -- "foo"
+            # prints "-- foo".  coreutils echo does the right thing, but is more
+            # expensive than the builtin printf, so use it only when needed.
 
             if [[ -n $val ]]; then
                 if [[ -n $expand ]]; then
-                    eval echo ''"$val"
+                    eval printf "%s" \""$val"\"
                 else
-                    echo ''"$val"
+                    printf "%s" ''"$val"
                 fi
             fi
             return
-- 
2.36.1.263.g194b774378.dirty




More information about the DTrace-devel mailing list