Loop difficulty

hi all
I am new to unix and want to create a loop to repeat the loop and before that it ask me to do so.I know "while" may help but I put it in my work and getting stuk with it.any help appreciated.

What did you try so far ?
Pls post your code, so we can help you to troubleshoot it then

1 Like

thanks for reply.here is my work.

while

PS3="Choose your favourite flower from these possibilities: "

select FLOWER in rose sunflower cornflower lily
if [[ -z "$FLOWER" ]]
then
echo "You typed a wrong figure."
else
echo "You choose $FLOWER as your favourite."
fi
break
echo "repeat?"
read var
if [ "$var" = "no" ]
then
echo "you choosed to exit"
break
else
fi
done

read var

This is an example (i did intentionally kept as closed to your code as i could)
$REPLY is the build-in variable that works with select case statement (ksh)

echo "Flower choice:"
PS3="Choose your favourite flower from these possibilities: "
select FLOWER in rose sunflower cornflower lily
do
        case $REPLY in
                1|2|3|4) echo "You have choosen $FLOWER as your favourite." ;;
                *) echo "You typed a wrong figure.";;
        esac
echo "repeat?"
read var
if [[ -z "$var" || "$var" = "no" ]]
then
        echo "you choosed to exit"
        break
fi
done
1 Like

thanks very much for your help.It worked perfect.

other question if I want to make it accepts two or more variables without entering the number(just gives an option to entre three variable names),I mean entering values and testing them that are from the existence name.how can I do that?
regards

REPLY do work with select statement but it will point to the increment number of the chosen value (as is appears in the menu built by the select).

To understand, you can run this code :

echo "Flower choice:"
PS3="Choose your favourite flower from these possibilities: "
select FLOWER in rose sunflower cornflower lily
do
echo "REPLY=$REPLY FLOWER=$FLOWER"
[[ -z "$FLOWER" ]] && break
done
1 Like

ctsgnb! you so kind and helped me alot to understand more unix.
I would like to ask if we have e.g 7 or less names and we want to select three names of them in one go and test them that are included in our pre-created names(for example they are file names),what is the best solution?I have used IF statement but it seems very long statement as I had to test it for each name (for example each variable should not be blank or other things).my question is do we need to check each variable or there is simple way exist?
for example:
echo "benz bmw gms nissan polo"
echo "select four names out these names:\c"
read name1 name2 name3
while [[ $name1 = "" ]]
do
echo "no entry"
read name1 name2 name3

    while [[ $name2 = "" ]]
    do
    echo "no entry"

    echo "Please enter your choices: \\c"
    
    read name1 name2 name3

    while [[ $name3 = "" ]]
do
echo "no entry"
echo "please enter your choices:\\c"
read name1 name2 name3

    if [[ -f $name1 ]]
    then
    echo "$name1"
    else
    echo "$name1 does not exist"
    fi

    if [[ -f $name2 ]]
    then
    echo "$name2"
    else
    echo "$name2 does not exist"
    fi

    if [[ -f $name3 ]]
    then
    echo "$name3" 
    else
    echo "$name3 does not exist"
    fi
   done

It would be so appreciated if you can see where I am in wrong place?
Regards

You can consider the following code for education purpose, run and understand it you can then adapt it to your needs

i=1
while [ $i -le 3 ]
do
        echo "Valid entries are : benz bmw gms nissan polo"
        echo "Enter your choice $i / 3 :"
        read n

        case $n in
                benz|bmw|polo) country=DE;;
                nissan) country=JP ;;
                gms) country=USA ;;
                *) echo "wrong entry" ; let i-- ;;
        esac

        eval n$i=$n\' from \'$country
        let i++
done
echo "you have chosen :
-> $n1
-> $n2
-> $n3"

---------- Post updated at 11:21 PM ---------- Previous update was at 06:38 PM ----------

Another one with an example of 'infinite loop' and how to break it as well as a sample of pattern matching (this part of code that does the check about empty parameter is for education purpose only because in fact it could removed from the script since the parameter are further checked into the case statement

#!/bin/ksh
while :
do
        echo "Valid entries are : benz bmw gms nissan polo"
        echo "Please enter 3 of them (separated by a space) :"
        read a b c
        #short check
        if [[ ":$a:$b:$c:" = +(*::*) ]] ; then
                echo "At least on of the parameter is empty, try again"
        else

        # extended check
        for i in $a $b $c
        do
                case $i in
                        "benz"|bmw|gms|nissan|polo) : ;;
                        *) echo "ERROR : $i entry is not valid" ;;
                esac
        done
        break

        fi
done
echo "you have chosen :
-> $a
-> $b
-> $c"
1 Like

hello gain,
the problem still exists if I enter value rather than those we have introduced for it (five values).for example if I enter " car bike train " it accepts them as entries and goes to next step.
do you have any idea why it does accept such wrong entries and how to sort it out?
regards

Try changing this line:

*) echo "ERROR : $i entry is not valid" ;;

to this

*) echo "ERROR : $i entry is not valid" ; continue 2;;

thanks for reply.now with this line it will exit and not prompting again for entries.I want it to prompt me again to enter values.
my problem is I want to select three choices out of five pre exist names.I have used "while and until" loop but no success.
any help appreciated.
thanx

I'll show the whole code because I suspect you didn't have the same as ctsgnb's 2nd post (change from original is in green below):

#!/bin/ksh
while :
do
        echo "Valid entries are : benz bmw gms nissan polo"
        echo "Please enter 3 of them (separated by a space) :"
        read a b c
        #short check
        if [[ ":$a:$b:$c:" = +(*::*) ]] ; then
                echo "At least on of the parameter is empty, try again"
        else
 
        # extended check
        for i in $a $b $c
        do
                case $i in
                        "benz"|bmw|gms|nissan|polo) : ;;
                        *) echo "ERROR : $i entry is not valid" ; continue 2 ;;
                esac
        done
        break
 
        fi
done
echo "you have chosen :
-> $a
-> $b
-> $c"
1 Like

that was really cool.it worked now.
another question ,if we have some names (five ) and we want to display four names as a menu and let us then to select only one(out of these three) by using the order number and not using the names.which command do I need to use?I have used:
case in
lablabala
but I had to check for each variable individually and it seems a bit tricky.
any idea?.
Regards

I think posting #4 answers this question, this is a good example of how to do what you want.