When I use scp command through expect, asterisk can not recognized

主宰稳场 提交于 2019-12-02 18:57:53

问题


Hello I have a one problem in linux shell

I write a scp script using expect and the script is like this.

#!/bin/sh

expect -c "spawn scp /tmp/data/*2017-06-14*.log2 id@localhost:~/"\
       -c "expect -re \"password\" "/
       -c "sleep 1" \
       -c "send \"password\r\""\
       -c "interact"

and result of execution shows error message.

/tmp/data/*2017-06-14*.log2 : No such file or directory

But When not use expect, scp execution is success

[user@localhost]# scp /tmp/data/*2017-06-14*.log2 id@localhost:~/"\

How can i solve this problem?


回答1:


Expect does not understand shell's syntax. You can:

spawn sh -c "scp /tmp/data/*2017-06-14*.log2 id@localhost:~/"

or

spawn sh -c {scp /tmp/data/*2017-06-14*.log2 id@localhost:~/}

komar's answer using glob would not always work. See following example:

bash-4.4# expect
expect1.1> system pwd
/root/tmp/tcl
expect1.2> system ls -l
total 0
-rw-r--r-- 1 root root 0 Jun 16 10:55 a  b
-rw-r--r-- 1 root root 0 Jun 16 10:55 c  d
expect1.3> spawn ls -l [glob *]; expect eof
spawn ls -l {a  b} {c  d}
ls: cannot access {a  b} {c  d}: No such file or directory
expect1.4> spawn sh -c "ls -l *"; expect eof
spawn sh -c ls -l *
-rw-r--r-- 1 root root 0 Jun 16 10:55 a  b
-rw-r--r-- 1 root root 0 Jun 16 10:55 c  d
expect1.5>



回答2:


You need globbing in expect/tcl style:

expect -c "spawn scp [glob /tmp/data/*2017-06-14*.log2] id@localhost:~/"


来源:https://stackoverflow.com/questions/44559187/when-i-use-scp-command-through-expect-asterisk-can-not-recognized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!