当我通过 expect 使用 scp 命令时,星号无法识别

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

你好,我有一个问题 linux shell

我用expect写了一个scp脚本,脚本是这样的

#!/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"

执行结果显示错误信息。

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

但不使用expect时,scp执行成功

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

我该如何解决这个问题?

Expect 不理解 shell 的语法。您可以:

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

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

使用 glob 并不总是有效。请参阅以下示例:

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>

您需要 expect/tcl 风格的 globbing:

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