I want to remain in the script until user passes the correct name.
I had tried the below code; but it didn't work out.
Please help
echo "\nPlease enter the source system: \c"
while read SYSTEM_NAME
do
if [[ $SYSTEM_NAME == "L1" || $SYSTEM_NAME == "L2" || $SYSTEM_NAME == "P1" || $SYSTEM_NAME == "P2 ]];
then
echo "\nMaking $SYSTEM_NAME as source system for particular script"
else
echo "\nPlease enter the source system as L1 L2 P1 P2"
echo "\nPlease enter the product name FUNDDB/SMF: \c"
fi
done
In your code, you'd have to break out of the loop when the test passes. Or, do the test directly in while, then read at the end of loop:
#!/bin/bash
echo "Please enter the source system: "
read SYSTEM_NAME
while ! [[ $SYSTEM_NAME == "L1" || $SYSTEM_NAME == "L2" || $SYSTEM_NAME == "P1" || $SYSTEM_NAME == "P2" ]] ; do
echo "\nPlease enter the source system as L1 L2 P1 P2"
read SYSTEM_NAME
done
echo "Making $SYSTEM_NAME as source system for particular script"
#continue with $SYSTEM_NAME
Easier to read with a case statement. Note the use of "break" to get out of "while read". Not sure where the "Please enter product" line should go.
echo "\nPlease enter the source system: \c"
while read SYSTEM_NAME
do
case "${SYSTEM_NAME}" in
"L1"|"L2"|"P1"|"P2")
echo "\nMaking $SYSTEM_NAME as source system for particular script"
break
;;
esac
echo "\nPlease enter the source system as L1 L2 P1 P2"
done
echo "\nPlease enter the product name FUNDDB/SMF: \c"