0

Im trying to copy file from /home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt on pc2 to the same location on pc1 (i.e. the current PC). If this seems a little bit weird I had to change the names of folders for privacy reasons.

However, I keep on getting mixtures of the bash syntax error for unexpected token: ( or an scp protocol error or scp can't find the file (which I have verified to be there). The issue stems from handling the directory with brackets in name i.e. apple(grannysmith) Here are the few cases I have tried

Case 1 (Basic Try):

dest="/home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt"
sshpass -p "amd" scp -o StrictHostKeyChecking=no usr2@pc2:"$dest" "$dest"

Output:

bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `scp -f /home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt'

Case 2 (Surrounding file args in single quotes)

dest="/home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt"
sshpass -p "amd" scp -o StrictHostKeyChecking=no usr2@pc2:"'$dest'" "'$dest'"

Output:

protocol error: filename does not match request

Case 3 (Fixing protocol error with -T flag):

dest="/home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt"
sshpass -p "amd" scp -o StrictHostKeyChecking=no usr2@pc2:"'$dest'" "'$dest'"

Output:

'/home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt': No such file or directory

Case 4 (Single quote assign the variable as well):

dest='/home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt'
sshpass -p "amd" scp -o StrictHostKeyChecking=no usr2@pc2:"$dest" "$dest"

Output:

bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `scp -f /home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt'

Case 5 (Single quote assign the variable and the calls):

dest='/home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt'
sshpass -p "amd" scp -o StrictHostKeyChecking=no usr2@pc2:"'$dest'" "'$dest'"

Output:

'/home/myuser1/fruits/apple(grannysmith)/size/2lbs/price.txt': No such file or directory

I've tried a lot more with trying to escape the brackets or all sorts but nothing has worked, it will just be either the scp file error or the bash syntax error. I've also added and removed the shebang #!/bin/bash and no dice either.

I don't know how to properly escape this directory for the command. I know that in the OS, the directory is outputted with single quotes surrounding it when I use the ls command (so 'apple(grannysmith)'). I also know that if I want to get into the directory, I can't do cd apple(grannysmith), I have to do cd 'apple(grannysmith)' And the weird thing is that I tried the variations with the cd command in script for comparison, but in places where it worked the scp still failed

I would appreciate any help in how to escape the brackets (and spaces) properly. If you have multiple machines in a network, I would love if you could set up a sample environment just to test it out: Directory on both machines, doesn't have to be same as mine, just make sure you have one of the intermediate dirs as <something>(<something>) scp is called from script and not from command line

Unfortunately changing the name of the directories and files isn't an option, but I'm willing to hear other options for transferring files. scp is my go to but I don't know if I can copy files in other ways.

Iffy
  • 13
  • 3
  • You need to quote/escape the remote filename differently from the local filename, because it goes through a second layer of shell parsing before it's used. Does my answer [here](https://stackoverflow.com/questions/9483171/escaping-special-characters-in-bash-variables) work for you? – Gordon Davisson Sep 15 '21 at 23:48

0 Answers0