#!/bin/bash
var="true"
i=1
while $var
do
read -p "Enter value (true/false): " var
if [[ $var == "true" ]]
then
echo "Iteration : $i"
((i++))
elif [[ $var == "false" ]]
then
echo "Exiting the process"
elif [[ $? -eq 1 ]]
then
echo "Invalid Choice."
echo "Avaialable Choices are true or false"
exit
fi
done
Script is Working Fine. I Enter true the loop will iterate for false the script stops. I want the script will continue asking "Enter Value" if any other value instead of true or false will be entered.
elif [[ $? -eq 1 ]]isn't doing anything useful -- the$?it's checking is the exit status of the last command, which is[[ $var == "false" ]], which clearly did fail or it wouldn't have gotten to this point. Just use a plainelseinstead. Also, your quoting reflexes are a bit backward; generally, static single-word strings without any special characters (liketrueandfalse) don't need to be quoted, but variable references like$varshould be double-quoted to avoid weird parsing if they do contain weird characters.$varnees to be quoted only in thewhileline. It does not need to be quoted later, because we are inside[[ ... ]].[[ ]]-- in[[ $var1 = $var2 ]],$var2will be treated as a glob pattern unless it's double-quoted. The rules for when it's safe to leave off double-quotes are complex enough that I generally recommend double-quoting all var references unless there's a specific reason not to.