help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Test exit status before printing the output without usin


From: Peng Yu
Subject: Re: [Help-bash] Test exit status before printing the output without using temp file
Date: Sun, 31 Jan 2016 08:49:18 -0600

On Sun, Jan 31, 2016 at 2:03 AM, Geir Hauge <address@hidden> wrote:
> On Sat, Jan 30, 2016 at 08:49:00PM -0600, Peng Yu wrote:
>> Hi, Suppose I want to only print the output of a program if the exit
>> status is 0.
>>
>> So far, I can only find a solution using a temp file. That is, pipe
>> the output of the program to a temp file and then test the exist
>> status of the program. If the exit status is 0, then I cat the content
>> in the temp file.
>>
>> Does anybody know if it is possible to have a solution without a temp file?
>
> You can store it in a variable:
>
>     if output=$(cmd); then
>         printf 'Success! The output was:\n%s\n' "$output"
>     fi

But the trailing '\n' will be removed in `$()`. Is there a way to
preserve the trailing '\n'?

printf "%s\n" "$(echo x)"
printf "%s\n" "$(echo -n x)"

-- 
Regards,
Peng



reply via email to

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