简体   繁体   English

将变量传递给 Expect 和 Spawn

[英]Passing variable to Expect and Spawn

I'm writing a script that will scp a tar file from my local server to a remote host.我正在编写一个脚本,它将 tar 文件从我的本地服务器scp到远程主机。 Since the script generates the file through a pre-requisite process, the name is generated dynamically.由于脚本通过先决条件生成文件,因此名称是动态生成的。 My script needs to take the name of the file and pass it to scp for transfer.我的脚本需要取文件名并将其传递给 scp 进行传输。

#!/usr/bin/expect -f

spawn scp test.$(date +%y%m%d_%H%M).tar user@IP-ADDRESS:/destination/folder
set pass "password"
expect "password: "
send -- "$pass\r"
expect eof

I've tried setting the filename as a variable but keep seeing the same error:我尝试将文件名设置为变量,但一直看到相同的错误:

can't read "(date +%y%m%d_%H%M)": no such variable while executing "spawn scp test.$(date +%y%m%d_%H%M).tar user@IP-ADDRESS:/destination/folder"无法读取“(date +%y%m%d_%H%M)”:执行“spawn scp test.$(date +%y%m%d_%H%M).tar user@时没有这样的变量” IP地址:/目的地/文件夹”

$(date +%y%m%d_%H%M) is not a Tcl command. $(date +%y%m%d_%H%M)不是 Tcl 命令。 If you use expect , you have to learn Tcl.如果使用expect ,则必须学习 Tcl。 To get a formatted date in Tcl, use the clock command.要在 Tcl 中获取格式化的日期,请使用clock命令。 Also, interpolation of the result from a command in Tcl is not done by $(....) , but by [....] .此外,Tcl 中命令的结果的插值不是由$(....) ,而是由[....] You can find examples for this construct here .您可以在此处找到此构造的示例。

Decided to go another route since the team was able to provision a new Artifactory repo for this binary and alike.决定走另一条路,因为团队能够为此二进制文件等提供新的 Artifactory 存储库。 However, to the advice provided here I was able to make a few discoveries which I used to fix my issues:但是,根据此处提供的建议,我能够发现一些用于解决问题的发现:

I also had a password with $ symbol and that also caused a world of issues.我还有一个带有 $ 符号的密码,这也引起了很多问题。

#!/bin/bash

TEST=$(date +%y%m%d_%H%M)
/usr/bin/expect <<eof

set password {pas\$word}
spawn scp "$TEST" user@IP-ADDRESS:/destination/folder
expect "*password:"
send "$pasword\r"
expect eof

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

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