Reputation: 6015
I have been using tcl shell on windows but while assisting someone on bash found a weird issue:
export SERVER_HOSTNAME=server1
export USERNAME=root
export PASSWORD=pswd
export LOG_FILE="a.log"
export LOG_PATH="$env(LOG_PATH)"
export log_search_pattern="Filterable_data"
/usr/bin/expect<<EOF
set lineNum {}
set SERVER_HOSTNAME "$env(SERVER_HOSTNAME)"
set USERNAME "$env(USERNAME)"
set PASSWORD "$env(PASSWORD)"
set LOG_FILE "$env(LOG_FILE)"
set log_search_pattern "$env(log_search_pattern)"
set timeout -1
spawn ssh "$USERNAME@$SERVER_HOSTNAME"
expect "assword:"
puts "$expect_out(buffer)"
parray expect_out
send "$PASSWORD\r"
expect "#"
puts "$expect_out(buffer)"
send "grep -n $log_search_pattern $LOG_PATH/$LOG_FILE|tail -1\r"
expect eof
EOF
Now the issue is that the following command:
puts "$expect_out(buffer)"
prints -> (buffer)
But prints the entire buffer contents
parray expect_out
I also tried adding following lines:
set a(1) val1
set a(2) val2
puts $a(1)
puts $a(2)
parray a
It printed:
(1)
(2)
a(1) = val1
a(2) = val2
I tried various combinations to get the puts $a(1) to print val1 but it always printed (1).
What's the correct method to do so?
Upvotes: 0
Views: 145
Reputation: 80931
Variables expand in HEREDOC
s. If you want to avoid that you need to quote some or all of the opening HEREDOC
marker (i.e. <<'EOF'
) (but not the closing one).
From Here Documents in the bash reference manual:
The format of here-documents is:
<<[-]word here-document delimiter
If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.
So when you have:
puts "$expect_out(buffer)"
in the HEREDOC
and expect expect
to see that literal string it actually sees:
puts (buffer)
because the shell has removed the quotes and expanded the $expect_out
variable for you.
Upvotes: 2