Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Compound Conditional, With Wildcards and File Existence Check

I've mastered the basics of Bash compound conditionals and have read a few different ways to check for file existence of a wildcard file, but this one is eluding me, so I figured I'd ask for help...

I need to: 1.) Check if some file matching a pattern exists AND 2.) Check that text in a different file exists.

I know there's lots of ways to do this, but I don't really have the knowledge to prioritize them (if you have that knowledge I'd be interested in reading about that as well).

First things that came to mind is to use find for #1 and grep for #2

So something like

if [ `grep -q "OUTPUT FILE AT STEP 1000" ../log/minimize.log` ] \
      && [ `find -name "jobscript_minim\*cmd\*o\*"` ]; then
   echo "Both passed! (1)"
fi

That fails, though curiously:

if `grep -q "OUTPUT FILE AT STEP 1000" ../log/minimize.log` ;then
   echo "Text passed!"
fi
if `find -name "jobscript_minim\*cmd\*o\*"` ;then
   echo "File passed!"
fi

both pass...

I've done a bit of reading and have seen people talking about the problem of multiple filenames matching wildcards within an if statement. What's the best solution to this? (in answer my question, I'd assumed you take a crack at that question, as well, in the process)

Any ideas/solutions/suggestions?

like image 472
Jason R. Mick Avatar asked Aug 24 '10 21:08

Jason R. Mick


1 Answers

Let's tackle why your attempt failed first:

if [ `grep -q …` ]; 

This runs the grep command between backticks, and interpolates the output inside the conditional command. Since grep -q doesn't produce any output, it's as if you wrote if [ ];

The conditional is supposed to test the return code of grep, not anything about its output. Therefore it should be simply written as

if grep -q …;

The find command returns 0 (i.e. true) even if it finds nothing, so this technique won't work. What will work is testing whether its output is empty, by collecting its output any comparing it to the empty string:

if [ "$(find …)" != "" ];

(An equivalent test is if [ -n "$(find …)" ].)

Notice two things here:

  • I used $(…) rather than backticks. They're equivalent, except that backticks require strange quoting inside them (especially if you try to nest them), whereas $(…) is simple and reliable. Just use $(…) and forget about backticks (except that you need to write \` inside double quotes).

  • There are double quotes around $(…). This is really important. Without the quotes, the shell would break the output of the find command into words. If find prints, say, two lines dir/file and dir/otherfile, we want if [ "dir/file dir/otherfile" = "" ]; to be executed, not if [ dir/file dir/otherfile = "" ]; which is a syntax error. This is a general rule of shell programming: always put double quotes around a variable or command substitution. (A variable substitution is $foo or ${foo}; a command substitution is $(command).)


Now let's see your requirements.

  1. Check if some file matching a pattern exists

    If you're looking for files in the current directory or in any directory below it recursively, then find -name "PATTERN" is right. However, if the directory tree can get large, it's inefficient, because it can spend a lot of time printing all the matches when we only care about one. An easy optimization is to only retain the first line by piping into head -n 1; find will stop searching once it realizes that head is no longer interested in what it has to say.

    if [ "$(find -name "jobscript_minimcmdo" | head -n 1)" != "" ];

    (Note that the double quotes already protect the wildcards from expansion.)

    If you're only looking for files in the current directory, assuming you have GNU find (which is the case on Linux, Cygwin and Gnuwin32), a simple solution is to tell it not to recurse deeper than the current directory.

    if [ "$(find -maxdepth 1 -name "jobscript_minim*cmd*o*")" != "" ];

    There are other solutions that are more portable, but they're more complicated to write.

  2. Check that text in a different file exists.

    You've already got a correct grep command. Note that if you want to search for a literal string, you should use grep -F; if you're looking for a regexp, grep -E has a saner syntax than plain grep.

Putting it all together:

if grep -q -F "OUTPUT FILE AT STEP 1000" ../log/minimize.log &&
   [ "$(find -name "jobscript_minim*cmd*o*")" != "" ]; then
  echo "Both passed! (1)"
fi
like image 63
Gilles 'SO- stop being evil' Avatar answered Nov 02 '22 09:11

Gilles 'SO- stop being evil'