下面你会发现一个简单的代码。如果读取输入为 Y,我将调用函数输入,但是输入函数中的读取命令永远不会执行。
#!/bin/bash
input() {
read -p "Press any key to continue..." # read input (this part never executed)
}
mg() # prompt user for y=true, n=false
{
[ $FORCE ] && echo>&2 "$* (forced)" && return
while read<&2 -p "$* (Y/N)? [Y] " YN
do
[ -z "$YN" -o "$YN" = Y ] && return
[ "$YN" = N ] && return 1
done
err "failed to read from YN $*"
}
if mg "Enter Input?"
then in="yes" | input # call function input if mg return Y
else in="no"
fi