bug-bash
[Top][All Lists]
Advanced

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

Re: issues in bash found while creating command result display


From: Chet Ramey
Subject: Re: issues in bash found while creating command result display
Date: Sun, 14 Apr 2019 17:28:37 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

On 4/14/19 1:28 AM, Paul Wise wrote:
> Hi folks,
> 
> I wanted a way in bash to print the status of the last command I ran,
> including information about each command in a pipe and about signals.
> 
> I noticed that bash does not have the ability to display a command
> result, so I came up with the attached prompt configuration.
> 
> I am hoping that folks here are interested in fixing the things that I
> found that I had to workaround:
> 
>  * The command number variable $# is not available at the time that the
>    PROMPT_COMMAND is run, only when $PS1 is evaluated.

That's the number of positional parameters.

>  * There isn't an easy way to detect no command run yet, syntax errors,
>    Ctrl+C, Enter etc versus a command being run and exiting

Syntax errors set $? to 2. SIGINT at the prompt sets $? to 130. There's
no way to determine whether or not those exit statuses differ from an
exit status resulting from a command being run.

>  * Sometimes PIPESTATUS isn't reset even though $? has been reset

PIPESTATUS doesn't get set until a job completes. $? will be reset to
128+SIGNUM when a job stops, since that is what shells do and it seems
reasonable as a way to let the user know a job suspended. It doesn't seem
particularly useful to set PIPESTATUS to, e.g., {146, 146, 146} in this
case, though.

> 
> I am hoping that folks here have suggestions or fixes for the
> things that I found I wasn't able to do:
> 
>  * Determine if a command exited normally or due to a signal, even if
>    it exited normally with an exit code greater than 128.

There is no way to distinguish these cases.  There is a lot of convention
at work here, and convention is that well-behaved programs don't exit with
a status greater than 128.

>  * Have the PIPESTATUS be correct after a Ctrl+Z.

See above. You could add logic if $? == 128+SIGTSTP.

>  * Differentiate between Ctrl+C Ctrl+C and Ctrl+C Enter.

The tool you have is the exit status of the last command. From that
perspective, there's no difference.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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