[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to send a string with special characters in a expect script

Posted on 2010-04-09
8
Medium Priority
?
5,453 Views
Last Modified: 2013-12-26
Hello there,
I am having a problem by sending a string which contains special characters, like dollar sign, into a expect script.  This is a script to change users passwords.  The script is embedded within a bash script in which I am capturing the old and new password for the user, so variables $oldPass and $newPass, used on the expect script, are set previously on the shell script:

echo -e "\nPlease enter your current password: "
stty -echo
read oldPass
echo -e "Please enter your desired password: "
stty -echo
read newPass
echo -e "Please re-enter your desired password: "
stty -echo
read newPass2
stty echo

On the expect script, I use the variables as it:

     expect {
             -re \"word:\" { send \"$oldPass\n\" }
             eof { exit }
             timeout { exit }
     }

but when the old/new passwords have, i.e., a dollar sign, I get this error message (I use $oldPass = my$Test):

can't read "Test": no such variable
    while executing
"set oldPass \"my$Test\""

How can I send a variable on a expect script which contains any special character?  How can I escape an entire variable in a expect script from special characters?

Any help will be sincerely appreciated.

Thanks.
0
Comment
Question by:esbfern
  • 4
  • 4
8 Comments
 
LVL 6

Expert Comment

by:tty2
ID: 30230743
Try

send "$oldPass\n"
0
 

Author Comment

by:esbfern
ID: 30231999
That's not possible because in the way I am embedding the expect script into the bash script is:

#!/bin/bash

................

 process=$(expect -c "

...........

 exit 1 ")
 echo "$process"


If I use your solution, I will be breaking the content of the $process variable and I will get error messages on the shell.
0
 
LVL 6

Expert Comment

by:tty2
ID: 30232817
I usually use separate script for expect, which begins with

#!/usr/bin/expect --

and call it from bash script.
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 

Author Comment

by:esbfern
ID: 30511226
The idea is to have both scripts on the same file for simplicity.   I know how to solve the problem, i.e., with Perl but I would like to make this implementation on a bash script which invokes expect instructions on it.
0
 
LVL 6

Expert Comment

by:tty2
ID: 30512400
#!/bin/bash
expect -c << EOF

...

EOF
0
 

Author Comment

by:esbfern
ID: 30512809
tty2,
thanks for all your comments.   The question I have posted is about how to send special characters, like the dollar sign by using expect.  I am not asking about how to invoke expect within a shell script.  Currently, my script is working correctly, except when sending strings with a dollar sign.  The expect script is 'expecting' a local o global variable when using '$' within the string.   My point is how to instruct the expect script that there is not a variable within the string I just sent.  I am asking the script if for example I send the string my$Test, it send my$Test instead of 'my + variable $Test'.  
Regards.
0
 
LVL 6

Accepted Solution

by:
tty2 earned 2000 total points
ID: 30531121
Add string to the script

oldPass=$(echo -n $oldPass | sed -e 's/\$/\\\$/g')

before using $oldPass in expect.
It works, but try another special characters - what expect will say...
0
 

Author Comment

by:esbfern
ID: 30536941
it appears this make the trick!   Thanks a lot.  I will check with other special characters but it seem it is working as expected.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question