Check for file existence using wildcards

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

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:

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

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

Thanks,
Paul Hudgens
Denver

-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

ls |  grep -q  '\.[Ll][Aa][Ss]$'
if [[ $? -eq 0 ]] ; then
     echo 'ok'
else
     echo ' not ok'
fi
#!/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

```text
 and 
```

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

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:

if ls *.[Ll][Aa][Ss] >/dev/null 2>&1; then
    echo las not found
else
    echo found las
fi

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.

 
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

Thanks for replying and including the solution you ended up using. :slight_smile:

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". :wink:

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

Just a matter of taste that does not require any external utility which is somewhat useless.

How's that?
Actually this is exactly what the OP trying to accomplish initially.

Yep, you're right. :b: I had a senior moment. (I thought there were vertical bars between each set of brackets.)

Thanks for the reply. This is not the first time that someone has suggested that I not use C Shell coding for my scripts. The problem is that all of my users log in to, and launch their programs from, the C Shell. I really have no choice but to write my scripts for that shell.

For the present, I'm not going to worry about whether a directory is readable or not. I'm assuming that none of my users are sufficiently UNIX savvy to know how to create a directory that is not readable.

Thanks again for all the help and input.

Paul H.

Unless you have an unusual environment, you can put "#!/bin/bash" (or some other full pathname to a shell) and then use that shell to write your scripts. It doesn't matter which shell the users log into, as the "#!" ensures that the exec() of the script succeeds, replacing the C shell with your chosen shell.

Heh, I understand. :slight_smile: