When scp is performed with bash expect, if there is a space in the file name, it will be treated as a separate file

Question: Question:

When scp is performed with bash expect, if the file name is separated by spaces, it will be treated as a separate file.

fromfile='test@testserver.co.jp:/tmp/hoge¥ hoge¥ hoge.txt'
expect -c "
set timeout 3600
spawn env LANG=C /usr/bin/scp ${fromfile} ${encdir}${ori_file}
expect \"password:\"
send \"${PW}\n\"
expect \"$\"
exit 0
"

This is the same for (ya) and [ya]. It is necessary to escape once, but even if it is escaped as above, it will be treated as a separate file and an error will occur.

If you know how to deal with it, please let us know.

Answer: Answer:

Of comments

Even if you use the shell command scp test@testserver.co.jp:'hoge hoge.txt' test.txt, hoge
And hoge will split it.

If is correct

export FROMFILE='test@example.co.jp:"/tmp/hoge hoge hoge.txt"'
export ENCDIR='...'
export ORI_FILE='...'
export PW='...'
expect -c '
  set timeout 3600
  spawn env LANG=C /usr/bin/scp "$env(FROMFILE)" "$env(ENCDIR)$env(ORI_FILE)"
  expect "\[Pp\]assword:"
  send -- "$env(PW)\n"
  expect "$"
  exit 0
'

Wonder? As you can see on https://stackoverflow.com/questions/19858176/how-to-escape-spaces-in-path-during-scp-copy-in-linux , it seems to be useless unless you double escape or quote. ..

Quarting seems to be difficult unless various variables are passed as environment variables instead of being embedded in the expect script as bash variables.

2017-03-14 Addendum: Since it was regularly (exposed) to "evaluate someone", I added consideration to quoting properly so as not to be embarrassed. Added consideration for passwords starting - .

Scroll to Top