# [PATCH] Fix use of wc in t0000-basic

10 messages from 2005-05-21 to 2005-05-21. Participants: Daniel Barkalow, Sean, Junio C Hamano, Herbert Xu.
Thread: https://gitlist.dev/t/680

## Daniel Barkalow, 2005-05-21 00:49

Subject: [PATCH] Fix use of wc in t0000-basic
Message-ID: <Pine.LNX.4.21.0505202045580.30848-100000@iabervon.org>
URL: https://gitlist.dev/e/Pine.LNX.4.21.0505202045580.30848-100000%40iabervon.org

```
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



```

## Sean, 2005-05-21 01:08

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <4600.10.10.10.24.1116637737.squirrel@linux1>
URL: https://gitlist.dev/e/4600.10.10.10.24.1116637737.squirrel%40linux1
In-Reply-To: <Pine.LNX.4.21.0505202045580.30848-100000@iabervon.org>

```
On Fri, May 20, 2005 8:49 pm, Daniel Barkalow said:
> 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, 2005-05-21 01:10

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <Pine.LNX.4.21.0505202109460.30848-100000@iabervon.org>
URL: https://gitlist.dev/e/Pine.LNX.4.21.0505202109460.30848-100000%40iabervon.org
In-Reply-To: <4600.10.10.10.24.1116637737.squirrel@linux1>

```
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, 2005-05-21 01:13

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <4616.10.10.10.24.1116637985.squirrel@linux1>
URL: https://gitlist.dev/e/4616.10.10.10.24.1116637985.squirrel%40linux1
In-Reply-To: <Pine.LNX.4.21.0505202109460.30848-100000@iabervon.org>

```
On Fri, May 20, 2005 9:10 pm, Daniel Barkalow said:
> 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, 2005-05-21 01:16

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <Pine.LNX.4.21.0505202113500.30848-100000@iabervon.org>
URL: https://gitlist.dev/e/Pine.LNX.4.21.0505202113500.30848-100000%40iabervon.org
In-Reply-To: <4616.10.10.10.24.1116637985.squirrel@linux1>

```
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, 2005-05-21 01:43

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <7vzmuppd4y.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vzmuppd4y.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <Pine.LNX.4.21.0505202113500.30848-100000@iabervon.org>

```
>>>>> "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, 2005-05-21 10:37

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <E1DZRMi-00021X-00@gondolin.me.apana.org.au>
URL: https://gitlist.dev/e/E1DZRMi-00021X-00%40gondolin.me.apana.org.au
In-Reply-To: <Pine.LNX.4.21.0505202045580.30848-100000@iabervon.org>

```
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, 2005-05-21 10:53

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <7vhdgwj1ed.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vhdgwj1ed.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <E1DZRMi-00021X-00@gondolin.me.apana.org.au>

```
>>>>> "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, 2005-05-21 11:01

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <20050521110129.GA7924@gondor.apana.org.au>
URL: https://gitlist.dev/e/20050521110129.GA7924%40gondor.apana.org.au
In-Reply-To: <7vhdgwj1ed.fsf@assigned-by-dhcp.cox.net>

```
On Sat, May 21, 2005 at 03:53:30AM -0700, Junio C Hamano wrote:
> >>>>> "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, 2005-05-21 17:24

Subject: Re: [PATCH] Fix use of wc in t0000-basic
Message-ID: <7vd5rkij9y.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vd5rkij9y.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20050521110129.GA7924@gondor.apana.org.au>

```
>>>>> "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.


```
