[Hampshire] Bash pipe creates infinite loop - why?

Top Page

Reply to this message
Author: Russell Gadd
Date:  
To: Hampshire LUG Discussion List
CC: 
Subject: [Hampshire] Bash pipe creates infinite loop - why?
I'm sure one of you gurus will be able to see my error in a trice and
enlighten me. As a relative newcomer I am having difficulty with an infinite
loop in certain circumstances. Basically I'm using echo to pipe 2 pieces of
information, separated by a newline, to a program - this program sometimes
asks for the first input again, in which case I would like to abort, but
unfortunately the echo command seems to keep resending the string.

Details are as follows:

I want to use bcrypt to encrypt a batch of files to write to CD. bcrypt is
really intended to be run manually and only creates a single file per
invocation. I am trying to write a bash wrapper so that I only have to input
the passphrase once (twice counting the verify). In order to test this I
have written a simple test script (below) to do one file only.

When I use correct passphrases the script runs fine. However if I use a
passphrase which is too small (< 8 characters) the script runs forever as
bcrypt keeps asking for reinput of a suitable passphrase and the piping of
the echo command seems to repeat. Clearly my knowledge of pipes is
deficient. (No doubt I could preempt bcrypt's requirements and try to ensure
I don't give it sloppy input, but I'd rather not have a risk of creating an
infinite loop, besides which it would bug me not to understand this). I've
tried redirecting a file as input instead of the echo command, but get the
same effects.

When run at the terminal, bcrypt prompts for a passphrase (min 8
characters), prompts for this to be input again for verification, then
creates a Blowfish encrypted file with an extension of .bfe. The -r option
stops it destroying the input file.

My test script is as follows:

#!/bin/bash
# sample script to encrypt a file

PASS1=asdfgjkl
PASS2=asdfgjkl

if [ "$PASS1" != "$PASS2" ]
then
    echo "Passwords were not the same, aborting"
else
    echo "Passwords ok"


#**************** this is the offending line ***********************
echo -e "${PASS1}\n${PASS1}" |bcrypt -r testfile 2> bcryptoutput.txt

fi

# these are the bcrypt prompts for the passphrase
cat << EOF >bcryptexpected.txt
Encryption key:
Again:
EOF

# if bcrypt throws an error, this next part should print the message
if cmp bcryptexpected.txt bcryptoutput.txt >/dev/null
then
    echo all ok
else
    echo error:
    cat  bcryptoutput.txt
fi