1

I'm using fswatch to keep track of changes to a directory, but would like this process to stop if a certain file exists (with a wildcard). This certain file is created in an alternative directory (not in the tracked directory) by another process (which is generating changes that need to be tracked).

Here's what I tried to do:

while [[ $(shopt -s nullglob; set -- "${file_to_check}"; echo $#) -eq 1 ]]; do
   fswatch "${path_to_the_tracked_directory}"
done && echo "Done"

However, this script does not terminate after ${file_to_check} appears.

The complex bit in the condition is to take care of wildcards as per: Bash check if file exists with double bracket test and wildcards

EDIT:

The complex bit can be simplified to:

while [ $(set -- "${path_to_the_file_to_check}"${file_to_check_with_wildcards}; echo $#) -eq 0 ]; do
   fswatch "${path_to_the_tracked_directory}"
done && echo "Done"
Community
  • 1
  • 1
econ
  • 547
  • 7
  • 22
  • You have transcribed the code incorrectly. There needs to be a space after the `set --` – tripleee Apr 19 '16 at 06:53
  • Also, double-quoting the pattern means you might as well simply use `while ! [ -f "$file_to_check" ]` -- the code you linkes to is for checking whether a wildcard matches exactly one file, as opposed to zero or several. With a single static file name and no wildcard, that's obviously serious overkill. – tripleee Apr 19 '16 at 06:54
  • @tripleee: Thanks, actually my first try was `while [ ! -f "${file_to_check}" ]`, but it doesn't seem to work with wildcards. – econ Apr 19 '16 at 07:02
  • If you want wildcards in `$file_to_check` you cannot use double quotes around it. – tripleee Apr 19 '16 at 07:02
  • @tripleee: Thanks, but my path contains spaces. If I put double quotes only around the part of the path that contains spaces, then I get `too many arguments` error (I guess due to multiple matches). – econ Apr 19 '16 at 07:06
  • 1
    @econ: How are you detecting the file changes with `fswatch`, because the man page (http://emcrisostomo.github.io/fswatch/doc/1.5.0/html/fswatch/Tutorial-Introduction-to-fswatch.html) suggests using an option `fswatch -o ` Refer this page http://stackoverflow.com/a/24574680/5291015 for more details. – Inian Apr 19 '16 at 07:17
  • As a workaround, perhaps split up the pattern into a wildcard part and a static part, and use `set -- $wildcard_part"$static_part"` -- I could find no sane way to quote a variable so that it can be evaluated without quotes and still contain some escaped metacharacters. – tripleee Apr 19 '16 at 07:23
  • @Inian: Thanks, I'm actually using the same syntax as in MWE, just substituting the path of the directory. – econ Apr 19 '16 at 07:26
  • @tripleee: Thank you, this simplifies the conditional, but `fswatch` still continues after `${file_to_check}` appears. – econ Apr 19 '16 at 07:36
  • I found one possible solution, but I'm wary that there might be problems with this approach (or at least some inefficiencies), so not going to accept this as an answer just yet. – econ Apr 19 '16 at 07:46

1 Answers1

0

One solution is to use -1/--one-event option (https://github.com/emcrisostomo/fswatch/wiki/How-to-Use-fswatch).

The code then looks as:

while [ $(set -- "${path_to_the_file_to_check}"${file_to_check_with_wildcards}; echo $#) -eq 0 ]; do
      fswatch -1 "${path_to_the_tracked_directory}"
done && echo "Done"
econ
  • 547
  • 7
  • 22