hi,
i need to check the existence of all files starting with abc in a directory. The code works fine with a particular file name, but the file existence is not detected when i use wildcard character (abc*)
kindly suggest what could be the issue
src_filename1=$AI_LANDING/abc*
printf "Checking for file \n $src_filename1 ...."
if [[ ! -s $src_filename1 ]]
then
print -u2 "ERROR: File was not found"
print -u2 "FILENAME: $src_filename1"
print -u2 ""
else
print " $src_filename1 EXISTS!!"
fi
For testing purpose there is only one file present in the directory whose name starts with abc.
I tried the following code like you have suggested
src_filename1=$AI_LANDING/`ls -1d abc* 2>/dev/null|tail -1`
printf "Checking for file ${src_filename1} ...."
if [ ! -s "${src_filename1}" ]
then
print -u2 "ERROR: File was not found"
print -u2 "FILENAME: ${src_filename1}"
print -u2 ""
else
print " ${src_filename1} EXISTS!!"
fi
This is evaluating the src_filename1 as only the AI_LANDING path without the file name, and giving the result as EXISTS! even when the file is not present.
Any suggestions?
---------- Post updated at 01:47 AM ---------- Previous update was at 01:46 AM ----------
Thanks for your reply. how can i check the set -x option option?
#!/bin/ksh
AI_LANDING=/searchdir
ls -1 ${AI_LANDING}/abc*|while read -r src_filename1 ; do
print "Checking for file $src_filename1 ...."
if [[ ! -s $src_filename1 ]]
then
print -u2 "ERROR: File '$src_filename1' was not found or has not a size greater than zero"
print -u2 ""
else
print "$src_filename1 EXISTS!!"
print -u2 ""
fi
done
If it evaluates src_filename1 as the path, then it does exist -- it's the directory $AI_LANDING. And it's not empty, so the test passes. All kosher there.
As for your original problem, I'd put it in a for loop:
for src_filename1 in $AI_LANDING/abc* ; do
whatEvahYouNeed2DoWith $src_filename1
done
Reading through your original post I have to ask:
Is this "testing - purposes" file named abc<somethin> empty by any chance? Because then it wouldn't pass your test...
When you want to test for existence, use
if [ -f $file ] ; then
test instead. -f returns true if the file exists and is a regular file (as opposed to a directory, block or special device, etc.)
I believe you need execute permissions on the dir to perform tests on files inside. You have both r and x perms, so that's ok.
Please see my previous post and look at your test file abc<>. It's not empty, or?
If there are no files inside the dir, shell does not expand the asterisk, and so your variable src_filename1 will contain 'landingpath/abc*'. And there is no such file...
just try this:
$ for i in nonexistingdir* ; do echo $i ; done
nonexistingdir*
That "Unix permissions " warning is odd... but I wouldn't worry about it too much. It says, your numbers will be off, if you are testing files that you can't read.