简体   繁体   English

使用 scp 传输搜索文件的 bash 脚本

[英]bash script using scp to transfer a file searched for

I've not had much experience with bash at all.我对 bash 没有太多经验。 The below script worked when I didn't use the find command and just scp straight from the file.当我不使用 find 命令而直接从文件中 scp 时,下面的脚本工作。 But I'm trying to find and transfer a file which has todays d/m/y appended and transfer only this one after finding it.但是我正在尝试查找并传输一个附加了今天 d/m/y 的文件,并在找到后仅传输了这个文件。

Currently gives the error 'file not found' also not sure how to use variables in this situation, as I'm a PHP coder.目前给出错误“找不到文件”,也不确定如何在这种情况下使用变量,因为我是一名 PHP 编码员。 Thanks谢谢

#!/usr/bin/expect -f

FILENAME = appendname_'.date +%d%m%Y;

spawn bash -c "scp 'find /www/reports/archives . -name 
$FILENAME.txt'    ftpdomain.com:/"

expect "password:"

send "pwaord\r"

expect "*\r"

expect "ok"

I recommend you use ssh-keygen and install the key on your server, then you can use scp without having to provide a password through expect .我建议您使用ssh-keygen并在您的服务器上安装密钥,然后您可以使用scp而无需通过expect提供密码。 To your question, you can use command substitution (bash supports $() and ``), something like对于您的问题,您可以使用命令替换(bash 支持$()和 ``),例如

spawn bash -c "scp $(find /www/reports/archives -name $FILENAME.txt)  
        ftpdomain.com:/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM