Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to assign a variable to the number of files NOT found by an ls command.
Suppose we have the following variables:DIRECTORY=/vw/dev/icwdev/opins
FILE_LIST='UVW1000 UVW1002 UVW1004 UVW1006 UVW1008 UVW1009 UVW1010 UVWXXXX'In this example, all the files in $FILE_LIST exist in $DIRECTORY, except UVWXXXX.
I can assign a variable to the number of files which exist in the directory using:
NO_OF_EXISTING_FILES=`ls -1 $FILE_LIST | wc -l`
I want to have a variable which is the opposite - the number of files which don't exist in the directory. I tried the following...
NO_OF_MISSING_FILES=`ls -1 $FILE_LIST | wc -l >/dev/null`
...on the hope that the real file listing would be 'thrown away', but the lines reading 'No such file or directory', having not been thrown away, would be picked up by wc -l. However, the wc -l command is not picking up input from standard error, which, I suppose, I should have expected.
How do I get wc -l to count the number of standard error lines from a command that's piped into it?

Sorry, I misread your question. At least, the above throws away the 'file not found' error. I'll think about this, and if I come up with a better answer, I'll let you know.

replace:
NO_OF_EXISTING_FILES=`ls -1 $FILE_LIST | wc -l`
with:NO_OF_EXISTING_FILES=`ls -1 $FILE_LIST 2> /dev/null | wc -l`
Luke Chi

I misunderstood it too. Here is the right answer.
#!/bin/sh <====== borne shell
NO_OF_MISSING_FILES=`ls -1 $FILE_LIST 3>&2 2>&1 1>&3 | wc -l`Luke Chi

sorry. i forgot to get rid of standard output:
NO_OF_MISSING_FILES=`ls -1 $FILE_LIST 3>&2 2>&1 1>&3 > /dev/null | wc -l`
Luke Chi

![]() |
Modifying contents of fil...
|
days left in current mont...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |