From 9f62fd9605eade23b62b07a235d1f02156f7a5c6 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Mon, 19 Nov 2012 00:10:10 -0800 Subject: [PATCH] xzless: Make "less -V" parsing more robust MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In v4.999.9beta~30 (xzless: Support compressed standard input, 2009-08-09), xzless learned to parse ‘less -V’ output to figure out whether less is new enough to handle $LESSOPEN settings starting with “|-”. That worked well for a while, but the version string from ‘less’ versions 448 (June, 2012) is misparsed, producing a warning: $ xzless /tmp/test.xz; echo $? /usr/bin/xzless: line 49: test: 456 (GNU regular expressions): \ integer expression expected 0 More precisely, modern ‘less’ lists the regexp implementation along with its version number, and xzless passes the entire version number with attached parenthetical phrase as a number to "test $a -gt $b", producing the above confusing message. $ less-444 -V | head -1 less 444 $ less -V | head -1 less 456 (no regular expressions) So relax the pattern matched --- instead of expecting "less ", look for a line of the form "less [ (extra parenthetical)]". While at it, improve the behavior when no matching line is found --- instead of producing a cryptic message, we can fall back on a LESSPIPE setting that is supported by all versions of ‘less’. The implementation uses "awk" for simplicity. Hopefully that’s portable enough. Reported-by: Jörg-Volker Peetz Signed-off-by: Jonathan Nieder --- src/scripts/xzless.in | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/scripts/xzless.in b/src/scripts/xzless.in index a3da697c..9bc9706b 100644 --- a/src/scripts/xzless.in +++ b/src/scripts/xzless.in @@ -46,7 +46,8 @@ if test "${LESSMETACHARS+set}" != set; then LESSMETACHARS="$space$tab$nl'"';*?"()<>[|&^`#\$%=~' fi -if test "$(less -V | { read ver && echo ${ver#less }; })" -ge 429; then +less_ver=$(less -V | awk '/^less ([0-9]+)( \(.*\))?$/ { print $2; exit }') +if test -n "$less_ver" && test "$less_ver" -ge 429; then # less 429 or later: LESSOPEN pipe will be used on # standard input if $LESSOPEN begins with |-. LESSOPEN="|-$xz -cdfq -- %s"