1

I'm trying to get a simple script going which looks at all .log files and greps for a particular string and depending on its value sends an email.

tail -f **/*.log | while read LOGLINE

do 
        [[ "${LOGLINE}" == *"complete!"* ]] && pkill -P $$ tail | 
                echo "LOG" | mutt -s "Test Passed!" abc@xyz.com

        [[ "${LOGLINE}" == *"FAILURE"* ]] && pkill -P $$ tail | 
                echo "LOG" | mutt -s "Test Failed!" abc@xyz.com

done

However this gives me the following results:

tail: cannot open `**/*.log' for reading: No such file or directory
tail: no files remaining

If I provide the exact path, the script works fine. How can I make this work for all the .log files in the parent and all sub-folders?

Electrix
  • 510
  • 3
  • 14
  • What are you trying to do with `**`? – e0k Jan 23 '16 at 01:49
  • @e0k Trying to follow the example given here: http://stackoverflow.com/questions/18321336/how-to-tail-all-the-log-files-inside-a-folder-and-subfolders where they suggested "**/*.log" – Electrix Jan 23 '16 at 01:51
  • OK, `**/` is from the [globstar](https://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching) option and matches only directories and subdirectories. Is the `globstar` option enabled in the shell? Are the `.log` files in the current directory or subdirectories? – e0k Jan 23 '16 at 02:03

1 Answers1

1

In my bash shell, the globstar option is off by default. You can enable it with

    shopt -s globstar

This should allow expansion of **/*.log into all subdirectories. See also this article. See if this finds your .log files.

Note: This feature was introduced in bash 4.0. If you're using an older version, this would explain why the code doesn't work (your shell doesn't have that feature).

An alternative is to use find. Replace the **/*.log with $(find . -name '*.log') so the line reads

    tail -f $(find . -name '*.log') | while read LOGLINE

In case you don't have permissions of some subdirectories, for example, it could give error messages too which go to stderr (file 2). You can remove them with a redirection

    $(find . -name '*.log' 2>/dev/null)
e0k
  • 6,961
  • 2
  • 23
  • 30
  • Nope, I get an invalid option name $shopt -s globstar bash: shopt: globstar: invalid shell option name (GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)) – Electrix Jan 23 '16 at 02:56
  • What happens if you type only `shopt` at a bash prompt? It should list every shell option and if it's on or off. Look for `globstar`. – e0k Jan 23 '16 at 03:00
  • 1
    Oh I see the problem now, you're using an old version of bash. This `globstar` option was introduced in 4.0 (see the link on my answer). So you can't use that `**/*.log` syntax as it was intended. – e0k Jan 23 '16 at 03:02
  • BTW, I _strongly_ recommend upgrading your bash due to the [shellshock](https://en.wikipedia.org/wiki/Shellshock_%28software_bug%29) bug, which is a security issue. – e0k Jan 23 '16 at 03:04
  • Yes, it was added in 4.0 as you mentioned. Alas I dont have rights to upgrade it - its tightly controlled by IT so I'll put in a request. Is there another way around this? Using find to recursively look at all .log files and use those to look for the pattern? Appreciate your suggestions. – Electrix Jan 23 '16 at 03:07