Skip to contentSkip to conversation

standalone: Log things we are running via with_logging

PATCH Superseded Ian Campbell 2 participants 4 messages Dec 3, 2015 – Dec 4, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 standalone: Log things we are running via with_logging

Turning on set -x is too verbose, so just echo the command ourselves.

---
 standalone | 1 +
 1 file changed, 1 insertion(+)
standalone +1 -0
index c804b74..6897400 100755
@@ -195,6 +195,7 @@ with_logging() {
     if command -v savelog >/dev/null ; then
         savelog -c 300 -n "$log" >/dev/null
     fi
+    echo "+ $@" >&2
     "$@" 2>&1 | tee "$log"
     rc=${PIPESTATUS[0]}
     if [ $rc -ne 0 ] ; then
Signature · 1 line
2.6.1
#1 lore raw

Ian Campbell writes ("[PATCH OSSTEST] standalone: Log things we are running via with_logging"):

Quoted 1 line

Turning on set -x is too verbose, so just echo the command ourselves.

It's not clear to me from the context whether $@ might contain things which would require quoting. set -x would quote them.

You can perhaps do something like this
bash -xc ': standalone-runs "$@"' x "$@" >&2
? (Sadly -n suppresses output from -x too.)

Ian.

#2 lore raw
Quoted 6 lines · Ian Jackson

Ian Campbell writes ("[PATCH OSSTEST] standalone: Log things we are running via with_logging"):

Quoted 1 line

Turning on set -x is too verbose, so just echo the command ourselves.

It's not clear to me from the context whether $@ might contain things which would require quoting.

It's stuff which is able to be run on the next line with:

    "$@" 2>&1 | tee "$log"

But I'm not sure that answers your question.

Are you worried about things being incorrectly expanded in the evaluation of the echo, or just about being able to cut and paste a command directly from this output into a shell? (or something else).

FWIW $@ is unlikely any $vars at this point or to contain spaces, quotes or other special characters, although it's not totally impossible (it would require job names, or idents, or ts-script arguments etc with such in them).

Quoted 5 lines

  set -x would quote them.

You can perhaps do something like this
   bash -xc ': standalone-runs "$@"' x "$@" >&2
?  (Sadly -n suppresses output from -x too.)

Sadly it also spews the contents of my .bashrc too. Weirdly (?) adding -- norc _and_ --noprofile to the mix doesn't change that, which seems rather contrary to the manpage.

Invoking as sh instead produces unquoted results:

$ sh -xc ': standalone-runs "$@"' x "foo" "bar bar"
+ : standalone-runs foo bar bar

(this must be difference between bash and POSIX?)

This seems to work:

$ bash -c 'set -x ; : standalone-runs: "$@"' x "foo" "bar bar"
+ : standalone-runs: foo 'bar bar'

And this: (set -x ; : "$@") also looks to DTRT:

$ foo() { (set -x ; : "$@") }
$ foo "foo" "bar bar"
+ : foo 'bar bar'

coreutils' printf(1) also has %q, but it's a bit ugly to use and is backslashy rather than quoting :

$ printf "%q " "foo bar" "baz" ; echo
foo\ bar baz 

This is a tricky thing to search for (you mostly just get stuff about regular variable quoting).

Closest thing I found to a solution was:
http://stackoverflow.com/questions/12985178/bash-quoted-array-expansion

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

Ian Campbell writes ("Re: [PATCH OSSTEST] standalone: Log things we are running via with_logging"):

Quoted 3 lines · Ian Jackson
Quoted 2 lines

It's not clear to me from the context whether $@ might contain things which would require quoting.

...

Quoted 3 lines

Are you worried about things being incorrectly expanded in the evaluation of the echo, or just about being able to cut and paste a command directly from this output into a shell? (or something else).

The latter, but also ambiguity in the debug output which might confuse a human reader.

Quoted 4 lines

FWIW $@ is unlikely any $vars at this point or to contain spaces, quotes or other special characters, although it's not totally impossible (it would require job names, or idents, or ts-script arguments etc with such in them).

I would prefer to do this properly if it's in a generic logging subfunction.

Quoted 7 lines
Quoted 3 lines

You can perhaps do something like this
   bash -xc ': standalone-runs "$@"' x "$@" >&2
?  (Sadly -n suppresses output from -x too.)

Sadly it also spews the contents of my .bashrc too. Weirdly (?) adding -- norc _and_ --noprofile to the mix doesn't change that, which seems rather contrary to the manpage.

Urgh.

Quoted 10 lines

This seems to work:

$ bash -c 'set -x ; : standalone-runs: "$@"' x "foo" "bar bar"
+ : standalone-runs: foo 'bar bar'

And this: (set -x ; : "$@") also looks to DTRT:

$ foo() { (set -x ; : "$@") }
$ foo "foo" "bar bar"
+ : foo 'bar bar'

Either of these would suit me. The latter seems tidier.

Quoted 4 lines

coreutils' printf(1) also has %q, but it's a bit ugly to use and is backslashy rather than quoting :

$ printf "%q " "foo bar" "baz" ; echo
foo\ bar baz 

The output from -x is nicer, indeed.

Ian.