threads / patch / 680

patchFix use of wc in t0000-basic

Subject: [PATCH] Fix use of wc in t0000-basic

## tl;dr

10 messages between May 21, 2005 and May 21, 2005. Diffs are folded; open one to read it.

replies: 9people: 4as markdown or json

Daniel Barkalow· May 21, 2005, 00:49 UTC · lore

The version of wc I have (GNU textutils-2.1) puts spaces at the beginning of lines. This patch should work for any version of wc.

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
Acked-by: Junio C Hamano <junkio@cox.net>
Show changes to diff +1 −1
Index: t/t0000-basic.sh
===================================================================
--- 58741c69570705801db4b785681790d636475695/t/t0000-basic.sh  (mode:100755 sha1:9a557129d98b499bcd601903d6646de29ba4bfc5)
+++ uncommitted/t/t0000-basic.sh  (mode:100755)
@@ -32,7 +32,7 @@
 find .git/objects -type d -print >full-of-directories
 test_expect_success \
     '.git/objects should have 256 subdirectories.' \
-    'test "$(wc -l full-of-directories | sed -e "s/ .*//")" = 257'
+    'test $(cat full-of-directories | wc -l) = 257'
 
 ################################################################
 # Basics of the basics
Sean· May 21, 2005, 01:08 UTC · re: Daniel Barkalow · lore

Re: [PATCH] Fix use of wc in t0000-basic

On Fri, May 20, 2005 8:49 pm, Daniel Barkalow said:
Show 20 quoted lines
> The version of wc I have (GNU textutils-2.1) puts spaces at the beginning
> of lines. This patch should work for any version of wc.
>
> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
> Acked-by: Junio C Hamano <junkio@cox.net>
> Index: t/t0000-basic.sh
> ===================================================================
> --- 58741c69570705801db4b785681790d636475695/t/t0000-basic.sh
> (mode:100755 sha1:9a557129d98b499bcd601903d6646de29ba4bfc5)
> +++ uncommitted/t/t0000-basic.sh  (mode:100755)
> @@ -32,7 +32,7 @@
>  find .git/objects -type d -print >full-of-directories
>  test_expect_success \
>      '.git/objects should have 256 subdirectories.' \
> -    'test "$(wc -l full-of-directories | sed -e "s/ .*//")" = 257'
> +    'test $(cat full-of-directories | wc -l) = 257'
>
>  ################################################################
>  # Basics of the basics
>

You can't do "wc -l filename" because some versionso of "wc" then include the filename in their output and confuse things. That was the reason to use "cat" in the first place. If you're going to use sed, just do away with wc altogether:

sed -ne '$=' full-of-directories
And that should work everywhere to get a line count.

Cheers, Sean

Daniel Barkalow· May 21, 2005, 01:10 UTC · re: Sean · lore

Re: [PATCH] Fix use of wc in t0000-basic

On Fri, 20 May 2005, Sean wrote:
> You can't do "wc -l filename" because some versionso of "wc" then include
> the filename in their output and confuse things.   That was the reason to
> use "cat" in the first place.
You're reading my patch backwards.
	-Daniel
*This .sig left intentionally blank*
Sean· May 21, 2005, 01:13 UTC · re: Daniel Barkalow · lore

Re: [PATCH] Fix use of wc in t0000-basic

On Fri, May 20, 2005 9:10 pm, Daniel Barkalow said:
Show 10 quoted lines
> On Fri, 20 May 2005, Sean wrote:
>
>> You can't do "wc -l filename" because some versionso of "wc" then
>> include
>> the filename in their output and confuse things.   That was the reason
>> to
>> use "cat" in the first place.
>
> You're reading my patch backwards.
>

Yes, i was. But presumably someone was stripping the whitespace from wc for a reason? Either way the sed-only solution seems a little cleaner.

Sean
Daniel Barkalow· May 21, 2005, 01:16 UTC · re: Sean · lore

Re: [PATCH] Fix use of wc in t0000-basic

On Fri, 20 May 2005, Sean wrote:
> Yes, i was.   But presumably someone was stripping the whitespace from wc
> for a reason?   Either way the sed-only solution seems a little cleaner.

Junio was stripping the filename (not whitespace) from wc, not knowing that it could be suppressed by using stdin. This didn't work with versions of wc that put whitespace at the beginning. I think the sed-only solution is far more obscure and no cleaner than cat and wc.

	-Daniel
*This .sig left intentionally blank*
Junio C Hamano· May 21, 2005, 01:43 UTC · re: Daniel Barkalow · lore

Re: [PATCH] Fix use of wc in t0000-basic

>>>>> "DB" == Daniel Barkalow <barkalow@iabervon.org> writes:

DB> Junio was stripping the filename (not whitespace) from wc, not knowing DB> that it could be suppressed by using stdin.

Actually the reason I did so initially was because I recalled seeing a wc that said "-" instead of omitting the filename. I do not have access to those obscure Unixen so I cannot these things easily anymore, though.

DB> of wc that put whitespace at the beginning. I think the DB> sed-only solution is far more obscure and no cleaner than DB> cat and wc.

This I tend to agree, but that is probably one of the most portable.

Herbert Xu· May 21, 2005, 10:37 UTC · re: Daniel Barkalow · lore

Re: [PATCH] Fix use of wc in t0000-basic

Daniel Barkalow <barkalow@iabervon.org> wrote:
>
> -    'test "$(wc -l full-of-directories | sed -e "s/ .*//")" = 257'
> +    'test $(cat full-of-directories | wc -l) = 257'
You don't need the cat:
wc -l < full-of-directories
will do the same thing.

It's also better to use -eq instead of = since you are comparing numbers, not strings. If you do that you can keep the double quotes since the spaces will be removed automatically.

Cheers,
-- 
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Junio C Hamano· May 21, 2005, 10:53 UTC · re: Herbert Xu · lore

Re: [PATCH] Fix use of wc in t0000-basic

>>>>> "HX" == Herbert Xu <herbert@gondor.apana.org.au> writes:

HX> It's also better to use -eq instead of = since you are comparing HX> numbers, not strings. If you do that you can keep the double HX> quotes since the spaces will be removed automatically.

I remember being burned by busybox "test" which did not ignore spaces. I do not know if the latest one is fixed, though.

Herbert Xu· May 21, 2005, 11:01 UTC · re: Junio C Hamano · lore

Re: [PATCH] Fix use of wc in t0000-basic

On Sat, May 21, 2005 at 03:53:30AM -0700, Junio C Hamano wrote:
Show 8 quoted lines
> >>>>> "HX" == Herbert Xu <herbert@gondor.apana.org.au> writes:
> 
> HX> It's also better to use -eq instead of = since you are comparing
> HX> numbers, not strings.  If you do that you can keep the double
> HX> quotes since the spaces will be removed automatically.
> 
> I remember being burned by busybox "test" which did not ignore
> spaces.  I do not know if the latest one is fixed, though.

Are you sure that it didn't ignore the leading spaces with -eq? The code in question just calls strtol.

Cheers,
-- 
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Junio C Hamano· May 21, 2005, 17:24 UTC · re: Herbert Xu · lore

Re: [PATCH] Fix use of wc in t0000-basic

>>>>> "HX" == Herbert Xu <herbert@gondor.apana.org.au> writes:

HX> Are you sure that it didn't ignore the leading spaces with -eq? HX> The code in question just calls strtol.

Sorry, I am not sure whose fault it was, and the recollection comes from my distant past. It could have been that the smallish shell in that semi-embedded environment had an incompatible built-in "test" command which was burning me, but I distinctively remember changing many of the vendor supplied shell script that had:

    if test " $number" -eq 3
    then
        ...

either stripping dq around it or simply removing the space from there, depending on how that $number was generated.

Since I assume we are only talking about portability across POSIXy world I do not think this is a big issue.

← back to recent threads