Help with UNIX test and wc Command

I want to xheck if a file exists that uses wildcards as only the partial filename is known using the test Command, and when it exists then output just the number of lines in the file... do not include the filename. Then this output, is it captured by the CommandOutput or the ReturnValue as I want to use this value in another program?

Code:

 
 test -e /logs/error_logs/*PROC* && wc -l /logs/error_logs/*PROC* || echo No
 

When a file exists, this will display the #line + filename, i.e.:
650 logs/error_logs/03_2017_PROCESSED.log
I just want 650.

I tried

 
 test -e /logs/error_logs/*PROC* && wc -l < /logs/error_logs/*PROC* || echo No
 

But it displays the following message:

sh: /logs/error_logs/*PROC*: No such file or directory

and returns No .

TIA

The test -e delivers a meaningful result ONLY if it is supplied one single file name. wc -l with its input redirected will do the trick ONLY if the redirection is from one single file.
If theres NO matching file, you'll get above error msgs; if there's more than one, you'll get ambiguous redirect .

How about dropping the test and using like

wc -l 2>/dev/null <*PROC*  || echo NO
9

OR

wc -l 2>/dev/null <*PROC*  || echo NO
NO

Make sure the stderr redirection is prior to the input redir so error msgs disappear into nirvana.

1 Like

Some shells might need braces to ensure the stderr redirection happens first and include the input redirection.?

{ wc -l <*PROC*; } 2>/dev/null || echo NO