Check file and increment

My scripts excepts 4 files

ABCD_01
ABCD_02
ABCD_03
ABCD_04

I want to check for these files , and increment counter one by one .
at the end i would like to echo as 4 of 4 expected instances of file found .

I tried something like thsi

$counter =1
if  [-f ABCD_01] 
counter=counter+1

i need to loop in and increment .. pls suggest

Maybe ...

#!/bin/bash

for ((X=1; X<=4; X++))
do
  if [ -f "file$X" ]
  then
    echo "file$X exists"
  else
    echo "file$X missing"
  fi
done

exit 0
#finis
[house@leonov] ls file*
file0  file1  file3  file4  file5
[house@leonov] bash code.bash
file1 exists
file2 missing
file3 exists
file4 exists
$> ll
total 12
drwxr-xr-x 2 root root 4096  9. Jul 12:24 .
drwxr-x--- 5 root root 4096  9. Jul 12:22 ..
-rw-r--r-- 1 root root    0  9. Jul 12:22 ABCD_01
-rw-r--r-- 1 root root    0  9. Jul 12:22 ABCD_02
-rw-r--r-- 1 root root    0  9. Jul 12:22 ABCD_03
-rw-r--r-- 1 root root    0  9. Jul 12:22 ABCD_04
-rw-r--r-- 1 root root    0  9. Jul 12:24 ABCD_05
-rw-r--r-- 1 root root    0  9. Jul 12:24 ABCD_06
-rw-r--r-- 1 root root    0  9. Jul 12:24 DCBA_01
-rw-r--r-- 1 root root   32  9. Jul 12:23 list
$> cat list
ABCD_01
ABCD_02
ABCD_03
ABCD_04
$> Z=0; while read LINE; do [[ -e "$LINE" ]] && let Z=$Z+1; done < list; echo $Z
4

Another approach (avoiding ksh93 as O/P has basic Solaris).

#!/bin/ksh
PREFIX="ABCD"
for SUFFIX in "01" "02" "03" "04"
do
        FILENAME="${PREFIX}_${SUFFIX}"
        if [ ! -f "${FILENAME}" ]
        then
                echo "Error: instance of ${PREFIX} missing"
                exit
        fi
done
echo "4 of 4 instances of ${PREFIX} found"

hi , I tried the following

rvst_cntr=0;
while read line;
do [[ -e "$line" ]] && rvst_cntr=$rvst_cntr+1; done < file_list.txt
echo "$rvst_cntr of 4 files found "

file_list.txt contains list of files

The o/p is coming as 0+1+1 of 4 files found

can't get the addn part done ...

Pls suggest

Please post sample input and sample output making it clear whether you are checking whether files exist in the current directory or whether you are checking whether file names are contained in a file called file_list.txt ... or both?

Reference your counting problem, compared with zaxxon post where is your "let" ?

1 Like