Hello lad
Hello lad

Reputation: 18790

Spawn in expect script do not have access to environment variable

I have a expect script named load_data.exp placed in home directory

#!/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

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

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

Upvotes: 2

Views: 2435

Answers (1)

szpal
szpal

Reputation: 647

You can pass Bash variables to the Expect the following way:

#!/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

Upvotes: 1

Related Questions