如何通过本地shell脚本变量期望? [英] How to pass local shell script variable to expect?

查看:256
本文介绍了如何通过本地shell脚本变量期望?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与如何传递变量有关从shell脚本到期望脚本?

但略有不同:

除了传递两个运行时shell脚本变量外,我想传递一个在shell脚本中的变量,例如:

Apart from passing two runtime shell script variables, I want to pass a variable thats inside the shell script For eg:

#!/bin/sh
d=`date '+%Y%m%d_%H%M'`

expect -c '
expect "sftp>"


#use $d here(how?)
'

推荐答案

您不需要将date变量传递给Expect.它具有内置的很好的日期命令:

You don't need to pass a date variable into expect. It has a very good date command builtin:

expect -c '

# ...

set d [timestamp -format "%Y%m%d_%H%M"]
something_with $d

# ...
'

如果您需要进行更复杂的日期操作,请阅读有关Tcl的信息 clock 命令

If you need to do more complicated date manipulation, read about the Tcl clock command

传递外壳变量期望的另一种好方法(不必做复杂的/混乱的引用/转义)是使用环境:导出外壳变量,然后使用Expect的全局env数组访问它们:

Another good technique to pass shell variables to expect (without having to do complicated/messy quoting/escaping) is to use the environment: export your shell variables, and then access them with expect's global env array:

export d=$(date ...)

expect -c 'puts "the date is $env(d)"'

这篇关于如何通过本地shell脚本变量期望?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆