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.


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

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.

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

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.

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

Dear RudiC,

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

 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

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.

Difficult to believe. man gzgrep :

man grep :

No timestamp mentioned . . .

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.

Dear RudiC,

The pattern is being searched in directory where in all the files are compressed hence the command gzgrep.

I will try a different approach to my script to obtain better result for 2nd part of my requirement. Thanks a ton for your precious time.

Regards
Gaurav Kumar