Arthur Guillemot
Arthur Guillemot

Reputation: 25

Check if a file exists on a remote server with spaces in path

I'm trying to write a script to send my music from my computer to my android phone via ssh but I'm having some difficulties. Here is the piece of code :

while read SONG
do
    echo $SONG
    ssh -qi ~/.ssh/key root@$ip [[ -f "/sdcard/Music/${SONG}" ]] && echo "File exists" || echo "File does not exist"
done < ~/.mpd/playlists/Blues.m3u

The goal is to check if the song is already in the phone, and if it's not I'll scp it (if it's there it is with the same relative path than in the .m3u file).

I always got sh: syntax error: 'Davis/Lost' unexpected operator/operand and I think it is because there is a space before Davis that I can't escape (the first $SONG is Geater Davis/Lost Soul Man/A Sad Shade Of Blue.mp3)

I also tried this, same result

while read SONG
do
    echo $SONG
    SONG="/sdcard/Music/$SONG"
    echo $SONG

    ssh -qi ~/.ssh/key root@$1 [[ -f "$SONG" ]] && echo "File exists" || echo "File does not exist"
done < ~/.mpd/playlists/Blues.m3u

Ideas welcome !!!

Upvotes: 1

Views: 1289

Answers (1)

Jakuje
Jakuje

Reputation: 25956

ssh command accepts only one argument as an command, as described in synopsis of manual page:

SYNOPSIS
   ssh [...] [user@]hostname [command]

You need to adhere with that if you want correct results. Good start is to put whole command into the quotes (and escape any inner quotes), like this:

ssh -qi ~/.ssh/key root@$1 "[[ -f \"$SONG\" ]] && echo \"File exists\" || echo \"File does not exist\""

It should solve your issue.

Upvotes: 2

Related Questions