Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-15-2008, 03:12 PM   #1
LQ Newbie
Registered: Aug 2008
Posts: 2

Rep: Reputation: 0
Expect Scripting help - escape characters


I'm having an issue with scripting expect and I was hoping someone could help me out. I'm scripting a telnet session connect, and I can't get the "expect" command to work correctly on the prompt, which is "~ $ ".

I think expect is getting confused by the $.

Heres the script:

#!/usr/bin/expect -f
set timeout 10
spawn telnet $argv
expect "login:"
send "mylogin\n"
expect "Password:"
send "mypass\n"
expect "~ $ "
# Send some commands

I've tried expect "~ \$", expect \$ and a few other escape permutations but the script just stops working after the prompt comes up. Thanks for any help!
Old 08-15-2008, 08:18 PM   #2
Senior Member
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,712

Rep: Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747
Expect might or might not try to do variable expansion. Just te be sure, use single quotes: '~$'.

Also, at the start of you script say:
exp_internal 1

It will tell you exactly what Expect sees and tries to match.

Hope this helps

Old 08-18-2008, 02:09 PM   #3
LQ Newbie
Registered: Aug 2008
Posts: 2

Original Poster
Rep: Reputation: 0

I was able to get it working by using that debug info from exp_internal. I still couldn't get it to recognize the whole prompt, but was able to get it to recognize the $ at the end which is enough for my purposes.

Single quotes didn't behave as described at all however. Not sure what the issue is there.
Old 09-10-2008, 12:49 PM   #4
LQ Newbie
Registered: Mar 2007
Posts: 2

Rep: Reputation: 0
what was the fix to the $ in the command prompt.

I am using variables and I am having a hard time getting my script to recognize the $ at the command prompt. I have been writing scripts using scp and sftp to get around this right now but...

Could you please post your solution to this problem.

Old 04-02-2009, 11:28 AM   #5
Registered: Mar 2009
Posts: 52

Rep: Reputation: 15
the solution is to use two forward slashes like so:


then it will look for the $ sign.


expect, scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Paths and escape characters code-breaker Linux - Newbie 4 07-12-2006 12:24 AM
Variables and escape characters? flobadon Programming 4 09-10-2005 05:34 PM
bach scripting and escape characters... Bud-froggy Linux - Newbie 3 07-09-2004 05:55 PM
Escape Characters in linux shunraj Linux - Software 1 05-18-2004 04:21 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 04:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:37 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration