How can I make an if [ -f ] statement with wildcards?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I make an if [ -f ] statement with wildcards?
# 1  
Old 07-28-2004
Question How can I make an if [ -f ] statement with wildcards?

Normally you would have something like..

if [ -f FILENAME]
then
foo bar
fi

but what if you wanted to do something like

if [ -f FILENAME*]
then
foo bar
fi

How do I get Unix to accept anything that matches a pattern of FILENAME with anything after it during an in if statement?
LordJezo
# 2  
Old 07-28-2004
Quote:
but what if you wanted to do something like

if [ -f FILENAME*]
then
foo bar
fi
Do it. It worked for me.
# 3  
Old 07-28-2004
Well would you look at that.

Ha.

Thanks.
LordJezo
# 4  
Old 07-29-2004
Using test -f for multiple files is unsafe and you should be aware of its limitations. Say you have a directory that contains a file and a directory...

$ file *
adir: directory
afile: ascii text

Testing for the file....

$ if [ -f a* ]
> then
> echo true
> else
> echo false
> fi
false

...i.e. the wrong result!

See this thread for details.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. UNIX for Dummies Questions & Answers

make - two wildcards in the target

Suppose I have a Makefile like this: x/Ax: x/Bx touch x/Ax y/Ay: y/By touch y/Ay z/Az: z/Bz touch z/Az It does not look like a proper style for make. I would like to write something like %/A%: %/B% touch %/A% but make does not accept it. What is a right way to shorten such a... (1 Reply)
Discussion started by: ybelenky
1 Replies

3. UNIX for Advanced & Expert Users

Wildcards

These 2 websites do a GREAT job of explaining different types of wildcards. I learned about the categories of characters which I never knew about at all. GNU/Linux Command-Line Tools Guide - Wildcards GREP (1 Reply)
Discussion started by: cokedude
1 Replies

4. Shell Programming and Scripting

Using wildcards in "if" statement

hey guys, what i am doing is that i would like the program to check if there was anything inputted. If nothing is imputed, it is suppose to display a message. echo -n "Enter Author:" read Author #echo -n "Enter Title:" #read Title if ] ; then echo "you enter something" else echo... (9 Replies)
Discussion started by: gregarion
9 Replies

5. Shell Programming and Scripting

wildcards in "if then" statement

Hello, I would like to use a simple "if then" test to check if an argument to a command begins with "http://" as follows: if http://* ]]; then command fi but the wildcard just seems to be ignored, ie., it will only execute the command if the expression is strictly "http://" with... (5 Replies)
Discussion started by: Allasso
5 Replies

6. Shell Programming and Scripting

wildcards in "if then" statement

Hello, I would like to use a simple "if then" test to check if an argument to a command begins with "http://" as follows: if http://* ]; then command fi but the wildcard just seems to be ignored, ie., it will only execute the command if the expression is strictly "http://" with nothing... (0 Replies)
Discussion started by: Allasso
0 Replies

7. Shell Programming and Scripting

wildcards with if statement?

Hello i am trying to use the wildcards with the if statement but it is displaying the error like this one if * | ** | * ] Any body can help me to for using the wild card option in the if case but i have used this code and working well with the case statement to enter the name without the... (14 Replies)
Discussion started by: murtaza
14 Replies

8. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies

9. UNIX for Dummies Questions & Answers

How can I put wildcards in an if statement that uses variables?

With the if statement: if How can I make it so it accepts a wildcard after the ${CURR_DAY_MONTH} variable? Putting a -f /webtrends/SUN/mrw2/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* won't work, right? I think I need some kind of special character so it knows the wildcard is... (3 Replies)
Discussion started by: LordJezo
3 Replies

10. UNIX for Dummies Questions & Answers

Wildcards in VI

I'm trying to delete lines from a large text file using VI. Every line that I am wanting to delete start with 'S' - all others do not. (A list of users) I've tried using * but doesn't seem to like it...any ideas... Doesn't have to be VI - but I'm better with VI than sed/awk. (8 Replies)
Discussion started by: peter.herlihy
8 Replies
Login or Register to Ask a Question