File exists or not

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers File exists or not
# 1  
Old 05-08-2018
File exists or not

Dear All,

I am facing a small issue in my code where in I am searching for a file in a directory and if found it sends me a message along with the time and filename.

However if a file is not found based on the search pattern, the result which I am getting is all the files present in that directory. Below is the code I have written and do let me know the resolution for it.

Code:
file="/home/fileback/var2.txt"

 ls -lrt 
`gzgrep -il "1234566789" 2018-"05-07"*ship*` >> $HOME/fileback/var2.txt
                                        if [ -f "$file" ]
                                        then
                                            echo "File found"
                                            cat "$file"
                                        else
                                            echo "File not found"
                                        fi

Above is the code and if I enter the patter as mentioned above and it is not found then expected output should be "file not found" and the program should exit.
However it is giving me all the files present in the directory under the ls -lrt command and creating a file var2.txt with all the files present in the search directory.

Regards
Gaurav Kumar
# 2  
Old 05-08-2018
Moderator's Comments:
Mod Comment This thread has been moved from a non-technical forum to a technical forum.


You haven't told us what operating system or shell you're using and gzgrep is not a "standard" utility that is available on all systems. (And, since it isn't present on the system I'm using, I have no idea what it is supposed to do.)

Running ls with no operands will give you a list of all filenames in the directory in which you run your script as long as the first character of the filename in not a period.

It isn't obvious to me why you would want to execute gzgrp in a command substitution instead of running it directly in your script. But no mater which way you invoke it, the redirection you're using will create the file that you then look for in the following if statement.

If gzgrep has options similar to the "standard" grep utility, you might want to look at the -q option and just check the exit status of gzgrep to determine if it found a match or not instead of looking for a file that will always be present.

Alternatively, depending on the output produced by gzgrep, you might want to look at the size of the file it produces instead of just looking to see whether or not the file exists.
# 3  
Old 05-08-2018
Dear Don,

Thanks for you precious time.
The command is perfectly running and I am getting exact file details if the pattern being searched is matched. The script gives me the correct file details which were generated.
The only concern is that if the patter is not present in the backup directory I am searching, the gzgrep command fails to yield a result which is going to ls -lrt command. This is turn is executing a blank ls -lrt command and it ends up giving me all the files present in the directory.
All I want is to avoid the situation when the pattern present in the file is not found, I need the message as file not found and graceful exit from the script.

Regards
Gaurav Kumar
# 4  
Old 05-08-2018
A few comments on top of what Don Cragun already said:
- The if construct operates on the gzgrep result ONLY if the HOME variable points to the /home directory.
- It will NOT "send" (i.e. print to screen) a time along with message and file name.
- Appending to the output file might obfuscate the result with data from past runs.
# 5  
Old 05-08-2018
In order to operate the way I think I infer from your post#3, the gzgrep "command substitution" must provide the parameters to the ls -lrt command (why the -rt option, BTW?) being run either on the same command line OR separated by a line continuation (i.e. a \<new line> combination).
# 6  
Old 05-10-2018
Dear RudiC,

The command used is in a single line continuation and not in new line.

Code:
 ls -lrt `gzgrep -il "1234566789" 2018-05-07*ship*` >> $HOME/fileback/var2.txt

When I search for pattern 123456789 and is found, it sends the filename
and timestamp of file creation to file var2.txt. This is working absolutely fine,
however when the pattern is not found, it sends a blank statement to ls -lrt command which in turn runs a blank ls -lrt command and sends all the file details to var2.txt folder.
All I want is to skip the 2nd part if a file is not found and send me a message as file is not found

Regards
Gaurav Kumar
# 7  
Old 05-10-2018
Quote:
Originally Posted by grvk101
Dear RudiC,

The command used is in a single line continuation and not in new line.
Why, then, don't you show it accordingly in post#1? We again can see that careless posting leads to confusion and in vain efforts in subsequent postings.

Quote:
When I search for pattern 123456789 and is found, it sends the filename
and timestamp of file creation to file var2.txt. This is working absolutely fine,
Difficult to believe. man gzgrep:
Quote:
DESCRIPTION
Gzgrep is used to invoke the grep on compress'ed or gzip'ed files. All options specified are passed directly to grep.
man grep:
Quote:
-l, --files-with-matches
Suppress normal output; instead print the name of each input file
No timestamp mentioned . . .


Quote:
however when the pattern is not found, it sends a blank statement to ls -lrt command which in turn runs a blank ls -lrt command and sends all the file details to var2.txt folder.
All I want is to skip the 2nd part if a file is not found and send me a message as file is not found
So you need a different approach, e.g. running the gzgrep statement independently, evaluate its result, and then run the ls -l command with an adequate parameter list.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

2. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

3. Windows & DOS: Issues & Discussions

Script that, if file exists in Samba share, moves file to Unix server

I'm looking to do pretty much what the title says. I want a script that runs, it can run on Unix or Windows, doesn't matter, and searches a Samba shares for a .txt file. If the file exists, the script will move (or possibly copy) the file from the Samba share into a directory on our Unix... (3 Replies)
Discussion started by: twcostello
3 Replies

4. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

5. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

6. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

7. Shell Programming and Scripting

file exists

hello, I want to know if a file exists, but do not know contador = 0 while(file$contador exists) do bla bla bla contador=$(($contador+1)) done how can i do it? thanks (4 Replies)
Discussion started by: uri_crack
4 Replies

8. Shell Programming and Scripting

File Exists

Hi, I have scenerio where i need to check 3 files whether they are of current day files and then count should not be equal to zero. how to achieve the same please let me know. Vishal (17 Replies)
Discussion started by: shady4u
17 Replies

9. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question