File exist test


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File exist test
# 1  
Old 04-15-2009
File exist test

Can someone please shed light on why this may not be working, file does exist, but I get an error


if [[ ! -f `${source_path}/${file_mask}` ]]
then
echo "No ${source_path}/${file_mask} found - ">> ${logfile}
result=1
check_result ${result} "Failed to find file ${source_path}/${file_mask}"
fi


source_path=$HOME/xxxx
file_mask=iamafile_[0-9][0-9][0-9]_[a-z].dat

Many Thanks
# 2  
Old 04-15-2009
Quote:
Originally Posted by Pokermad
Can someone please shed light on why this may not be working, file does exist, but I get an error
Which error?

[ code ][ /code ] tags (sans the spaces) help to keep source code and other listings readable (especially indentions, like this)
Code:
source_path=$HOME/xxxx
file_mask=iamafile_[0-9][0-9][0-9]_[a-z].dat

 if [[ ! -f "${source_path}/${file_mask}" ]]
       then
                echo "No ${source_path}/${file_mask} found - ">> ${logfile}
                result=1
                check_result ${result} "Failed to find file ${source_path}/${file_mask}"
       fi

The (probable) problem was that you used backticks when there was no command to be executed. Use double quotes instead.
# 3  
Old 04-15-2009
Backstick vs quotes

Thanks Pludi.

I tried that as well, but that does not interpret the filename i.e. it tests to see if $HOME/xxxx/iamafile_[0-9][0-9][0-9]_[a-z].dat exists rather than $HOME/xxxx/iamafile_111_b.dat

Thanks in advance
# 4  
Old 04-15-2009
If it checks for a file named "iamafile_[0-9][0-9][0-9]_[a-z].dat", you've disabled globbing somewhere or are using one weird kind of shell, because this is a valid file pattern which should match any file from iamafile_000_a.dat to iamafile_999_z.dat
# 5  
Old 04-15-2009
this can work ( not tested)

Quote:
file_mask=`ls iamafile_[0-9][0-9][0-9]_[a-z].dat`
and use double quotes in test command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to test if remote dir exist?

Hello, :) I'm trying to test if a remote directory exist per ssh, I have already made a ssh key and it works : #!/bin/bash HOST="10.10.1.22" FILE_PATH="/var/wwww/html" ssh -q $HOST ] && echo "Directory exists" || echo "Directory does not exist"; Output : ... (4 Replies)
Discussion started by: Arnaudh78
4 Replies

2. Shell Programming and Scripting

File not exist in direct

friends can do this from unix AIX I need to ask for a file that does not exist then if file.txt * **** echo "execute procedure" else *** echo "File does not exist" if You can sucedere that the file does not exist (2 Replies)
Discussion started by: tricampeon81
2 Replies

3. Shell Programming and Scripting

Test variables exist using a for loop

I am setting a number of variables in my script and I would like to test they exist by using a for loop. e.g. VAR1=abc VAR2=xyz for i in VAR1 VAR2; do if ; then echo Alert fi done But of course the $i doesn't refer to the variable itself, rather to the strings VAR1 & VAR2. Does... (2 Replies)
Discussion started by: Catullus
2 Replies

4. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

5. Programming

Kernel module - How to test if file doesn't exist

Hey, I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part: struct file *fp; /* ... */ fp = filp_open("some/file/on/my/pc", O_RDONLY, 0); if(fp == NULL) { ... (15 Replies)
Discussion started by: disaster
15 Replies

6. 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

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

8. Shell Programming and Scripting

How to Check whether list file present in TXT file exist or not

Hi All, I have txt file which has list of files. I have to check whether these files exist or not. Thanks supriya (6 Replies)
Discussion started by: supriyabv
6 Replies

9. Shell Programming and Scripting

Have a shell script check for a file to exist before processing another file

I have a shell script that runs all the time looking for a certain type of file and then it processes the file through a series of other scripts. The script is watching a directory that has files uploaded to it via SFTP. It already checks the size of the file to make sure that it is not still... (3 Replies)
Discussion started by: heprox
3 Replies

10. UNIX for Advanced & Expert Users

Printer Error(the Object Instance Test Does Not Exist)

Hello, i need some help about how to set up a high velocity impact printer in UNIX SCO 5.05, this printer is attached with a parallel port in a PC(host), the host use tunemul to access unix.(this reference is just to ask you if this is a local or remote connection, just to be sure), so, i... (2 Replies)
Discussion started by: jav_v
2 Replies
Login or Register to Ask a Question