3

Thanks in advance for any advice

I'm working on an fswatch command to launch a script when files hit the Downloads folder.

fswatch --event Created Downloads/ | (while read x; do echo $x | xargs -0 bash ./dlsort.sh; done)

The script is working as intended. The issue I'm having is (I expect) coming from the (while read x; bit:

The problem is that dlsort.sh is getting called twice for each download. Since the first run of the script moves the file, I'm getting a "No such file or directory" error on the second run. It's not a big deal, since the file makes it where it's supposed to go, but I'm mostly working on this project to teach myself more about bash/scripting, so I'd like to figure out what I'm misunderstanding about this command.

EDIT: adding dlsort.sh

if (echo "$1" | grep -q '\.jpg$'); then
  mv "$1" Desktop/targdir/
else
  mv "$1" Downloads/
fi
  • Think it's got something to do with STDOUT getting picked up by the read command, and trying to get reprocessed. Not sure how to fix that, but working on figuring that out – Curtis Everingham Dec 09 '15 at 22:08
  • I don't understand why you need both xargs and a while...read loop: **either** read and process each filename in turn **or** pipe the whole thing to xargs. Based on your [previous question](http://askubuntu.com/questions/707839/troubles-piping-echo-output-to-xargs-script-for-mv-command/707903#707903) your script only accepts a single argument so if you **do** use xargs you probably need `-n 1` – steeldriver Dec 09 '15 at 22:13
  • Without a while loop, the command exits after one file gets caught (I want to continuously monitor the directory and apply the script to each file that hits it). As for xargs... I'm not sure, so I'll see what I can do without it – Curtis Everingham Dec 09 '15 at 22:16
  • Well, the [fswatch wiki](https://github.com/emcrisostomo/fswatch/wiki/How-to-Use-fswatch) seems to suggest something like `fswatch -0 --event Created Downloads/ | xargs -0 -n 1 ./dlsort.sh` should work – steeldriver Dec 09 '15 at 22:21
  • Sure enough... That works continuously, where I thought it was exiting after one iteration. I'm still getting the issue with the script getting called once (which works), and then a second time which throws a "no such file or directory" error, since the first call moved the file out of the watched directory. – Curtis Everingham Dec 09 '15 at 22:39
  • Hmm... do you actually see each filename echoed twice if you pipe to xargs with no script/command i.e. `fswatch -0 --event Created Downloads/ | xargs -0 -n 1`? If so, I expect it's a "feature" of fswatch – steeldriver Dec 09 '15 at 22:59
  • No, only gets echoed once. Just the command seems to work as expected, and manually running the script on its own works just fine (by manually adding the needed argument), but combining the two is giving this weird problem. Cannot for the life of me figure out exactly whats going on/how to fix it – Curtis Everingham Dec 09 '15 at 23:08
  • Please [edit] your question to include the contents of your dlsort.sh script – steeldriver Dec 09 '15 at 23:10
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/32806/discussion-between-zpawwn-and-steeldriver). – Curtis Everingham Dec 10 '15 at 15:45
  • For me, your solution seems to work. The script is only called once. I guess maybe the software you use for the download does something unusual or there is a bug in the implementation of the event system. – jottbe Jun 27 '19 at 08:56
  • Did you guys ever figure this out? I'm trying to use fswatch and either xargs or a while loop and the resulting behavior is unexpected. I never see any output from the command I supply to either xargs or the while loop. All I see is the output of fswatch as if I wasn't running another command. The verbatim examples on the fswatch doc don't seem to work for me either. Does STDOUT get closed for the following command? It would be nice if I could see a working example with output as a sanity check. – hepcat72 Jun 20 '21 at 05:31

0 Answers0