1

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 !!!

1 Answers1

2

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.

Jakuje
  • 24,773
  • 12
  • 69
  • 75
  • Thanks it worked ! Actually ssh -qi ~/.ssh/key root@$1 "[[ -f \"$SONG\" ]]" && echo "File exists" || echo "File does not exist" is correct too, it echoes on the client instead of the server. But the ssh seems to stop the loop so I only have the first line of Blues.m3u – Arthur Guillemot Apr 28 '16 at 17:48
  • this answers that : http://stackoverflow.com/questions/9393038/ssh-breaks-out-of-while-loop-in-bash – Arthur Guillemot Apr 28 '16 at 17:56