Expect
expect(1) is a tool and scripting language for automating interactive command line tools.
Contents
Example
Consider the age(1) encryption tool, which supports passphrase (symmetric key) encryption but does not support passing the passphrase in plain text.
$ tar -c example.txt | age --encrypt --passphrase > example.tar.age Enter passphrase (leave empty to autogenerate a secure one): Confirm passphrase:
To automate this exchange, try:
expect <<EOF spawn sh -c "age --encrypt --passphrase example.tar > example.tar.age" expect "*:" send "test123\r" expect "*:" send "test123\r" expect eof EOF
Installation
Most Linux and BSD distributions offer an expect package.
Syntax
spawn executes a singular command. To spawn a pipeline of commands, instead spawn a shell process with the pipeline passed as a command string.
expect reads output from the spawned process and conditionally dispatches to further logic.
send submits some message to the spawned process. Typically it is necessary to specify the newline (using \r) at the end of the message.
Passing exit codes
To pass an exit code from a spawned process, try:
foreach {pid spawnid os_error_flag value} [wait] break exit $value