loop=1
while [ loop -eq "1" ]
echo "Type F for first choice"
read firstChoice
echo "Type S for second choice"
read secondChoice
if [ "$firstChoice" = "F" ]; then
echo "This is the first choice"
elif [ "$secondChoice" = "S" ]; then
echo "This is the second choice"
else
echo "no choice"
loop=0
exit 1
fi
how do i catch the keyboard input for $var to quit any time?
if [ $var = "q" ]; then
echo "yes... quitting"
exit 1
fi
done
check() {
if [ $1 = "q" ]; then
echo "yes... quitting"
exit 1
fi
}
loop=1
while [ loop -eq "1" ]
echo "Type F for first choice"
read firstChoice
#
check firstChoice
#
echo "Type S for second choice"
read secondChoice
#
check secondChoice
#
if [ "$firstChoice" = "F" ]; then
echo "This is the first choice"
elif [ "$secondChoice" = "S" ]; then
echo "This is the second choice"
else
echo "no choice"
loop=0
exit 1
fi
done
What do you mean by "any time"? Do you mean any time the script prompts you for input, or just whenever you press q? If you mean every time the script asks you for input, you could write a function like:
QUIT () {
if [ $firstChoice = "q" ] || [ $secondChoice = "q" ]; then
exit 1
fi
}
Then just call the function right after very read call. like:
loop=1
while [ loop -eq "1" ]
echo "Type F for first choice"
read firstChoice
QUIT
echo "Type S for second choice"
read secondChoice
QUIT
if [ "$firstChoice" = "F" ]; then
echo "This is the first choice"
elif [ "$secondChoice" = "S" ]; then
echo "This is the second choice"
else
echo "no choice"
loop=0
exit 1
fi