I am checking for the presence of certain no of files in a directory. Only if the required no of files are present should I continue with my processing.
For e.g. in the temp directory below, there are 4 files of the format sample_aa(bb)(cc)(dd)_test. I need to check the count of these four before continuing. Trying something like the below:
cd /temp
for i in aa bb cc dd
do
fname=$(ls sample_${i}_test | wc -l)
if [ $fname -eq 4 ]
then
echo "There are "$fname" files present in temp"
fi
done
When I echo $fname , I get the value of 1 for each of the four files. I need to check for the value of 4 which it holds at the end of the loop basically. Thanks in advance.
for i in aa bb cc dd
do
fname="${fname} sample_${i}_test"
done
cd /temp
if [[ $(ls -l ${fname} | wc -l) -eq 4 ]]; then
echo "${fname} files are available in /temp"
fi
Your if statement is embedded within your for loop, that's your problem. So your ${fname} variable will change each time you iterate over the variables in your for loop. The if statement should be performed after the for loop.
Assuming you are using a shell that accepts POSIX standard arithmetic expansions, a more direct approach would be something like:
cd /temp # Did you really mean /tmp???
fcnt=0
for i in aa bb cc dd
do if [ -e "sample_${i}_test" ]
then fcnt=$((fcnt + 1))
fi
done
if [ $fcnt -eq 4 ]
then echo "There are $fcnt files present in /temp"
else echo "There are only $fcnt files present in /temp; try again later."
exit 1
fi
echo 'Continue processing...'
If you're going to use this method, you need to add a cd to get into the correct directory; you don't need the ls options, and the ERE in awk is wrong for the specified files. The following would work (but would be less efficient than using shell built-ins as shown in previous suggestions):
cd /temp;ls | awk '/^sample_(aa|bb|cc|dd)_test$/ {a++} END{if(a > 3) print "files have been found."; else print "files have not been found."}'
The value of fcnt is getting reset to 0 immediately after the for loop
cd /temp # This was actually /tmp
fcnt=0
for i in aa bb cc dd
do if [ -e "sample_${i}_test" ]
then fcnt=$((fcnt + 1))
fi
done
echo $fcnt # This has a value of 0
if [ $fcnt -eq 4 ]
then echo "There are $fcnt files present in /temp"
else echo "There are only $fcnt files present in /temp; try again later." #Says 0 files present even though there are files
exit 1
fi
echo 'Continue processing...'
Also, is there a way to alert the user by echoing the missing file(s)?
I am in the right directory and the files are indeed present there. But when I run, I get the message below:
sample_aa_test is not present
sample_bb_test is not present
sample_cc_test is not present
sample_dd_test is not present
0
There are only 0 files present in /temp.
0000000 / t e m p / s a m p l e _ a a _ t
057 164 145 155 160 057 163 141 155 160 154 145 137 141 141 137 164
0000020 e s t \n / t e m p / s a m p l e _
145 163 164 012 057 164 145 155 160 057 163 141 155 160 154 145 137
0000040 b b _ t e s t \n / t e m p / s a m
142 142 137 164 145 163 164 012 057 164 145 155 160 057 163 141 155
0000060 p l e _ c c _ t e s t \n / t e m p
160 154 145 137 143 143 137 164 145 163 164 012 057 164 145 155 160
0000100 / s a m p l e _ d d _ t e s t \n
057 163 141 155 160 154 145 137 144 144 137 164 145 163 164 012
0000120
Pls let me know what can be inferred from the above
We have now determined that there are no spaces, tabs, or carriage return characters at the ends of the filenames and that there are no embedded control characters in the middle of the filenames.
What operating system are you using?
What shell are you using?
What do you get when you run the updated script:
#!/bin/ksh
set -xv
cd /temp # This was actually /tmp
fcnt=0
for i in aa bb cc dd
do if [ -e "sample_${i}_test" ]
then fcnt=$((fcnt + 1))
else echo "sample_${i}_test missing"
fi
done
if [ $fcnt -eq 4 ]
then echo "There are $fcnt files present in /temp"
else echo "There are only $fcnt files present in /temp; try again later."
exit 1
fi
echo 'Continue processing...'
What did you mean by the comment shown in red above? Are these files in /temp or in /tmp?
I repeat, please run the updated script shown in post #16 in this thread and show us the output. We need to see the trace output to figure out what is going on.