0

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?

1 Answer 1

2

Variables expand in HEREDOCs. 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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Etan - but it does that even without the quotes: e.g. puts $a(1) would also print "(1)" - any way to overcome this.
I'm confused by that comment. The quotes around the variable expansion are not the problem. The lack of quotes around the heredoc marker are the problem. The entire heredoc is being subjected to various kinds of expansion by the shell. You don't want that so quote the heredoc marker... or escape every $, ", ', etc. in the heredoc with backslashes.
@user1570408, this same issue is also affecting the variables you're passing in from shell to expect: the shell does not have a $env variable defined, so in expect you have $SERVER_HOSTNAME == "(SERVER_HOSTNAME)" etc
To demonstrate what's going on, where you set your shell exports, add this line: a="I am a shell variable"
Thanks Etan and Glenn, being new to tcl - bash, (is it possible?) will be trying edit the HereDoc as Etan suggested and post results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.