| |||||||||||
Char "^M" at end of each line in output file Posted by Kan (Kan), 24 November 2005 I've prepared a expect script on a Linux box to telnet to a SUNOS box. In the telnet session, I ping to 2 remote host to check connectivity and output the ping result to a file.However, when I use vi to view the file, there are strange char "^M" at the end of each line. How to prevent "^M" from writing into the output file? Here is my script: #!/usr/bin/expect set timeout 300 set hostname IPAddress set username abc set password pw spawn telnet $hostname expect "login: " send "$username\r" expect "Password:" send "$password\r" send "\r" expect "bash$ " set ret [open result.txt w] send "ping 1.1.1.1\r" send "ping 1.1.1.2\r" send "uname\r" expect "SunOS" puts $ret "$expect_out(buffer)" close $ret exit Here is the result.txt view in vi: ^M bash$ ping 1.1.1.1^M 1.1.1.1 is alive^M bash$ ping 1.1.1.2^M 1.1.1.2 is alive^M bash$ uname^M SunOS Posted by admin (Graham Ellis), 24 November 2005 At the end of each line you've got a carriage return - control M - character. You'll see it in Tcl strings represented as \r. Terminal output has a carriage return, line feed sequence on it as do Windows files - that's ^M^J or \r\n. The string trim command can be used to get rid of leading and trailing white space. Posted by Kan (Kan), 25 November 2005 Thanks Graham. It works after I string trim each line in $expect_out(buffer) before I write to the output file as follow: set buf $expect_out(buffer) foreach line [split $buf "\n"] { set line [string trim $line] puts $ret $line } This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |