在 UNIX BASH 中使用通配符参数从远程服务器复制多个 SCP 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32598129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:34:01  来源:igfitidea点击:

Copying SCP multiple files from remote server with wildcard argument in UNIX BASH

bashunixexpectscp

提问by user3390986

This Expect script is a part of my UNIX Bash script

这个 Expect 脚本是我的 UNIX Bash 脚本的一部分

expect -c " 
spawn scp [email protected]:\"encryptor *.enc\" . 
expect password:  { send \"$PASS\r\"  }
expect 100%
sleep 1
exit
 "

I am trying to copy both 'encryptor' and '*.enc' with this one SCP command. Console tells me it cannot find ' *.enc" '

我正在尝试使用这个 SCP 命令复制“加密器”和“*.enc”。控制台告诉我它找不到 ' *.enc' '

回答by PierreLecocq

Not sure it helps but I was looking for a similar objective.

不确定它有帮助,但我一直在寻找类似的目标。

Means: Copying some selected files based on their filenames / extensions but located in different subfolders (same level of subfolders).

意思是:根据文件名/扩展名复制一些选定的文件,但位于不同的子文件夹(相同级别的子文件夹)。

this works e.g. copying .csv files from Server:/CommonPath/SpecificPath/

这有效,例如从复制 .csv 文件 Server:/CommonPath/SpecificPath/

scp -r Server:/CommonPath/\*/\*csv YourRecordingLocation

I even tested more complex "perl like" regular expressions.

我什至测试了更复杂的“perl like”正则表达式。

Not sure the -roption is still useful.

不确定该-r选项是否仍然有用。

回答by Marc Young

The syntax for multiple files:

多个文件的语法:

$ scp [email protected]:~/\{foo.txt,bar.txt\} .

I would guess in your case (untested)

我猜你的情况(未经测试)

scp [email protected]:\{encryptor, \*.enc\} .

回答by Dinesh

#!/bin/bash
expect -c "
set timeout 60; # 1 min 
spawn scp [email protected]:{encryptor *.enc} .
expect \"password: $\"
send \"mypassword\r\"
expect eof
"

You can increase the timeoutif your file copy takes more time to complete. I have used expect eofwhich will wait till the closure of the scpcommand. i.e. we are waiting for the End Of File (EOF) of the scpafter sending the password.

timeout如果您的文件复制需要更多时间来完成,您可以增加。我已经使用expect eofwhich 将等到scp命令关闭。即我们scp在发送密码后等待文件结束(EOF)。