[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Set verbose Flag Inside if-block
From: |
Dennis Williamson |
Subject: |
Re: Set verbose Flag Inside if-block |
Date: |
Sun, 16 Apr 2023 18:19:57 -0500 |
On Sun, Apr 16, 2023 at 1:24 PM Sysadmin Lists <sysadmin.lists@mailfence.com>
wrote:
> [ resending with proper subject line]
>
> > Message: 2
> > Date: Sun, 16 Apr 2023 10:42:29 +0200
> > From: Andreas Kusalananda Kähäri <andreas.kahari@abc.se>
> > To: Sysadmin Lists <sysadmin.lists@mailfence.com>
> > Cc: help-bash@gnu.org
> > Subject: Re: Set verbose Flag Inside if-block
> > Message-ID: <ZDu09W09cknzRrnZ@harpo.local>
> > Content-Type: text/plain; charset=utf-8
> >
> > > $ if true; then set -ve; echo $-; echo foo bar baz; set -ve; fi
> > > ehimvBHs
> > > foo bar baz
> >
> > Using set -v causes the shell to print the input lines *as they are
> > read*. The line that contains the "set -v" command has already been
> > read, so it won't be outputted.
> >
> > >
> > > $ set -ve; if true; then echo $-; echo foo bar baz; set -ve; fi
> > > set -ve; if true; then echo $-; echo foo bar baz; set -ve; fi
> > > ehimvBHs
> > > foo bar baz
> >
> > No diference here, the line has already been read.
> >
> > --
> > Andreas (Kusalananda) Kähäri
> > SciLifeLab, NBIS, ICM
> > Uppsala University, Sweden
> >
> > .
>
> That's not what's happening. Breaking up that command line into newlines
> shows
> the bug. The first version should echo everything inside the if-statement
> after the
> 'set -ve' but doesn't. It's treating the entire if-block as a single
> command.
>
> $ cat tmpfile.sh
> if true
> then
> set -ve
> echo $-
> echo "foo bar baz"
> set +ve
> fi
>
> $ ./tmpfile.sh
> ehvB
> foo bar baz
>
> $ cat tmpfile.sh
> set -ve
> if true
> then
> echo $-
> echo "foo bar baz"
> set +ve
> fi
>
> $ ./tmpfile.sh
> if true
> then
> echo $-
> echo "foo bar baz"
> set +ve
> fi
> ehvB
> foo bar baz
>
>
> --
> Sent with https://mailfence.com
> Secure and private email
>
> Do your test twice: once to set verbose mode and the second time, after a
newline, to do things you need.
mytest=true
if "$mytest"
then
set -v
fi
# verbose is only just now on, assuming the test is true
if "$mytest"
then
do_stuff_verbosely
fi
set +v
It might show a few more lines than you wanted.
Otherwise, use set -x and perhaps unset PS4. But this will show everything
expanded.
--
Visit serverfault.com to get your system administration questions answered.
Re: Set verbose Flag Inside if-block, Sysadmin Lists, 2023/04/17