help- wildcard not working in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help- wildcard not working in shell
# 1  
Old 05-05-2011
Question 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 Smilie


Code:
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.

Last edited by pludi; 05-05-2011 at 10:29 AM..
# 2  
Old 05-05-2011
Can you check once with set -x option enabled and paste the output?
# 3  
Old 05-05-2011
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.

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

This User Gave Thanks to methyl For This Post:
# 4  
Old 05-09-2011
Thanks for your response. I am using ksh

I tried the following code like you have suggested
Code:
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 ----------

Quote:
Originally Posted by 116@434
Can you check once with set -x option enabled and paste the output?
Thanks for your reply. how can i check the set -x option option?

Last edited by Franklin52; 05-09-2011 at 05:59 AM.. Reason: Please use code tags
# 5  
Old 05-09-2011
try this
Code:
#!/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

This User Gave Thanks to ygemici For This Post:
# 6  
Old 05-09-2011
Quote:
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.
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:
Code:
for src_filename1 in $AI_LANDING/abc* ; do 
  whatEvahYouNeed2DoWith $src_filename1
done

Reading through your original post I have to ask:
Quote:
For testing purpose there is only one file present in the directory whose name starts with abc.
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
Code:
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.)

Last edited by mirni; 05-09-2011 at 06:59 AM..
This User Gave Thanks to mirni For This Post:
# 7  
Old 05-09-2011
Quote:
Originally Posted by ygemici
try this
Code:
#!/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

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cp not working in shell script but running perfectly from shell

Dear All, I have script. Dest="" IFS=' ' for translation in $(echo $MY_MAP) do t1=$(echo $translation | cut -d"=" -f1) t2=$(echo $translation | cut -d"=" -f2| cut -d"," -f1) if then Dest=$UNX/$u_product_path/$u_study_path/$UNXTR/$t2 break; ... (4 Replies)
Discussion started by: yadavricky
4 Replies

2. Shell Programming and Scripting

C-Shell!! Please help...cannot get it working...

Dear Friends, I am trying to get a script in c-shell working but no way! I don't know why but on my mac was working and now on linux is not! Basically the script should accept a root_file_name and work in batch on files. This is the script: #!/bin/csh -fn setenv IMAGIC_BATCH 1 ... (3 Replies)
Discussion started by: Mandrake83
3 Replies

3. Shell Programming and Scripting

What does " shell wildcard " shell script?

Hi, I just wondering what does " shell wildcard " Thank (1 Reply)
Discussion started by: guidely
1 Replies

4. Shell Programming and Scripting

wildcard in bash shell script

I tried to use the wildcard '*' in my bash script, but I can not get it work. Here is a simple example (list file names in current directory): ls ./* does not work in my bash script. But it works if I use ls ./ So is there any special syntax to use '*' wildcard in bash script (I tested the... (11 Replies)
Discussion started by: aerosols
11 Replies

5. AIX

wildcard not working in /etc/hosts.allow

Had any one made the wildcard "*" or "?" work in tcp wrappers configure file hosts.allow and hosts.deny in AIX? It won't work for me no matter how hard I try. I am trying to restrict the access of our ftp server and if I specify following in hosts.allow: ftpd : host01.domain.com... (0 Replies)
Discussion started by: cypher4066
0 Replies

6. Shell Programming and Scripting

shell script is not working..

Dear all, I am writing simple shell script. #/bin/bash ORACLE_HOME=/home/app/oracle/product/9.2.0 export ORACLE_HOME export ORACLE_SID=DATX $ORACLE_HOME/bin/sqlplus -s datadmin/password <<EOF execute testproc; exit; EOF ~ ~ In the above shell script, the oracle instance name is... (2 Replies)
Discussion started by: govindts
2 Replies

7. Shell Programming and Scripting

Which Shell I'm working?

Hi, I'm using FreeBSD 6. I want to know which Shell I'm using. So I gave the command: -sh-3.1$ ps -p $$ PID TTY TIME CMD 15547 pts/1 00:00:00 sh Which shell is "sh"? Is it C Shell or Bourne Shell or Korn Shell? Also, are Bourne & Korn shell one and the same or are... (4 Replies)
Discussion started by: freephoneid
4 Replies

8. Shell Programming and Scripting

if -z not working in SH shell

Hello all, Any idea how to check whether a variable holding null value or not. if -z option works fine in bash, where as it is not working in sh. bash-3.00$ sh $ TEST= $ if ; then > echo "Null" > else > echo "Not null" > fi sh: test: 0403-004 Specify a parameter with this command.... (2 Replies)
Discussion started by: rijeshpp
2 Replies

9. Shell Programming and Scripting

shell not working

hi freinds, i just learning shell programming. i usually work in bash shell, but when i need tcshell, i normally i/p chshell command and go there. but although i have installed all the neccessay patches for this shell my machine can not work in tshell. but it shows the shell to be present..... (0 Replies)
Discussion started by: hytechpro
0 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question