<&4 waitfor 'password:'
echo "$2"
<&4 waitfor 'password:'
echo "$2"
<&4 cat >/dev/null
) | ( pty passwd "$1" >out.$$ )
Here, 'waitfor' is a simple C program that searches for
its argument in the input, character by character.
A simpler pty solution (which has the drawback of not
synchronizing properly with the passwd program) is
#!/bin/sh
( sleep 5; echo "$2"; sleep 5; echo "$2") | pty passwd "$1"