1

I have this simple csh script to rsync stuff :

#!/bin/csh

set SOURCE_FOLDER = $1
set DESTINATION_SERVER_NAME = $2
set DESTINATION_FOLDER = $3

echo Copying ${SOURCE_FOLDER} to ${DESTINATION_SERVER_NAME}:${DESTINATION_FOLDER}

if (ssh $DESTINATION_SERVER_NAME '[ -d $DESTINATION_FOLDER') then
    rsync -vlptr ${SOURCE_FOLDER}  ${DESTINATION_SERVER_NAME}:${DESTINATION_FOLDER}
    set RSYNCSTATUS = $status
    if ($RSYNCSTATUS == 0) then
        echo "COPY : Done"
    else
        exit $RSYNCSTATUS
    endif
else
    echo " ${DESTINATION_SERVER_NAME}:${DESTINATION_FOLDER} does not exist " 
    exit 1
endif

I am running this as super user , so I can ssh freely without passwords or usernames . I want to handle all the error cases of rsync i.e Source does not exits , target does not exist.

Source not existing is taken care of using the $status and inner if condition. However for target not existing , rsync creates the target folder if it doesn't exist. Hence I am forced to handle it explicitly . I know that -e /folder or -d /folder checks if folder exists, but how do I do it across machines ?

This question How to check if dir exist over ssh and return results to host machine

handles it as I tried . But when I run the above script : I get if:Expression syntax

The above one is in bash so am not sure if csh does not support that answer . So , I tried using command mode :

if (`ssh $DESTINATION_SERVER_NAME '[ -d "$DESTINATION_FOLDER" ]'`) then

and

if (`ssh $DESTINATION_SERVER_NAME '[ -d '$DESTINATION_FOLDER' ]'`) then

The first one gives me $DESTINATION_FOLDER undefined variable . Second one gives me a false output i.e if condition fails even if paths exist.

How can I make this work ? I feel I am close somewhere. Or Any other simpler alternatives to get this done ? My hands are tied to using csh here (legacy code , boss ) and it's a slow struggling process. Any inputs would be great .

Community
  • 1
  • 1
Ace McCloud
  • 798
  • 9
  • 27

1 Answers1

0

You need to be caution about variables and quotes. The first example will evaluate on the server, where you don't have such variable and the second is completely wrong since you dive single quotes one into another. You can try this one:

if (`ssh $DESTINATION_SERVER_NAME "[ -d \\\"$DESTINATION_FOLDER\\\" ]"`) then

it should do what you need.

Jakuje
  • 24,773
  • 12
  • 69
  • 75