[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Not to set PIPESTATUS unless necessary
From: |
Kerin Millar |
Subject: |
Re: Not to set PIPESTATUS unless necessary |
Date: |
Thu, 09 May 2024 05:00:51 +0100 |
User-agent: |
Cyrus-JMAP/3.11.0-alpha0-443-g0dc955c2a-fm-20240507.001-g0dc955c2 |
On Thu, 9 May 2024, at 4:34 AM, Peng Yu wrote:
> On Wed, May 8, 2024 at 9:23 PM Kerin Millar <kfm@plushkava.net> wrote:
>>
>> On Thu, 9 May 2024, at 2:57 AM, Peng Yu wrote:
>> > Hi,
>> >
>> > true | true
>> > false
>> > echo ${PIPESTATUS[@]}
>> >
>> > In the above commands, echo will print 1. But such information is also
>> > available in $?. Therefore, I think it is unnecessary to set
>> > PIPESTATUS after false. It is better to preserve its value until it is
>>
>> Bear in mind that "false" is also a foreground pipeline there.
>>
>> > really necessary. Otherwise, users will have to preserve PIPESTATUS
>> > after running true | true. This will result in more code to write.
>>
>> Can you show a realistic program in which it necessitates significantly more
>> code?
>
> Here is a reasonably real application.
>
> curl url | jq something || {
> pipestatus=(${PIPESTATUS[@]})
> # A few lines of code to consolidate the exit statuses of curl and jq
> into a single exit statute of the pipeline.
> }
>
> curl has many exit statuses. jq also has several exit statuses. It
> requires a few lines of code to preserve all the exit status info from
> both programs. Thus, not having to save PIPESTATUS to pipestatus will
> make the above scenario more compact and more readable.
>
> When similar pipelines are used a lot, it would save a lot of
> redundant code for not having to save PIPESTATUS.
OK. Given that bash can't presently do as you wish, encapsulating all of this
within a function might then help you.
curl2jq() {
local curl_args pipestatus
while (( $# )); do
curl_args+=("$1")
shift
if [[ $1 == -- ]]; then
shift
break
fi
done
if ! curl "${curl_args[@]}" | jq "$@"; then
pipestatus=("${PIPESTATUS[@]}")
: consolidation code that returns the desired value
fi
}
curl2jq "$url" -- 'jq program'
--
Kerin Millar
- Not to set PIPESTATUS unless necessary, Peng Yu, 2024/05/08
- Re: Not to set PIPESTATUS unless necessary, Kerin Millar, 2024/05/08
- Re: Not to set PIPESTATUS unless necessary, Koichi Murase, 2024/05/08
- Re: Not to set PIPESTATUS unless necessary, Koichi Murase, 2024/05/08
- Re: Not to set PIPESTATUS unless necessary, Chet Ramey, 2024/05/09
- Message not available
- Message not available
- Re: Not to set PIPESTATUS unless necessary, Peng Yu, 2024/05/18
- Re: Not to set PIPESTATUS unless necessary, Andreas Kähäri, 2024/05/18
- Re: Not to set PIPESTATUS unless necessary, Peng Yu, 2024/05/18
- Re: Not to set PIPESTATUS unless necessary, Andreas Kähäri, 2024/05/18
- Re: Not to set PIPESTATUS unless necessary, Peng Yu, 2024/05/18
- Re: Not to set PIPESTATUS unless necessary, Lawrence Velázquez, 2024/05/18
- Re: Not to set PIPESTATUS unless necessary, Peng Yu, 2024/05/18