Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I've got an annoying problem with a shell script that sets some variables, then executes rsync with_my_options_defined_in_variables. Here's what my script looks like: Code: #!/bin/sh DEST="r002" USER="root" BACKDIR="/" DESTDIR="/" ...
  1. #1
    Just Joined!
    Join Date
    Aug 2004
    Location
    Seattle, WA
    Posts
    42

    Shell script, executing command with variables

    Hello,

    I've got an annoying problem with a shell script that sets some variables, then executes rsync with_my_options_defined_in_variables. Here's what my script looks like:

    Code:
    #!/bin/sh
    
    DEST="r002"
    USER="root"
    BACKDIR="/"
    DESTDIR="/"
    FILTER=/etc/rsyncd.filter
    
    if [ ! -f $FILTER ]
    then
            # Doesn't exist
            FILTER=" "
    else
            # Exists
            echo "FILTER is: " $FILTER
    fi
    OPTS="-n -vv -u -a --rsh=ssh --filter='merge "$FILTER"' --stats --progress"
    
    # May be needed if run by cron?
    export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
    
    # Only run rsync if $DEST responds.
    VAR=`ping -s 1 -c 1 $DEST > /dev/null; echo $?`
    if [ $VAR -eq 0 ]; then
            echo "Command to execute: rsync $OPTS $BACKDIR $USER@$DEST:$DESTDIR"
    	#this returns an error
            rsync $OPTS $BACKDIR $USER@$DEST:$DESTDIR
            
            EXEC_CMD="rsync $OPTS $BACKDIR $USER@$DEST:$DESTDIR"
    	#this returns same error as above
            $EXEC_CMD
            
    	#this works just fine
            rsync -n -vv -u -a --rsh=ssh --filter='merge /etc/rsyncd.filter' --stats --progress / root@r002:/
    else
        echo "Cannot connect to $DEST."
    fi
    Inside the "if" statement I've tried running the command 3 different ways:
    1. rsync with the options.
    2. declare new variable with the entire command, then execute it.
    3. run the command with all the options not using variables.

    This is what I get when I run the command:

    Code:
    zRH5> ./rsync_opt.sh  
    FILTER is:  /etc/rsyncd.filter
    OPTS:  -n -vv -u -a --rsh=ssh --filter='merge /etc/rsyncd.filter' --stats --progress
    Command to execute: rsync -n -vv -u -a --rsh=ssh --filter='merge /etc/rsyncd.filter' --stats --progress / root@r002:/
    Unknown filter rule: `'merge'
    rsync error: syntax or usage error (code 1) at exclude.c(769) [client=2.6.8]
    Unknown filter rule: `'merge'
    rsync error: syntax or usage error (code 1) at exclude.c(769) [client=2.6.8]
    opening connection using ssh -l root r002 rsync --server -vvunlogDtpr . / 
    building file list ... 
    [sender] hiding directory srv because of pattern /*
    [sender] hiding file webmin-setup.out because of pattern /*]
    ...snip...
    I'm sure it's just a simple syntax problem with the quotes or with how I'm defining the variables, but I can't figure it out for the life of me. Any ideas?

    Thanks,
    Jared

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Try this:

    Code:
    echo $EXEC_CMD
    eval $EXEC_CMD
    The 'echo' is to see what the command looks like.

    Run the script like this to see the commands as they execute:

    Code:
    sh -vx ./rsync_opt.sh

  3. #3
    Just Joined!
    Join Date
    Aug 2004
    Location
    Seattle, WA
    Posts
    42
    When executing the script using sh -vx I got:

    Code:
    FILTER=/etc/rsyncd.filter
    + FILTER=/etc/rsyncd.filter
    
    ...snip...
    
    OPTS="-avz --rsh=ssh --filter='merge $FILTER' --stats"
    + OPTS='-avz --rsh=ssh --filter='\''merge /etc/rsyncd.filter'\'' --stats'
    
    ...snip...
    
    + echo 'OPTS: ' -avz --rsh=ssh '--filter='\''merge' '/etc/rsyncd.filter'\''' --stats
    OPTS:  -avz --rsh=ssh --filter='merge /etc/rsyncd.filter' --stats
    
    ...snip...
    
    + rsync -avz --rsh=ssh '--filter='\''merge' '/etc/rsyncd.filter'\''' --stats / root@r002:/
    Unknown filter rule: `'merge'
    Using eval worked just fine though.

    Why would there be extra slashes and apostrophes?

Posting Permissions

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