I've created a function to check for the existence of a file on a remote server, but I'm getting the error "ssh: command not found" when I try to call the function. Here's the function:
remote_file_exists() {
local SERVER="$1"
local PATH="$2"
local FILE="$3"
FILE_EXISTS=`ssh "$SERVER" \'find "$PATH" -name \"$FILE\"\'`
if [ -z $FILE_EXISTS ]; then
return 1
else
return 0
fi
}
I'm calling the function like this:
if ( remote_file_exists $REMOTE_SERVER "$REMOTE_PATH/" $REMOTE_FILE ); then
echo "$REMOTE_PATH/$REMOTE_FILE exists on $REMOTE_SERVER"
...
The error I'm getting:
myscript.sh: line x: ssh: command not found
The value of 'x' in the error is the line number of the line in the function that starts with "FILE_EXISTS=".
I suspect this has something to do with not quoting correctly, but I can't figure it out. What am I doing wrong?
EDIT: Thanks to Cfreak for the good catch on the PATH variable name. Once I fixed that I got a different error:
bash: find <MYPATH> -name "<FILE>": No such file or directory
After some experimenting I found that removing the escaped single quotes fixed the 2nd issue. The working line looks like this:
FILE_EXISTS=$(ssh "$SERVER" find "$MYPATH" -name \""$FILE"\")
$FILE_EXISTSin doublequotes, since it's likely to contain spaces.$(ssh ...)instead of the backquotes. It is much safer and nestable...