Search code examples
tclrsync

rsync error: when using source which contain "*" character


In my TCL script I use rsync command and unfortunately it is refusing to sync path contain "*" character.

Code Example:

 set srs "/users/home/username/common/*"
 catch {exec  rsync -av $src /tmp} res
 puts $res

Output:

 building file list ... done

 sent 29 bytes  received 20 bytes  98.00 bytes/sec
 total size is 0  speedup is 0.00
 rsync: link_stat "/users/home/username/common/*" failed: No such file or directory (2)
 rsync error: some files could not be transferred (code 23) at main.c(977) [sender=2.6.9]
    while executing
  "exec  rsync -rLptgov  $src /tmp "

Solution

  • The canonical way of writing that is:

    set srs "/users/home/username/common/*"
    catch {exec rsync -av {*}[glob $src] /tmp} res
    puts $res
    

    This is because Tcl doesn't expand glob metacharacters by default; it's safer that way and easier to write correct code (it's hard to write good shell code that is resistant to problems in this area) but it does mean that you need to do a little extra work. The extra work is:

    1. Ask for glob expansion of $srs with the glob command. That returns a list.
    2. Ask for expansion of the list out of the glob by using the {*} pseudo-operator (it's not an operator — it's technically a kind of syntax — but it works a lot like one).

    If you're using Tcl 8.4 (or before!) then you do it a bit different way:

    set srs "/users/home/username/common/*"
    catch {eval [list exec rsync -av] [glob $src] [list /tmp]} res
    puts $res
    

    OK, that's sometimes often shortened by leaving out the list bits (and there are many more obscure ways of writing it!) but that's a bad habit as it can cause huge problems when dealing with variables with values that aren't “nice”, e.g., with spaces in pathnames. If you have 8.5 or later, use {*}. Really.