help- wildcard not working in shell

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 :confused:

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.

Can you check once with set -x option enabled and paste the output?

It always helps to know what Shell you have.

One unrefinded idea which should still work whether or not the file exists. Of couse if the file does not exist, ${src_filename} is blank.

src_filename1=`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
1 Like

Thanks for your response. I am using ksh

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?

try this

#!/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
1 Like

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.)

1 Like

Hi,

I tried the above code but it is still not finding the file at the directory..This is the output I get

  • Unix file permissions may prevent an accurate summation of reported data.
    /abc*: No such file or directory

Do you have execute permissions on that directory?
Can you post output of

ls -dl ${AI_LANDING}

This is the output

drwxr-xr-x 14 userid etl 28 May 9 05:05 .

What does this say about the permission

---------- Post updated at 05:15 AM ---------- Previous update was at 05:11 AM ----------

Only having trouble while using wildcard. The results are correct when the filename is used.

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?

Hi,

This code worked and is able to identify files starting with abc :slight_smile: (I had made a mistake wid one of the variables while using your code earlier)

So now it is correctly identifying the presence of file..just one small thing

It gives the correct message when file exists, but when it does not exist the following default msg is displayed

  • Unix file permissions may prevent an accurate summation of reported data.
    landingpath/abc*: No such file or directory

instead of our message which should display

File '$src_filename1' was not found or has not a size greater than zero"

---------- Post updated at 05:39 AM ---------- Previous update was at 05:38 AM ----------

Filesize is greater than zero. Can u see my latest post..

OK. But that's what it says:

landingpath/abc*: No such file or directory

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.