I have to read in_dir and file_name values from a file and should check the count
For some instance lets say
in_dir hold /int/inbound
file_name hold EMP*.dat
below is one of lines in my shell script and we use AIX
file_cnt=`ls $in_dir/$file_name | wc -l`
even if some files(EMP001.dat, EMP002.dat) exist with that name, it is not giving expected result if, is it because the values i read from file are considered as common text.
Please let us know how to handle this, i need to count all the files with EMP<any>.dat
That method is prone to failures if a directory accumulates too many files, especially on a system like HP-UX where you may be using an old shell. It would be better to use grep -c, though it takes regexes, not globs. A dumb/simple conversion from glob to regex can be done by replacing . with \. and * with .*, prepending ^ and appending $ to force it to match entire lines, but this may not be perfect.
#!/bin/ksh
while read DIR HOLD GLOB
do
GLOB="${GLOB/./\\.}" # Replace . with \.
GLOB="${GLOB/\*/.*}" # Replace * with .*
GLOB="^${GLOB}\$" # Turn GLOB into ^GLOB$ to match entire line
if [ ! -e "$DIR" ]
then
echo "$DIR does not exist"
elif [ ! -d "$DIR" ]
then
echo "$DIR is not a directory"
continue
else
COUNT="`ls "$DIR" 2>/dev/null | grep -c "$GLOB"`"
echo "Found $COUNT matching files in directory"
fi
done < listfile