0

I am trying to echo an expect script into a file so I can execute it on the fly.

Here is what I have so far:

#!/bin/bash

echo '#!/usr/bin/expect -f' > expect_file
echo 'expect "\[RETURN\]" {send "\r"}' >> expect_file

that will yield me a file with the following:

#!/usr/bin/expect -f
"}pect "\[RETURN\]" {send "

If I try and use echo and escape the quotations:

#!/bin/bash

echo '#!/usr/bin/expect -f' > expect_file
echo "expect \"\[RETURN\]\" {send \"\r\"}" >> expect_file

cat expect_file
exit 0

The expect command and parts of the line do not get echoed

#!/usr/bin/expect -f
"}pect "\[RETURN\]" {send "

How can I echo or place the follow lines of code into a file by itself from within my bash script?

#!/usr/bin/expect -f
expect "\[RETURN\]" {send "\r"}
expect ":" {send "q"}
expect "otherwise:" {send "y\r"}
expect eof {exit}
Curious Sam
  • 737
  • 5
  • 21
  • Are you sure you are executing the script with `bash`? That result is consistent with a POSIX-compliant implementation of `echo`, where `\r` in the argument to `echo` is treated as a literal carriage return. If you display the resulting file with `cat`, the final `"}` will appear at the beginning the line. – chepner Oct 26 '17 at 22:56

2 Answers2

2

Don't use echo. Use cat:

cat << 'EOF' > expect_file
#!/usr/bin/expect -f
expect "\[RETURN\]" {send "\r"}
expect ":" {send "q"}
expect "otherwise:" {send "y\r"}
expect eof {exit}
EOF
William Pursell
  • 174,418
  • 44
  • 247
  • 279
1

Using echo

#!/bin/bash
echo -e '#!/usr/bin/expect -f\nexpect "\\[RETURN\\]" {send "\\r"}\nexpect ":" {send "q"}\nexpect "otherwise:" {send "y\\r"}\nexpect eof {exit}' > expect_file

OR

#!/bin/bash
echo '#!/usr/bin/expect -f
expect "\[RETURN\]" {send "\r"}
expect ":" {send "q"}
expect "otherwise:" {send "y\r"}
expect eof {exit}' > expect_file

OR

#!/bin/bash
echo '#!/usr/bin/expect -f' > expect_file
echo 'expect "\[RETURN\]" {send "\r"}' >> expect_file
echo 'expect ":" {send "q"}' >> expect_file
echo 'expect "otherwise:" {send "y\r"}' >> expect_file
echo 'expect eof {exit}' >> expect_file
Darby_Crash
  • 436
  • 3
  • 6