本文介绍了当我通过期望使用scp命令时,星号无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在linux shell中遇到一个问题

Hello I have a one problem in linux shell

我使用Expect编写了一个scp脚本,该脚本是这样的.

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

但是如果不使用期望,则scp执行成功

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?

推荐答案

期望不了解shell的语法.您可以:

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

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

komar的答案并不总是有效.请参见以下示例:


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>

这篇关于当我通过期望使用scp命令时,星号无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:17