Page 3 of 3 FirstFirst 123
Results 21 to 30 of 30

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

  1. #21
    Join Date
    Apr 2008
    Beans
    71

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

    No idea. There's a load of settings loaded from who knows where that may or may not have an effect, but I've included the output of "shopt" below.

    Code:
    autocd         	off
    assoc_expand_once	off
    cdable_vars    	off
    cdspell        	off
    checkhash      	off
    checkjobs      	off
    checkwinsize   	on
    cmdhist        	on
    compat31       	off
    compat32       	off
    compat40       	off
    compat41       	off
    compat42       	off
    compat43       	off
    compat44       	off
    complete_fullquote	on
    direxpand      	off
    dirspell       	off
    dotglob        	off
    execfail       	off
    expand_aliases 	on
    extdebug       	off
    extglob        	on
    extquote       	on
    failglob       	off
    force_fignore  	on
    globasciiranges	on
    globstar       	off
    gnu_errfmt     	off
    histappend     	on
    histreedit     	off
    histverify     	off
    hostcomplete   	off
    huponexit      	off
    inherit_errexit	off
    interactive_comments	on
    lastpipe       	off
    lithist        	off
    localvar_inherit	off
    localvar_unset 	off
    login_shell    	off
    mailwarn       	off
    no_empty_cmd_completion	off
    nocaseglob     	off
    nocasematch    	off
    nullglob       	off
    progcomp       	on
    progcomp_alias 	off
    promptvars     	on
    restricted_shell	off
    shift_verbose  	off
    sourcepath     	on
    xpg_echo       	off

  2. #22
    Join Date
    Dec 2009
    Beans
    192

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

    You could pass the EXCLUDE variable to the rsync command by using bash arrays for the EXCLUDE string value. Array values do not suffer from the shell's unwanted expansions. Something like:

    EXCLUDE=( /* )

    and referencing it in the rsync script with ${EXCLUDE[0]} for the first element.

    rsync ... "${EXCLUDE[0]}" ...


    Take a look at this:

    https://unix.stackexchange.com/quest...-and-wildcards

  3. #23
    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
    No idea. There's a load of settings loaded from who knows where that may or may not have an effect, but I've included the output of "shopt" below.
    Sorry for the late reply — Ubuntu Forums frequently doesn't send me notifications.

    My shopt and yours are identical, so it remains a mystery!
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  4. #24
    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 erind View Post
    You could pass the EXCLUDE variable to the rsync command by using bash arrays
    I've already tried this, but it doesn't work.

    I've found that whenever I include wildcards or quotation marks, it causes failure. I have no clue why, though.

    I ended up using the format that the link from @philhughes gave.
    Code:
    if some_condition; then EXCLUDE='/*'; else EXCLUDE=''; fi
    rsync ... ${EXCLUDE:+--exclude="${EXCLUDE}"} ...
    This is better than my previous workaround, because it eliminates the need for a temporary file. Why does this work and not any of the other suggestions? I don't know
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  5. #25
    Join Date
    Dec 2009
    Beans
    192

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

    Quote Originally Posted by Paddy Landau View Post
    I've already tried this, but it doesn't work.

    I've found that whenever I include wildcards or quotation marks, it causes failure. I have no clue why, though.

    [...]
    It should work. Arrays are the conventional way to pass wildcards to the scripts in these special cases. One advice though, when you declare the arrays you don't quote the wildcards, only the strings with whitespaces in them.

    In other similar cases I used to use eval too and it would work great. But nowadays eval has some security issues and that's why it's deprecated in favor of the arrays, which are considered superior.

    However, I'm glad you found a solution.

  6. #26
    Join Date
    Jun 2018
    Beans
    163

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

    Are you 100% sure that you condition works as it should?

    Code:
    EXCLUDE=""
    if [ some_condition ]; then EXCLUDE="--exclude=/*"; fi
    # Debug print to verify condition
    print ${EXCLUDE}
    # Proceed...
    Have a ubuntastic day!

  7. #27
    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 erind View Post
    It should work.
    What can I say? It doesn't!
    Quote Originally Posted by erind View Post
    … when you declare the arrays you don't quote the wildcards, only the strings with whitespaces in them.
    I tried both with and without the quotation marks.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  8. #28
    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 dinkidonk View Post
    Are you 100% sure that you condition works as it should?
    Yes, I have tested it thoroughly.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  9. #29
    Join Date
    Dec 2009
    Beans
    192

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

    Quote Originally Posted by Paddy Landau View Post
    What can I say? It doesn't!

    I tried both with and without the quotation marks.
    It works fine on my end. Here it is a proof of concept case:

    Code:
    $ ll
    total 24
    drwxrwxr-x 2 miri miri 4096 Jul 10 20:12  d1
    drwxrwxr-x 2 miri miri 4096 Jul 10 20:35  d2
    drwxrwxr-x 2 miri miri 4096 Jul 10 20:28 'dir 1'
    drwxrwxr-x 3 miri miri 4096 Jul 10 20:31 'dir 2'
    -rwxr-x--- 1 miri miri  190 Jul 10 20:30  rs
    -rwxr-x--- 1 miri miri  211 Jul 10 20:31  rs_1
    miri@miri-IdeaPad-3:~/Temporary$ 
    miri@miri-IdeaPad-3:~/Temporary$ 
    miri@miri-IdeaPad-3:~/Temporary$ ll d1
    total 0
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_1
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_2
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_3
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_4
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_5
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 b_1
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 b_2
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 b_3
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 b_4
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 b_5
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 c_1
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 c_2
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 c_3
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 c_4
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 c_5
    miri@miri-IdeaPad-3:~/Temporary$ 
    miri@miri-IdeaPad-3:~/Temporary$ ll d2
    total 0
    miri@miri-IdeaPad-3:~/Temporary$
    Script below:

    Code:
    #!/bin/bash
    set -x
    
    if true 
    then
     EXCLUDE=( --exclude=d1/b_* --exclude=d1/c_* ) 
    else 
     EXCLUDE='' 
    fi
    
    rsync -avz "${EXCLUDE[@]}" d1 d2
    
    ## Or with the array values spelled out:
    # rsync -avz "${EXCLUDE[0]}" "${EXCLUDE[1]}" d1 d2
    After running the above script:

    Code:
    $ ./rs
    + true
    + EXCLUDE=(--exclude=d1/b_* --exclude=d1/c_*)
    + rsync -avz '--exclude=d1/b_*' '--exclude=d1/c_*' d1 d2
    sending incremental file list
    d1/
    d1/a_1
    d1/a_2
    d1/a_3
    d1/a_4
    d1/a_5
    
    sent 324 bytes  received 115 bytes  878.00 bytes/sec
    total size is 0  speedup is 0.00
    miri@miri-IdeaPad-3:~/Temporary$ 
    miri@miri-IdeaPad-3:~/Temporary$ ll d2/d1/
    total 0
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_1
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_2
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_3
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_4
    -rw-rw-r-- 1 miri miri 0 Jul 10 20:08 a_5
    miri@miri-IdeaPad-3:~/Temporary$
    If the directories' names contain whitespaces quote them like so:

    Code:
    #!/bin/bash
    set -x
    
    if true 
    then
     EXCLUDE=( --exclude="dir 1"/b_* --exclude="dir 1"/c_* ) 
    else 
     EXCLUDE='' 
    fi
    
    rsync -avz "${EXCLUDE[@]}" "dir 1" "dir 2" 
    This is just a simple test to prove that arrays are the way to go. For your needs modify the rsync and EXCLUDE command in the script.

  10. #30
    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 erind View Post
    It works fine on my end. Here it is a proof of concept case:
    I believe you. It just doesn't work for me. I've done exactly as you describe, but it doesn't work.

    Somehow, we must have different settings somewhere.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

Page 3 of 3 FirstFirst 123

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
  •