1

I'm trying to write a script to copy multiple files (in multiple directories) from a remote host to my local machine.

My script is (more or less) as follows:

path1="/home/db/primary/*.xml"
path2="/tmp/log_*"
copyto="/home/pathtodesktop/Desktop/temp"

mkdir $copyto

scpcommand="scp -o StrictHostKeyChecking=no root@$address:\"$path1 $path2\" $copyto"
echo $scpcommand
$scpcommand

When I run the script, I get the following output:

scp -o StrictHostKeyChecking=no root@SERVER:"/home/db/primary/*.xml /tmp/log_*" /home/pathtodesktop/Desktop/temp
sh: syntax error: unterminated quoted string
cp: cannot stat '/tmp/log_*"': No such file or directory

The output of the echo is as expected. But when I copy the output above and run the command manually in the terminal, it works as expected with no errors.

So the ultimate question is, what am I doing wrong? The command seems to work fine when run manually in the terminal. Where is my syntax error?

Thanks for your help!

Drexlin
  • 31
  • 6

1 Answers1

1

Adding set -f will prevent the wildcards in your paths from being expanded locally (although you may run in to other issues with spaces/special characters).

(You can re-enable wildcards afterwards with set +f)

Nick
  • 7,700
  • 2
  • 29
  • 37