From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14145 invoked from network); 4 Feb 2007 16:46:55 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.7 (2006-10-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,FORGED_RCVD_HELO autolearn=ham version=3.1.7 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 4 Feb 2007 16:46:55 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 56298 invoked from network); 4 Feb 2007 16:46:45 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 4 Feb 2007 16:46:45 -0000 Received: (qmail 6301 invoked by alias); 4 Feb 2007 16:46:36 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 11180 Received: (qmail 6291 invoked from network); 4 Feb 2007 16:46:36 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 4 Feb 2007 16:46:36 -0000 Received: (qmail 55044 invoked from network); 4 Feb 2007 16:46:36 -0000 Received: from unknown (HELO n064.sc1.he.tucows.com) (64.97.136.147) by a.mx.sunsite.dk with SMTP; 4 Feb 2007 16:46:30 -0000 Received: from sc.homeunix.net (82.26.164.249) by n064.sc1.he.tucows.com (7.2.069.1) id 45B64150000E47FA; Sun, 4 Feb 2007 16:46:19 +0000 Received: from chazelas by sc.homeunix.net with local (Exim 3.36 #1 (Debian)) id 1HDkVN-0001uF-00; Sun, 04 Feb 2007 16:46:17 +0000 Date: Sun, 4 Feb 2007 16:46:17 +0000 From: Stephane Chazelas To: Bart Schaefer Cc: zsh-users Subject: Re: keep empty lines at the end of files? Message-ID: <20070204164616.GA6888@sc.homeunix.net> Mail-Followup-To: Bart Schaefer , zsh-users References: <45C2FE72.7040303@ulpmm.u-strasbg.fr> <070203125237.ZM7933@torch.brasslantern.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <070203125237.ZM7933@torch.brasslantern.com> User-Agent: Mutt/1.5.6i Sender: Stephane Chazelas On Sat, Feb 03, 2007 at 12:52:37PM -0800, Bart Schaefer wrote: > On Feb 2, 10:03am, Marc Chantreux wrote: > } > } I have some files with very important empty lines at the end of them. > } > } Is the a clean and short way to *not* trim the file ? > > zmodload -i zsh/mapfile > content=${mapfile[ldap_user_passwd]} > > I'm not sure, historically speaking, why $(<...) is trimming trailing > blank lines. It makes sense to trim *one* newline character, because for most commands, there's one newline added by the command to the data it is actually displaying. In base=$(basename -- "$file") You don't want in $base the trailing newline character added by basename. However, you want the ones before because those ones are part of the filename. However all the Bourne and csh like shells have always removed *every* trailing newline character. I personally consider it as a bug. A work around for POSIX shells is: cmdoutput() { REPLY=$(eval "$1"; echo .) REPLY=${REPLY%?.} } For Bourne shells: cmdoutput() { REPLY=` eval "$1" | sed "s/'/'\\\\\\\\''/g;\\\$s/\\\$/'/" ` eval "REPLY='$REPLY" } In rc, it's a bit more consistent, but not much more usable. First in rc, all variables are lists, so var = `{ls} (first, quite sensibly, filename generation is not performed as in zsh) Then the output of ls is split and stored into the $var list. The splitting algoithm is a lot simpler and more consistent than in Bourne like shells, A sequence of separators is a separator and leading and trailing separators are discarded whatever they are (like in sh for the whitespace separators). So, the trailing characters are removed as part of the word splitting in rc. That means var = `{basename -- $file} is no more correct than is Bourne like shells (even less correct as the command output is split). However, in rc, one can explicitely specify the separator: var = ``(separators){cmd} So var = ``(){cmd} captures exactly the output of cmd (as long as it doesn't contain null bytes as all rc variables are environment variables). Now, if you want to remove the trailing newline character, it ends up being as difficult as having a correct behavior in sh. var = ``(){cmd | awk 'NR>1{print ""}{printf "%s", $0}'} GNU only: var = ``(){cmd | head -c-1} In es (based on rc): var = <={~~ ``(){cmd} *\n} So the question remains. Why hasn't any single shell come up with the correct solution? -- Stéphane