test if a filename exists with specified string (ksh)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test if a filename exists with specified string (ksh)
# 1  
Old 01-19-2007
test if a filename exists with specified string (ksh)

I'm trying to do a simple if statement that tests if a filename exists with a user specified string.

So say I have these files:

Assigned_1day_after_due_chuong
Assigned_1day_after_due_gallen
Assigned_1day_after_due_heidenre

and i'm running a script and want to know if a file exists with "chuong" in the name. I tried this but it never evaluates to true

if [[ (-f *$actualRecipient) ]]; then
...
fi

the variable actualRecipient = chuong

anyone know the correct syntax for this?
# 2  
Old 01-19-2007
Code:
if ls "*${actualrecipient}*" > /dev/null 2>&1 ; then 
....
fi

# 3  
Old 01-19-2007
Quote:
Originally Posted by bob122480
I'm trying to do a simple if statement that tests if a filename exists with a user specified string.

So say I have these files:

Assigned_1day_after_due_chuong
Assigned_1day_after_due_gallen
Assigned_1day_after_due_heidenre

and i'm running a script and want to know if a file exists with "chuong" in the name.
[snip]
For regular files:

Code:
set -- *"${actualrecipient}"*;[ -f "$1" ] && ...

Otherwise change the test.

Last edited by radoulov; 01-19-2007 at 06:58 PM..
# 4  
Old 01-19-2007
The thing is that *$var could match mutliple items and perhaps only one of them is file. So a secure solution would be to loop through them all to see if *$var matches a file or not. If you are very sure that *$var will match 1 or 0 files then you could use the single bracket if statement:
if [ -f *$var ] ; then echo match found ; fi
But this will do various things if *$var matches 2 or more items.
# 5  
Old 01-20-2007
Quote:
Originally Posted by Perderabo
The thing is that *$var could match mutliple items and perhaps only one of them is file. So a secure solution would be to loop through them all to see if *$var matches a file or not.
[snip]
Right,
mine fails if the first match is to a directory or pipe, for example.

May be something like this will be more suitable:

Code:
f_exists() {
	for f;do
		[ -f "$f" ] && return 0;
	done;
return 1
}

f_exists *"${actualrecipient}"*

# 6  
Old 01-20-2007
echo $var | grep chuong >/dev/null

if [[ $? -eq "0" ]]
then
... statement
else
..... statement
fi
# 7  
Old 01-21-2007
find <directory where files are> -type f -name \*chuong\* -exec <whatever you want to do with the file> {} \;

if it is complex what you want to do with this file you could use:

find <directory where files are> -type f -name \*chuong\* | \
while read FILE
do
<do whatever with the file which name is stored in the variable "FILE">
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Verify if filename exists

Hi, I have a variable returned from Oracle SQL Function which holds file names. I would like to test if all the file names mentioned in the string exists in a directory. If all the files exists print "exists", even if one file does not exists print "Does not exists". e.g. ... (3 Replies)
Discussion started by: pointers1234
3 Replies

2. Emergency UNIX and Linux Support

Waiting for wildcard filename to exists in while loop

Hi Experts, We are developing a script which will wait for the trigger file(with datetime in the trigger file name). But the problem is when I use 'while' loop to wait for the file, it waits for the filename with wilcard in it that is wait for 'Trigger*.done' file. :eek: Below is the script ... (4 Replies)
Discussion started by: Amey Joshi
4 Replies

3. UNIX for Advanced & Expert Users

Perl XML::DOM: How to test if element exists?

Hi, I'm trying to write a script for some xml file handling, but I'm not getting too far with it. I've got the following xml content <?xml version="1.0" encoding="UTF-8"?> <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <Operation name="OPER1"> <Action name="ACTION1">... (2 Replies)
Discussion started by: Juha
2 Replies

4. Shell Programming and Scripting

Test if string exists

How do I use bash to test whether a string occurs more than two times in a file? (2 Replies)
Discussion started by: locoroco
2 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. UNIX for Dummies Questions & Answers

rm: Unable to remove directory /mnt/users/test/logs/: File exists

rm: Unable to remove directory /mnt/users/test/logs/: File exists ls -latr total 191208 drwxrwxrwx 6 test echo 4096 Jul 3 22:36 .. -rwxrwxrwx 1 test echo 97692804 Jul 3 22:36 .nfsDFA4 drwxrwxr-x 2 test echo 4096 Jul 3 23:00 . M not able to delete... (4 Replies)
Discussion started by: solitare123
4 Replies

8. Shell Programming and Scripting

Problem when test to see if directory exists

Hi, I'm writing a shell script that will create a folder if it does not exist yet. Here's the script: (this if statement is inside a while loop) folderName="Pics" if ! test -d folderName then mkdir $folderName fi However, after the folder Pics has been created, every time the... (3 Replies)
Discussion started by: trivektor
3 Replies

9. Shell Programming and Scripting

ksh - test if string contains alphanumeric...

Okay I will let users input spaces as well :) I am having a mental block. I have done a couple of searches but havent found anything that I understand (the likes of :alpha: and awk). Basically I want to give the user an option to enter some text which will go down as a field within a flat... (3 Replies)
Discussion started by: tugger
3 Replies

10. Shell Programming and Scripting

how to test filename is file or directory

hi all plz let me how to test output of "tail -1 filelist.lst" is file or directory. regards -Bali Reddy (1 Reply)
Discussion started by: balireddy_77
1 Replies
Login or Register to Ask a Question