bug-bash
[Top][All Lists]
Advanced

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

Re: Echoing commands


From: Angelo Borsotti
Subject: Re: Echoing commands
Date: Thu, 13 Jun 2024 10:01:16 +0200

Dear all,

the solution to show commands with "set -x" has, however, a flow: it
does not show properly commands that contain redirections. E.g., let
tmp.sh be:

#!/bin/bash
shopt -s expand_aliases
alias @echo-on='set -x'
alias @echo-off='{ set +x; } 2>/dev/null'
PS4=
@echo-on
cat f1.txt f1.txt > f1.tmp
@echo-off

running it we obtain:

$ ./tmp.sh
cat f1.txt f1.txt

I.e. the command is not entirely displayed.

P.S. we can replace "set -x" by "set +v":
alias @echo-on='set -v'
alias @echo-off='{ set +v; } 2>/dev/null'

 This shows properly the command, but also shows @echo-off. I.e.
'{ set +v; } 2>/dev/null'  shows itself.

I have no idea how to suppress this.

-Angelo Borsotti

On Thu, 13 Jun 2024 at 08:28, Angelo Borsotti <angelo.borsotti@gmail.com>
wrote:

> Dear all,
>
> thank you very much for your quick replies. The solution:
>
>     alias @echo-on='set -x'
>     alias @echo-off='{ set +x; } 2>/dev/null'
>     PS4=
>
> Solves the problem, and relieves from writing "echo COMMAND" before each
> command that should be shown.
>
> -Angelo Borsotti
>
>
> On Wed, 12 Jun 2024 at 23:21, Koichi Murase <myoga.murase@gmail.com>
> wrote:
>
>> 2024年6月13日(木) 5:20 Angelo Borsotti <angelo.borsotti@gmail.com>:
>> > This is not the same as debugging, for which set -x is devoted.
>> > "set -x" makes the ensuing commands be printed, but prefixed
>> > with "+ ", which makes the result look ugly,
>>
>> PS4= (as Greg has replied)
>>
>> > not to mention that
>> > the following "set +x" is echoed too (there are hacks to suppress
>> > the "set +x" output, but they are just hacks).
>>
>> What are the hacks? What are the problems with them? You can have aliases:
>>
>> alias @echo-on='set -x'
>> alias @echo-off='{ set +x; } 2>/dev/null'
>>
>> > I would stress the importance of this: the purpose of scripts is
>> > to execute commands, informing the caller of what they execute,
>> > purged of all the builtins and other calculations the script does
>> > in order to come to the commands.
>>
>> I don't think the purpose of scripts is to execute only the external
>> commands in general. The tasks implemented by builtins and other
>> calculations are allowed to be the purpose of scripts.
>>
>> > Many solutions are posted in the internet, all of them are hacks with
>> > lots of edge cases that make them fail in presence of commands
>> > containing special characters, redirections, substitutions, etc.
>>
>> Have you tried `set -v'? `set -v' is not a hack, (though there doesn't
>> seem to be a way to suppress echoing set +v).
>>
>> The detailed design of how the logged commands should be filtered and
>> formatted depends on the purpose of each user. We already have `set
>> -x' and `set -v'. We can adjust the format by PS4 for `set -x'. It's
>> unrealistic to add a new option for every new preference of detailed
>> filtering and formatting. If it is really needed, you can implement
>> your filtering through the DEBUG trap, but it would finally be best to
>> explicitly write `echo COMMAND' before every COMMAND that you think is
>> important (unless you are lazy). It's stable and flexible.
>>
>> --
>> Koichi
>>
>


reply via email to

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