[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: colored verbose
From: |
lacsaP Patatetom |
Subject: |
Re: colored verbose |
Date: |
Wed, 8 Nov 2023 15:22:24 +0100 |
Le mer. 8 nov. 2023 à 15:14, Greg Wooledge <greg@wooledge.org> a écrit :
> On Wed, Nov 08, 2023 at 03:01:11PM +0100, lacsaP Patatetom wrote:
> > bash's verbose mode makes it possible to "exploit" comments and
> "preserve"
> > code (pipe characters don't disappear for example)
>
> It also means that any compound commands are written by verbose mode
> when the parser reads them, NOT when they're executed. This affects
> functions, loops, if/then, case, etc.
>
> Consider something as simple as this:
>
> #!/bin/bash -v
> f() {
> echo "f has been called"
> }
>
> for i in 1 2 3; do
> sleep 1
> f
> done
>
> What happens when we run it:
>
> unicorn:~$ ./bar
> #!/bin/bash -v
> f() {
> echo "f has been called"
> }
>
> for i in 1 2 3; do
> sleep 1
> f
> done
> f has been called
> f has been called
> f has been called
> unicorn:~$
>
> The entire script is a shebang, a compound command (function definition),
> a blank line, and another compound command (for loop). With verbose
> mode, we see the entire script written out, because the parser has to
> read it all before it can begin executing the loop. Then we see the
> three lines of output from the function's three calls. We don't see the
> sleep commands or the function calls or even the echo being executed.
>
> With -x, we get this:
>
> unicorn:~$ ./bar
> + for i in 1 2 3
> + sleep 1
> + f
> + echo 'f has been called'
> f has been called
> + for i in 1 2 3
> + sleep 1
> + f
> + echo 'f has been called'
> f has been called
> + for i in 1 2 3
> + sleep 1
> + f
> + echo 'f has been called'
> f has been called
> unicorn:~$
>
> I'd argue that most people find that more useful than the -v version.
>
> The ONLY time -v is ever useful is in the most trivial of scripts, where
> there are no compound commands at all. If the script is just a linear
> sequence of commands, which are executed straight down the line, then -v
> and -x are basically equivalent, and some people might prefer -v's
> output, as it shows the redirections. But most scripts are not that
> simple. In most real world applications, there'll be *some* sort of
> flow control (a loop or a conditional), and then -v becomes far less
> useful.
>
>
xtrace mode is really designed for debugging (which is not my intention).
thank you for all your comments and clarifications, and for the
`BASH_XTRACEFD` track.
regards, lacsaP.
Re: colored verbose, alex xmb sw ratchev, 2023/11/08