Control C in Expect Script
By Jefe Nubarron
The Expect computer scripting language derives its name from the primary function of the language, which is to expect user input from an interactive program and automate the response. Expect is primarily used in systems-administration type activities, where an administrator may need to periodically interact with another system in a formulaic manner. One common keystroke in systems administration is "Ctrl-C," typically used to abort a process. Expect is designed to automate any typical key or combination of keys that a system administrator might enter.
Expect Scripting Language
Expect is a scripting language that was designed specifically to automate interaction with computer programs. An Expect script can allow a programmer or systems administrator to automate processes that would typically require user interaction. Don Libes created the Expect language while working at the National Institute of Standards and it has been implemented widely since the early 1990s. Expect is developed on top of the Tcl scripting language and subroutine libraries. The syntax for Expect scripts is similar to that of Tcl scripts.
Certain keyboard sequences have been adopted as standards in the computing world, across platforms and operating systems. The "Ctrl-C" command is one such significant key sequence, particularly on Unix platforms. "Ctrl-C" is typed by simultaneously pressing the "Control" key and the letter "C" key on the keyboard. Within a command-line environment in Unix, "Ctrl-C" has been widely adopted as an abort command. Scripts and commands are designed to terminate upon receiving a "Ctrl-C" interrupt. A common shorthand for "Ctrl-C" is written as ^C.
"Ctrl-C" in Expect
It may be necessary to send or receive a "Ctrl-C" from within an Expect script, although it is more typical to send than receive such a command. The safest way to send a "Ctrl-C" sequence in Expect is to use the octal encoding capabilities of the Tcl language. All control codes have an equivalent octal sequence in ASCII. "Ctrl-C" has an octal value of three in ASCII, so the Expect sequence would be "\003". To send a "Ctrl-C" in Expect using its octal value, use the command "send \003" in your script.
One of the tools available to developers writing scripts in Expect is autoexpect. Autoexpect is designed to generate an Expect script through monitoring your interactions with another program. Autoexpect will reproduce your interactions in an Expect script that can be used in the future. You can use autoexpect to work through complexities you may encounter when coding an Expect script. If you are attempting to send or receive a "Ctrl-C" sequence in Expect, and your script does not behave as anticipated, run an interaction through autoexpect to debug the issues. You can run autoexpect from the command line on any computer where Expect is installed with the "autoexpect" command.
Jefe Nubarron has been writing technical articles since 1995. He has been published in technical magazines and on popular websites. He has a Bachelor of Arts in anthropology and is working on additional coursework towards a master's degree.