0

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
4

1 Answer 1

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .