Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 30

Thread: Bash: How can I pass a wildcard as a parameter within a script?

  1. #11
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    There is one approach I use sometimes to test rsync syntax.
    I first run grsync (GUI) session and do a test (dry) run.
    When it works you can then grab the validated command from
    File > Rsync Command Line.

  2. #12
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,784
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    Quote Originally Posted by dragonfly41 View Post
    There is one approach I use sometimes to test rsync syntax.
    I first run grsync (GUI) session and do a test (dry) run.
    When it works you can then grab the validated command from
    File > Rsync Command Line.
    That's a good tip, thanks, but my rsync has been thoroughly tested, and I know that it works.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  3. #13
    Join Date
    Apr 2008
    Beans
    71

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    I think this is probably an example of BashFAQ/050:

    https://mywiki.wooledge.org/BashFAQ/050

  4. #14
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,784
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    Quote Originally Posted by philhughes View Post
    I think this is probably an example of BashFAQ/050:
    https://mywiki.wooledge.org/BashFAQ/050
    That makes interesting reading, thank you!

    I'm going to print it out and experiment.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  5. #15
    Join Date
    Apr 2008
    Beans
    71

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    I think that effectively you are telling rsync to exclude a file literally called '/*', and since you probably don't have one, the effect is it is not trying to exclude anything. If you leave out the single quotes, in my testing it works, but that might cause problems with more complex patterns.

  6. #16
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,784
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    Quote Originally Posted by philhughes View Post
    I think that effectively you are telling rsync to exclude a file literally called '/*', and since you probably don't have one, the effect is it is not trying to exclude anything. If you leave out the single quotes, in my testing it works, but that might cause problems with more complex patterns.
    No, you misunderstand. It's not a file literally called '/*'.

    This is a parameter that's passed to rsync, which (in my case) passes it to the remote server. The remote server expands it there.

    Expanding it on my computer will give entirely the wrong results.

    That's why the parameter must be passed, unexpanded, to rsync.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  7. #17
    Join Date
    Apr 2008
    Beans
    71

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    Yes, I know, but qouting from the BashFAQ: "...because the single quotes inside the variable are literal, not syntactical."

    So what I mean is that when the parameter is passed to rsync, rsync sees the single quotation marks literally as part of name of the file to be excluded

  8. #18
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,784
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    Quote Originally Posted by philhughes View Post
    … when the parameter is passed to rsync, rsync sees the single quotation marks literally as part of name of the file to be excluded
    That would indeed happen if I were to quote the variable:
    Code:
    rsync ... "${EXCLUDE}" ...
    But I don't quote the variable:
    Code:
    rsync ... ${EXCLUDE} ...
    Anyway, I did test both with and without. The results are identical:
    Code:
    EXCLUDE="--exclude='/*'"
    EXCLUDE="--exclude=/*"
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  9. #19
    Join Date
    Apr 2008
    Beans
    71

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    You are quoting the variable when you set it.

    When using:
    Code:
    rsync -av ${EXCLUDE} ...
    The behaviour I see is:

    EXCLUDE=--exclude=/* - files are excluded as expected
    EXCLUDE=--exclude='/*' - files are excluded as expected
    EXCLUDE="--exclude=/*" - files are excluded as expected
    EXCLUDE="--exclude='/*'" - files are not excluded

    Obviously I am not replicating your exact set up, so I don't know why we are seeing different behaviour. It would be interesting to ask on stackoverflow!

  10. #20
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,784
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: Bash: How can I pass a wildcard as a parameter within a script?

    Quote Originally Posted by philhughes View Post
    EXCLUDE=--exclude=/* - files are excluded as expected
    EXCLUDE=--exclude='/*' - files are excluded as expected
    EXCLUDE="--exclude=/*" - files are excluded as expected
    That's what I expected, but don't get.
    Quote Originally Posted by philhughes View Post
    EXCLUDE="--exclude='/*'" - files are not excluded
    I am baffled as to why that's different for you, but not for me!

    I wonder if there's some Bash setting that you and I have different?
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •