0

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"\")
4
  • Have you tried using the full path i.e. /usr/bin/ssh ? Commented Jun 28, 2013 at 22:47
  • Unrelated to the question, but you should put $FILE_EXISTS in doublequotes, since it's likely to contain spaces. Commented Jun 28, 2013 at 22:49
  • Unrelated too, but use $(ssh ...) instead of the backquotes. It is much safer and nestable... Commented Jun 28, 2013 at 22:55
  • Thanks for those suggestions. Commented Jun 28, 2013 at 23:25

2 Answers 2

1

PATH is the variable that controls where bash finds commands and you're resetting it. Change your PATH variable name.

remote_file_exists() {
    local SERVER="$1"
    local MYPATH="$2"
    local FILE="$3"
    FILE_EXISTS=`ssh "$SERVER" \'find "$MYPATH" -name \"$FILE\"\'`
    if [ -z $FILE_EXISTS ]; then
        return 1
    else
        return 0
    fi
}
Sign up to request clarification or add additional context in comments.

2 Comments

Agreed, that was a great catch. Now I get a different error: "bash: find <MYPATH> -name "<FILE>": No such file or directory". I think it's coming from the remote server.
BTW, I know that MYPATH exists on the remote server, so I'm fairly sure that's not the problem.
0

Try this. (I've switched the _ to - for easier typing, and `` to $(), but that's not required)

remote-file-exists () 
{ 
    local SERVER="$1"
    local MYPATH="$2"
    local FILE="$3"
    local FILE_EXISTS="$(ssh "$SERVER" find "'$MYPATH'" -name "'$FILE'" -print)"
    if [ -z "$FILE_EXISTS" ]; then
        return 1
    else
        return 0
    fi
}

You could shorten this substantially. Also, local variables are usually lowercased to avoid implying that they're environment variables:

remote-file-exists () 
{ 
    local server="$1" mypath="$2" file="$3"
    [ ! -z "$(ssh "$server" find "'$mypath'" -name "'$file'" -print)" ]
}

1 Comment

Thanks! I really like the shortened version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.