Re: [Help-bash] Test exit status before printing the output without usin
From:
Geir Hauge
Subject:
Re: [Help-bash] Test exit status before printing the output without using temp file
Date:
Mon, 1 Feb 2016 09:17:11 +0100
User-agent:
Mutt/1.5.23 (2014-03-12)
On Sun, Jan 31, 2016 at 05:31:08PM -0500, Dave Rutherford wrote:
> On Sun, Jan 31, 2016 at 9:49 AM, Peng Yu <address@hidden> wrote:
> > But the trailing '\n' will be removed in `$()`. Is there a way to
> > preserve the trailing '\n'?
>
> That actually removes all the '\n''s (changing internal ones to spaces.)
> In the following, the reason for the '-n' to `echo' is that mapfile
> not only preserves endlines, but stores them in the variable,
> so just echoing each line would get you *too many*.
No, command substitution modifies the data in two ways.
1. It removes trailing newlines (only). Often you don't care much about
those anyway, but Peng Yu did in this case.
2. NUL bytes are ignored/stripped (making it useless for non-textual
data).
There's no changing newlines to spaces going on.
> set +m
> shopt -s lastpipe
> if cmd | mapfile; then # did you want to redirect stderr to /dev/null?
> for w in "address@hidden"; do
> echo -n "$w"
> done
> fi
Sure, mapfile is another way if you can rely on a new enough bash
version, but you're not testing cmd there.
set +m
shopt -s lastpipe
cmd | mapfile output
(( PIPESTATUS[0] == 0 )) && printf %s "address@hidden"
And you should use printf rather than echo in new scripts.
--
Geir Hauge
[Prev in Thread]
Current Thread
[Next in Thread]
Re: [Help-bash] Test exit status before printing the output without using temp file,
Geir Hauge<=