Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What I want is as follows:

At first when user runs .sh file it displays following:

Review id:
You id is:XXX000YYY
Do you want to change it?[Press Y to change, C to continue]:

Now If user presses Y then let him change id and again show this again, and do this until user presses C to continue. How can I do this in shell script?


I tried following but don't know what to write in if conditions???

while :
do
  echo "Enter id:"
  read line
  echo "Your id: $line"
  echo "Do you want to change(Y to change, C to conntinue)?"
  read ans
  if [ $ans = C ] || [ $ans = c ]; then
  /// .... finish the loop
  elif [ $ans = y ] || [ $ans = Y ]; then
  /// .... continue while loop
  else
  echo "Wrong selection."
  /// .... continue while loop
  fi
exit 0

I changed it to following, but now it's in infinite loop:

echo "Enter id:"
read line
echo "Your id: $line"
echo "Do you want to change(Y to change, C to conntinue)?"
read ans
while [ $ans = y ] || [ $ans = Y ];:
do
  echo "Enter id:"
  read line
  echo "Your id: $line"
  echo "Do you want to change(Y to change, C to conntinue)?"
  read ans
done
share|improve this question
1  
The Advanced BASH Scripting Guide can help you decipher where to start –  uSlackr Feb 9 '12 at 4:29
    
Where did he wrote condition in while statement? –  Harry Joy Feb 9 '12 at 4:37
    
I have updated question with my try. Can you help me in what to write in if part? –  Harry Joy Feb 9 '12 at 4:57
1  
This maybe a better question for unix.stackexchange.com –  chrisjlee Feb 9 '12 at 6:02
add comment

1 Answer 1

up vote 0 down vote accepted

The problem lies in this line:

while [ $ans = y ] || [ $ans = Y ];:

which should be

while [ $ans = y ] || [ $ans = Y ];

The issue is that the colon is a bash built-in that gets interpereted as true. More information at What Is the Purpose of the `:' (colon) GNU Bash Builtin?.

The following code works for me

#!/bin/env bash
echo "Enter id:"
read line
echo "Your id: $line"
echo "Do you want to change(Y to change, C to conntinue)?"
read ans
while [ $ans == y ] || [ $ans == Y ] ;
do
echo "ANS: ${ans}"
  echo "Enter id:"
  read line
  echo "Your id: $line"
  echo "Do you want to change(Y to change, C to conntinue)?"
  read ans
done

Note: it's always better to include a shebang at the top of your scripts.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.