Check for file existence using wildcards


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for file existence using wildcards
# 1  
Old 10-14-2009
Check for file existence using wildcards

I am using the following command to check for files on a Unix (Solaris 9) and on Linux:

Code:
if (-r *.[Ll][Aa][Ss]) then
   echo " las file found"
else
   echo " no las file found"
endif

If no las file is present, the "no las file found" message is displayed. If a las file is present, however, I get the following:

Code:
*.[Ll][Aa][Ss]: Ambiguous

Does anyone know what it is that I am doing wrong?

Thanks,
Paul Hudgens
Denver

Last edited by vgersh99; 10-14-2009 at 01:52 PM.. Reason: code tages PLEASE!
# 2  
Old 10-14-2009
-r is a unary operator - meaning it takes one argument. Wildcards can return many file names.

I'm using the -q option, not all grep versions have it. Change as needed
Code:
ls |  grep -q  '\.[Ll][Aa][Ss]$'
if [[ $? -eq 0 ]] ; then
     echo 'ok'
else
     echo ' not ok'
fi

# 3  
Old 10-14-2009
Code:
#!/bin/ksh

[ -r *.@([lL][aA][sS]) ] && echo found las || echo NOT found las



---------- Post updated at 12:53 PM ---------- Previous update was at 12:51 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 4  
Old 10-14-2009
Quote:
Originally Posted by vgersh99
Code:
#!/bin/ksh

[ -r *.@([lL][aA][sS]) ] && echo found las || echo NOT found las

Why did you choose to use the @() extended wildcard? That's not what the OP was asking for. In keeping with his (preferred?) formatting and structure:
Code:
if ls *.[Ll][Aa][Ss] >/dev/null 2>&1; then
    echo las not found
else
    echo found las
fi

# 5  
Old 10-19-2009
Check for file existence using wildcards

Azhrei's use of the /dev/null method reminded me that I had used this code previously. I was able to find it in some old code and got the following to work in both Unix and Linux. Thanks everyone for the help.

Code:
 
set LASInputFile = ""
( ls -d *.[Ll][Aa][Ss] > /dev/null ) >& /dev/null
if ( "$status" ) then
   echo ""
   echo "  A LAS file was not found in the main level directory.  Exiting program."
   echo ""
   exit
else
   set LASInputFile = `ls *.[Ll][Aa][Ss] | head -1`
endif

Paul Hudgens
Denver
# 6  
Old 10-19-2009
Thanks for replying and including the solution you ended up using. Smilie

Btw, that looks like C shell code due to the redirection syntax. You really don't want to use the C shell for any kind of production work; it's too buggy and idiosyncratic. Google for "101 reasons not to use the C shell". Smilie

A problem with both my code and yours is the assumption that the current directory is readable and thus ls(1) won't fail due to anything other than a wildcard not being replaced by the shell. In the real world this is a reasonable assumption, but truly defensive shell scripting would check for that with and additional if statement of [ -r . ]

Because both of our routines use ls(1) they pay a performance penalty for the fork() and exec() required to run ls(1). If all built-in commands could be used it would be much faster.

For example, using for i in *.[Ll][Aa][Ss]; do break; done would result in the wildcard being expanded but the loop breaking the very first time through. The value of i will either be a real filename (as the wildcard was expanded) or the string *.[Ll][Aa][Ss] and a simple [ -e "$i" ] will determine that. This solution requires no fork/exec and will be much faster.

But I'm betting that the performance is not a big issue anyway. Smilie
# 7  
Old 10-19-2009
Quote:
Originally Posted by Azhrei
Why did you choose to use the @() extended wildcard?
Just a matter of taste that does not require any external utility which is somewhat useless.
Quote:
Originally Posted by Azhrei
That's not what the OP was asking for.
How's that?
Actually this is exactly what the OP trying to accomplish initially.
Quote:
Originally Posted by Azhrei
In keeping with his (preferred?) formatting and structure:
Code:
if ls *.[Ll][Aa][Ss] >/dev/null 2>&1; then
    echo las not found
else
    echo found las
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File existence check

hi i wanted to check if the file exist or not(multiple files) DIRE=/home/V478 if ; then echo "file present" else echo "file not present" fi But i am getting the error as : [: unexpected operator/operand (3 Replies)
Discussion started by: ATWC
3 Replies

2. UNIX for Dummies Questions & Answers

To check for existence of a file

I need to check for the existence of a file *.log in a specific directory using a perl script. Presently am not in that particular directory. So i am using chdir ("/path/to/my/file) And then i am using the -e in an if statement to check if it exists. if (-e $File) {......} $File contains the... (1 Reply)
Discussion started by: manutd
1 Replies

3. Shell Programming and Scripting

command to check existence of file name which has regex

Hi All. Pls help me with the command to check existence of files (I'll mention name of the file as regex) and proceed with my further processing if atleast one of them exists in detail, I've a dir /tmp/TURP, which may or may not have files named with "exter*.txt" I need to check and... (2 Replies)
Discussion started by: skpvalvekar
2 Replies

4. Shell Programming and Scripting

script to check for existence of file (or else sleep for x time)

Hi Forum. I have a script that accepts 3 input parameters (source directory, list file text, sleep time) and checks for the presence of files. If not there, script goes to sleep for certain amount of time provided by 3rd input. list file text contains 1 entry but may contain more (file... (13 Replies)
Discussion started by: pchang
13 Replies

5. Shell Programming and Scripting

How to check for file existence?

i want to check if the file is in the directory or not, and also it should be handle error conditions, like missing files and report the error and exit. i did something like this: file ="hello" if !test -e "${file}" then echo "No such files exist!" exit 1 else do something....... fi ... (1 Reply)
Discussion started by: mingming88
1 Replies

6. AIX

Check for File Existence

I have requirement where i need to search for files which start with SALESORDER and PURCHASEORDER. i need to process the files with SALESORDER first and then PURCHASEORDER. If SALESORDER files are not there i dont want to process PURCHASEORDER and i want to come out of script. I have written a code... (4 Replies)
Discussion started by: dsdev_123
4 Replies

7. AIX

check for file existence

Hello I am having a requirement like if there is no file in the directory then i need a message to pop on after the execution of the script. My script basically does for File in `ls -t $DIRECTORY | tail -1`; if there is no file the DIRECTORY then the script is simply exiting with out... (2 Replies)
Discussion started by: dsdev_123
2 Replies

8. AIX

how to check the existence of a file using korn shell?

we have tranferred an ear from local server to remote server using ftp.consider, we have an ear file named a.ear in remote server,again if we transfer the same file named a.ear from local server to remote server.we need the kshell to check the existence of the ear file in remote server,and if the... (3 Replies)
Discussion started by: karthikprasathk
3 Replies

9. AIX

how to check the existence of a file during ftp using korn shell?

i can able to transfer a file from build server(AIX)to webserver using ksh through ftp.my query is to check the existence of file while transfering from one server to other .i.e i need some command or script that checks the existence of file with same name in both server,within ftp syntax. ... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

10. Shell Programming and Scripting

Csh to check for existence of file

Hi, I would like to check the existence of files (doesn;t matter the number of files) in a directory. My file is named in the following manner (always ending with " myfile "). Can anybody give me some guidance? EG: abc1_myfile sdfr_myfile sffgd_myfile and so on ...... My... (9 Replies)
Discussion started by: Raynon
9 Replies
Login or Register to Ask a Question