help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Set verbose Flag Inside if-block


From: Sysadmin Lists
Subject: Re: Set verbose Flag Inside if-block
Date: Mon, 17 Apr 2023 20:08:41 +0200 (CEST)

On Apr 16, 2023 at 4:19 PM, Dennis Williamson <dennistwilliamson@gmail.com> 
wrote:

On Sun, Apr 16, 2023 at 1:24 PM Sysadmin Lists <sysadmin.lists@mailfence.com> 
wrote:

[ resending with proper subject line]

$ 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

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.

Well, until someone can convince Chet to re-write Bash to make this flag
useful, I've come up with a ridiculous workaround:

$ cat tmpfile.sh
if true
then
        printf "echo foo bar baz\n" | sh -v
fi

$ ./tmpfile.sh 
echo foo bar baz
foo bar baz

Being able to selectively echo commands inside if-blocks makes sense. The -v
flag is a bit of a fraud the way it's currently implemented.

PS. I don't know why this doesn't work:
(set -ve; echo $-; echo foo bar baz; set+ve)

That should create a sub-shell with the v flag set on the entire command line.

 
-- Sent with https://mailfence.com  Secure and private email


reply via email to

[Prev in Thread] Current Thread [Next in Thread]