Expect script – Read / write file

Thanks to this source : http://wiki.tcl.tk/367

To read data from a file in an Expect script:

#!/usr/bin/expect -f

set fd "input.txt"
set fp [open "$fd" r]
set data [read $fp]

# Read line by line

foreach line $data {

puts "$line\r"

}

And to write data to a file:

#!/usr/bin/expect -f

set outputFilename "output.txt"
set outFileId [open $outputFilename "w"]

puts -nonewline $outFileId "A first line\n"
puts -nonewline $outFileId "A second line\n"

#Close file descriptor to ensure data are flush to file
close $outFileId

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s