0

I am trying to pipe the output of fswatch to several commands in a shell script with following technique:

$ fswatch -0 [opts] [paths] | xargs -0 -n 1 -I {} [command]

Instead of [command] I put the shell script path. Here is my command line:

fswatch -0 -Ie ".*\.*$" -i ".*\.mp4$" ~/Desktop/encoding\ tests | xargs -0 -n 1 -I {} ~/Desktop/s3cmd.sh

The script is following:

#!/bin/sh 
terminal-notifier -message "s3cmd Upload {}" ; 
s3cmd sync --acl-public -m video/mp4 --add-header=Cache-Control:public,max-age=2052000 {} s3://saltanat-test/ && 
terminal-notifier -message "s3cmd Upload of {} done"

Sorry I am not experienced with shell scripting. How can I pipe the the fswatch output into the script?

Thank you.

saltarob
  • 1
  • 3
  • the `{}` will not be recognized in a shell script, but you can replace them with `${@}` or more narrowly `$1`. I haven't had reason to use that in a while, but it should work. Good luck. – shellter Sep 08 '16 at 03:02
  • Unfortunately it does not work. Neither with `${@}` nor with `$1`. But thank you for your answer. I was thinking maybe the read command could help: `read {}`. But also no success. – saltarob Sep 09 '16 at 21:32
  • So I mean `#!/bin/sh ; terminal-notifier -message "s3cmd Upload ${@}" ; ...${@} ...; .... ${@}... etc` OR `...terminal-notifier -message "s3cmd Upload ${1}"; ... etc` . Good luck. – shellter Sep 09 '16 at 22:33
  • Yes I tried it like this, but I was not successful, sorry. – saltarob Sep 10 '16 at 23:59
  • I would try `fswatch .... | xargs ..... echo {}` to see what is being processed by `xargs`. Maybe that will give you a clue. Sorry, I don't have access to a system w fswatch, so I can't be more specific. Good luck. – shellter Sep 11 '16 at 00:06
  • Thank you. The output of xargs is the path of the modified or added file, as expected. If I put just one command behind xargs it works fine. But if I put a second command the second command is not receiving the xargs output similar to the script. – saltarob Sep 15 '16 at 01:10
  • Maybe you need `....xargs -0 -n 1 -I {} ~/Desktop/s3cmd.sh {}` ? Good luck. – shellter Sep 15 '16 at 01:31
  • Thank you ;-) Still not working ... `$ fswatch -0 -Ie ".*\.*$" -i ".*\.mp4$" ~/Desktop/encoding\ tests | xargs -0 -n 1 -I {} ~/Desktop/s3cmd.sh {} ERROR: Parameter problem: Invalid source: '{}' is not an existing file or directory ERROR: Parameter problem: Invalid source: '{}' is not an existing file or directory` – saltarob Sep 18 '16 at 21:19

2 Answers2

1

For anyone in the future with this question :

fswatch command:

fswatch -0 /path/to/watch | xargs -0 -n 1 -I {} ~/yourfile.sh {}

.sh file :

#!/bin/bash
echo $1

Now $1 will be equal to the output of fswatch, which is the full path to the file that was changed.

efru
  • 1,401
  • 3
  • 17
  • 20
0
#!/bin/bash
fswatch -0 -x --event Created --event Updated --event Renamed -Ie '.*\.*$' -i '.*\.mp4$' ~/Desktop/encoding\ tests \
     | xargs -0 -n 1 ~/Desktop/s3cmd-3.sh

This works. Thanks.

saltarob
  • 1
  • 3