Home Expect: how to spawn a command containing a backslash?
Reply: 0

Expect: how to spawn a command containing a backslash?

user6311
1#
user6311 Published in September 20, 2018, 10:22 pm

I have the following script:

#!/bin/bash
echo -n "Enter user name: "
read USER
echo -n "Enter password: "
read -s PWD
cat $HOME/etc/switches.txt | while read IP SWITCH
do
  echo ${SWITCH}
  /usr/bin/expect <<EOD
# Change to 1 to Log to STDOUT
log_user 1
# Change to 1 to enable verbose debugging
exp_internal 1
# Set timeout for the script
set timeout 20
spawn ssh -l {$USER} -oCheckHostIP=no -oStrictHostKeyChecking=no -q $IP
match_max [expr 32 * 1024]
expect "Password:"
send $PWD
send "\n"
expect "#"
send "show fcip summary | grep TRNK\n"
EOD
  echo
done

When I run it, the backslash in the username disappears, giving these result:

Enter user name: corp\user
Enter password:
=== ss3303-m-esannw-m01a ===
spawn ssh -l corpuser -oCheckHostIP=no -oStrictHostKeyChecking=no -q 10.247.184.70
[...]

I suspect my problem is due in part to embedding my expect script inside a bash script. I've tried using $USER and "$USER" as well, with the same results. Using corp\\\\user (yes, four backslashes!) does work but is inconvenient. I'm seriously considering using sed or something to multiply the backslashes, but would love to hear other ideas.

share|improve this question
  • Yes, the backslash is treated specially so you have to use two of them. Not really sure why this case requires four, though. – bb94 Nov 8 '17 at 14:48
  • take a look at stackoverflow.com/questions/15398022/… – pynexj Nov 8 '17 at 15:13

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.47801 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO