0
#!/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.

3
  • The 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 plain else instead. Also, your quoting reflexes are a bit backward; generally, static single-word strings without any special characters (like true and false) don't need to be quoted, but variable references like $var should be double-quoted to avoid weird parsing if they do contain weird characters. Commented Oct 19, 2021 at 19:14
  • @GordonDavisson : $var nees to be quoted only in the while line. It does not need to be quoted later, because we are inside [[ ... ]]. Commented Oct 20, 2021 at 7:25
  • @user1934428 True. I was going to point that out, but ran into the comment size limit. The reason I commented was that quoting simple fixed strings but not variable references suggests a basic misunderstanding of what quotes do in shell syntax. BTW, not quoting isn't always safe even in [[ ]] -- in [[ $var1 = $var2 ]], $var2 will 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. Commented Oct 20, 2021 at 7:58

3 Answers 3

2

This would do the same with a more academic syntax:

i=0
while :; do
  printf 'Enter value (true/false): '
  read -r var
  case $var in
    true)
      i=$((i + 1))
      printf 'Iteration : %d\n' $i
      ;;
    false)
      printf 'Exiting the process\n'
      break
      ;;
    *)
      printf 'Invalid Choice.\nAvaialable Choices are true or false\n'
      ;;
  esac
done
Sign up to request clarification or add additional context in comments.

Comments

2

You might find this to be a cleaner solution:

i=0
while true; do
  read -p "enter value: " myinput
  if [[ $myinput = true ]]; then
    echo "iteration $i"
    i=$((i+1))
  elif [[ $myinput = false ]]; then
    echo "exiting"
    exit
  else
    echo "invalid input"
  fi;
done;

The issue I see with your current code is that it is unclear which command's exit status $? refers to. Does it refer to the echo in the previous elif block? Or the last condition check? Or something else entirely?

Comments

1

I'm new in bash. I tried that:

#!/bin/bash
i=1
while [[ $var != "false" ]]
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"
    fi
done

I changed while $var with while [[ $var ]] because while works like if. It runs the given command. In there it is $var's value.

And I moved exit to first elif expression's end. So if user type false program will exit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.