Expect Script - Variables are Empty immediately after "Setting" Them?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect Script - Variables are Empty immediately after "Setting" Them?
# 1  
Old 03-13-2013
Expect Script - Variables are Empty immediately after "Setting" Them?

Hello All,

I have embedded some expect code inside a Bash script I'm writing, but for some reason any variable I 'set' to something is
showing as empty immediately on the next line...

I haven't run into this problem before so I'm not sure what it could be...? I'm guessing it has something to do with the code
being embedded in another script..?


Here's my Code:
Code:
#!/bin/bash

echo "Here, in Bash Script"

# other stuff.....:......


/usr/bin/expect - << EOF

    set my_var "Hello World"

    puts "my_var = $my_var\n"

EOF

echo "Back from Expect Script"

exit 0

And my Output:
Code:
Here, in Bash Script
my_var = 

Back from Expect Script

Anyone know why the variables are NOT being set? I'm Stumpped..!!!
Any thoughts would be very much appreciated.


Thanks in Advance,
Matt

---------- Post updated at 04:51 PM ---------- Previous update was at 04:17 PM ----------

Figured it out...!!

You need to escape the '$' in front of any/all variables. I think this is due to using the "<<" which is open to variable expansion, so
I think that's why they need to be escaped...

So if the code is like this, it will work:
Code:
#!/bin/bash

echo "Here, in Bash Script"

# other stuff.....:......


/usr/bin/expect - << EOF

    set my_var "Hello World"

    puts "my_var = \$my_var\n"

EOF

echo "Back from Expect Script"

exit 0

*Notice the '\' in front of "\$my_var" in the puts statement...

And my Output:
Code:
Here, in Bash Script
my_var = Hello World

Back from Expect Script

Hopefully that helps someone in the same predicament I was in...

Thanks,
Matt
# 2  
Old 03-13-2013
Exactly right. The $ gets substituted first, otherwise.

There is also an alternative way:

Code:
<<"EOF"

This kind of here-doc will not substitute, at all.

Last edited by Corona688; 03-13-2013 at 07:40 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-14-2013
Hey Corona, thanks for the reply!

Ohh ok, cool... So if you quote the "EOF" then do you not need to escape the '$' for all the variables?


Thanks Again,
Matt
# 4  
Old 03-14-2013
Exactly. (The ending EOF is not in quotes, incidentally.)

The only problem is, if you actually want to substitute anything, you really can't.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-14-2013
Hey Corona, thanks again for the reply!

Yea, that should be fine, I don't think I'll need to do that in there anyway... Its just basically only like 10 lines or so of code, so nothing crazy
going on in there.

Anyway, thanks again for the info, much appreciated!

Thanks,
Matt
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All, i am trying to ssh to a remote machine and execute certain command to remote machine through script. i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as expect: spawn id exp5 not open while executing "expect "$" {... (3 Replies)
Discussion started by: Siddharth shivh
3 Replies

3. Shell Programming and Scripting

Can someone please show me a very simple "expect" script to change password in Solaris please?

Ladies & Gents, Can one of you gurus please show me a very simple "expect" script to change the password in Solaris in a script, please? Nothing fancy, no error checking, no nothing. Just to change the password of a new user, it's all. Many thanks in advance. U guys have honestly earned my... (1 Reply)
Discussion started by: Hiroshi
1 Replies

4. Shell Programming and Scripting

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (14 Replies)
Discussion started by: thisissouvik
14 Replies

5. Shell Programming and Scripting

Passing username and password to a script running inside "expect" script

Hi I'm trying to run a script " abc.sh" which triggers "use.sh" . abc.sh is nothing but a "expect" script which provides username and password automatically to the use.sh script. Please find below the scripts: #abc.sh #!/usr/bin/expect -f exec /root/use.sh expect "*name*" send... (1 Reply)
Discussion started by: baddykam
1 Replies

6. Shell Programming and Scripting

exec perl in expect script yields "invalid command"

I'm trying to execute something like this: exec perl -i -pe 's/\015/\012/g' '${file}' in my expect script and I get: error "invalid command name \"perl\". however, if I run perl -i -pe 's/\015/\012/g' "/Users/Shared/menu-items.txt" directly in my terminal, it runs fine. I'm an... (4 Replies)
Discussion started by: dpouliot
4 Replies

7. UNIX for Dummies Questions & Answers

Setting shell variables with ":" syntax

I nedd help on the below highlited one. can any one explain on this,How its work in shell scripting.. then that will be very great full... L_ABCD=${L_DBG:=N} Thanks Muddasas (3 Replies)
Discussion started by: muddasani
3 Replies

8. Shell Programming and Scripting

check input = "empty" and "numeric"

Hi how to check input is "empty" and "numeric" in ksh? e.g: ./myscript.ksh k output show: invalid number input ./myscript.ksh output show: no input ./myscript.ksh 10 output show: input is numeric (6 Replies)
Discussion started by: geoffry
6 Replies
Login or Register to Ask a Question