0

I try to do a simple rsync command and to exclude directories that have a flagged file in it. The flagged flagged.txt is a text file contains only *. This command below run fine in terminal:

rsync -avP --delete --filter="dir-merge,n- flagged.txt" \
    "/mnt/c/Documents/somefolder" "/mnt/j/"

However when I put in in bash script, the flagged is ignored. Tried

rsync -avP --delete --filter='dir-merge,n- flagged.txt' \
    "/mnt/c/Documents/somefolder" "/mnt/j/"

and

filter=(--filter="dir-merge,n- flagged.txt")
rsync -avP --delete "${filter[@]}" "/mnt/c/Documents/somefolder" "/mnt/j/"

and

filter=(--filter='dir-merge,n- flagged.txt')
rsync -avP --delete "${filter[@]}" "/mnt/c/Documents/somefolder" "/mnt/j/"

All failed. Any idea? Thanks in advance!

pynexj
  • 19,215
  • 5
  • 38
  • 56
Gregor Isack
  • 1,111
  • 12
  • 25
  • Do you have an alias or something like that for `rsync`? If you run `type rsync` in your interactive shell and in the script, do you get the same result? Also, run `set -x` before it in each situation and compare the output from those. – Gordon Davisson Jul 14 '19 at 18:43
  • You can also try using `\rsync` in your interactive shell to turn off any alias you might have, and see if it still works. There are several differences between a script and an interactive shell: different dot files get loaded, process monitoring is not on in a script by default, aliases are not on by default, among others. – joanis Jul 14 '19 at 19:21
  • There is no rsyncd daemon involved here? –  Jul 14 '19 at 21:59
  • You might want to call rsync with its full path, which is `/usr/bin/rsync` typically. –  Jul 14 '19 at 22:00
  • How did it fail? Any error messages? –  Jul 15 '19 at 02:10
  • @GordonDavisson I didn't have any alias for rsync, however trying with your diagnostic steps, it's weird 'cause `type rsync` gave me the full path, however when I put it in script it gave me an error `not foundwares/rsync_script/rsync.sh: line 2: type: rsync`. and when I put `#!/bin/bash` in the beginning of the script, it gave me another error `/mnt/d/rsync_script/rsync.sh: line 2: $'\r': command not found`. This is the command I put in terminal: `/bin/bash /mnt/d/rsync_script/rsync.sh` – Gregor Isack Jul 15 '19 at 04:15
  • That sounds like your shell script has DOS/Windows line endings, which need to be fixed. See: [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Jul 15 '19 at 04:21
  • I totally forgot that I'm on Windows! However, fixed the endings but I still can't fully make it function as it should. That means all the files will still be copied over even if there's a `flagged.txt`, it's as though as rsync totally ignore that parameter (works fine in terminal). – Gregor Isack Jul 15 '19 at 04:29
  • Any remaining differences in `type rsync` and `set -x` output? – Gordon Davisson Jul 15 '19 at 06:29
  • Fixed it. It seems like the encoding is the culprit here. I thought I fixed before but it's not completely. I should close this question since it's not really a wsl issue. Thanks Gordon. – Gregor Isack Jul 16 '19 at 11:55
  • @GregorIsack I've been following this question and now I'm curious, what exactly did you change to fix the encoding? Don't leave us hanging with "I fixed it" without telling us how... – joanis Jul 16 '19 at 12:47
  • 1
    @joanis My bad. Since my bash script was created in Windows environment using Notepad++, the end of line (EOL) of the script was in Windows format, thus when I try to run the script, error such as `/mnt/d/rsync_script/rsync.sh: line 2: $'\r': command not found` occurred and rsync command can't run as it should. So one way to fix this is to [convert the EOL to Unix base](http://robinsnippet.blogspot.com/2017/06/convert-windowss-end-of-line-characters.html), which pretty much solved the issue. – Gregor Isack Jul 16 '19 at 14:52
  • I should use nano and such to create the script in WSL environment the first place, but I'm too dependent on the GUI program and ease of use of Notepad++. – Gregor Isack Jul 16 '19 at 14:52
  • @GregorIsack Thank, that's good to know, and an error I've seen many times. I should have clued in when you posted your comment with that error yesterday, actually, since I got it just last week on my own bash script. – joanis Jul 16 '19 at 16:15

0 Answers0