Hi everyone, I have a question about using expect with gpg. I'm new to
using expect and am trying to write a simple expect script to deal with
decrypting a .gpg file...
The script is:
spawn gpg -o OUTPUTFILE --yes -d GPGFILE
expect "phrase: "
send "PASSPHRASE\r"
exit
where OUTPUTFILE, GPGFILE and PASSPHRASE are replaced with the files I
want to use. when I invoke expect directly from the command line and
enter each line manually at the "expect1.x>" prompts, it works flawlessly
every time, however when I put the exact same lines into a script, it
appears to do everything, but the script exits and the gpg file is simply
never decrypted. any ideas what I'm doing wrong?
thanks!
peter
My guess: you need to give the process time to finish, rather than
exiting immediately after delivery of PASSPHRASE. How about putting
one more [expect] before the exit?
your guess was right! it was killing gpg before it had a chance to
finish, so i had to put in another expect statement, in this case:
expect "\"$"
since a successful decryption results in a line with a double quote at
the very end of the line just before gpg exits. thanks very much for your
help!!!
best,
peter
# this script should be called with syntax:
# expect_test encryptedfile outputfile "passphrase"
# NOTE that the passphrase should be put in double quotes in case it has
any spaces
set encryptedfile [lindex $argv 0]
set outputfile [lindex $argv 1]
set the_passphrase [lindex $argv 2]
spawn gpg -o $outputfile --yes -d $encryptedfile
expect "phrase: "
stty -echo
send $the_passphrase\r
stty echo
expect "\"$"
exit
-----------
FYI, if you really want to make sure the spawned process has exited,
you can use expect eof. That way if future/alternate versions of pgp
change chars that are output, you won't be negatively affected.
Bruce