问题
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