2

and thank you in advance for any advice.

I'm working on a script that will automatically sort my downloads. The script is very simple, and running it manually has the intended effect. The problem I'm having is with the fswatch command that's intended to trigger the script - I can't get the output from echo correctly piped into xargs. Here's the command:

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

and the script:

if (echo "$1" | grep -q '\.jpg$'); then
  mv "$1" Desktop/images/ &
else
  mv "$1" Downloads/other/ &
fi

I've determined what the issue is, I just don't know how to correct it. a newline character is getting put at the end of the filename, which causes the mv command to fail with a "no such file or directory" error in the script. I've been experimenting with quite a few variations in both the echo and fswatch commands, but nothing I've tried has worked.

Thanks again to anyone who can help with this

5
  • did you try echo -n $x?
    – Lety
    Dec 9, 2015 at 18:12
  • I did, when I do that I only get mv: (name of the file): no such file or directory (it doesn't print any destination folder). Not sure why that is either, but that could definitely be a part of the solution Dec 9, 2015 at 18:29
  • Well that's odd... I went to clear out the directories that this script is sorting into, and some files actually have made it (when using echo -n). I still get an error message, but it looks like the file is getting moved where it's supposed to. That works, but I would like to figure out why I'm still getting a mv: no such file or directory message Dec 9, 2015 at 18:38
  • The script is getting called twice... that's why. Working on figuring out why that is. Thanks for getting me back on the right track, @Lety :) Dec 9, 2015 at 18:54
  • reading and echoing within a while loop seems superfluous here, to me. Why don't you either pipe the (null-delimited) fswatch command straight to xargs -0 or read each null-delimited argument in a while loop and call your script on it? Dec 9, 2015 at 20:03

1 Answer 1

1

Figured out a good answer to this specific problem:

Use the fswatch option --format="%p%0". This makes sure that the null character is at the end of the path name that gets handed off to xargs, which is what it's looking for with the -0 option enabled.

1
  • You seem to subvert this by reading and echoing the result (which will split on whitespace I think?) Dec 9, 2015 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .