简体   繁体   English

生成在Expect脚本中无权访问环境变量

[英]Spawn in expect script do not have access to environment variable

I have a expect script named load_data.exp placed in home directory 我有一个名为load_data.exp的预期脚本放在主目录中

#!/usr/bin/expect
spawn osm2pgsql -s -l -d postgres -W -U postgres -H $OSM_DATABASE_PORT_5432_TCP_ADDR -P 5432 --hstore $filename
expect "Password" 
send "$OSM_DATABASE_ENV_POSTGRES_PASSWORD\n"
interact

there is a environment variable OSM_DATABASE_PORT_5432_TCP_ADDR with has the value of 172.17.0.13 verified by 存在一个环境变量OSM_DATABASE_PORT_5432_TCP_ADDR,其值172.17.0.13已通过以下方式验证

echo $OSM_DATABASE_PORT_5432_TCP_ADDR

output 输出

172.17.0.13

run the load_data.exp by ./load_data.exp, I got the error 通过./load_data.exp运行load_data.exp,我得到了错误

can't read "OSM_DATABASE_PORT_5432_TCP_ADDR": no such variable
while executing
"spawn osm2pgsql -s -l -d postgres -W -U postgres -H $OSM_DATABASE_PORT_5432_TCP_ADDR -P 5432 --hstore $filename"
(file "../load_data.exp" line 4)

seems to be that spawn can not have access to environment variable DATABASE_PORT_5432_TCP_ADDR 似乎是spawn无法访问环境变量DATABASE_PORT_5432_TCP_ADDR

You can pass Bash variables to the Expect the following way: 您可以通过以下方式将Bash变量传递给Expect:

#!/usr/bin/expect
set HOST [lindex $argv 0]
set FILENAME [lindex $argv 1]
set PASSWORD [lindex $argv 2]
spawn osm2pgsql -s -l -d postgres -W -U postgres -H $HOST -P 5432 --hstore $FILENAME
expect "Password" 
send "$PASSWORD\n"
interact

Then call your expect script like this: 然后像这样调用您的期望脚本:

load_data.exp $OSM_DATABASE_PORT_5432_TCP_ADDR $filename $OSM_DATABASE_ENV_POSTGRES_PASSWORD

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

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