Loop difficulty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop difficulty
# 8  
Old 12-04-2010
You can consider the following code for education purpose, run and understand it you can then adapt it to your needs

Code:
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

Code:
#!/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"

This User Gave Thanks to ctsgnb For This Post:
# 9  
Old 12-05-2010
loop difficulty

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
# 10  
Old 12-05-2010
Try changing this line:

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

to this

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

# 11  
Old 12-05-2010
loop difficulty

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
# 12  
Old 12-05-2010
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):

Code:
#!/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"

This User Gave Thanks to Chubler_XL For This Post:
# 13  
Old 12-05-2010
loop difficulty

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
# 14  
Old 12-05-2010
I think posting #4 answers this question, this is a good example of how to do what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Difficulty with CAT redirection in script

I have not been able to append the contents of many files into one file. I have executed the CAT command shown below separately substituting an actual path and file name for the array variable to verify that I have the syntax correct. The bottom line - nothing is happening with CAT. I am running... (16 Replies)
Discussion started by: demmith
16 Replies

2. UNIX for Dummies Questions & Answers

Difficulty with set - command

Hi , i have a shell script with the first two lines as #! /bin/ksh set -x when i try opening the file from unix using the command 'sh filename.sh' , i am getting the below error : invalid optionline 2: set: - Pls help Use code tags for you code and data... (1 Reply)
Discussion started by: Rajankum
1 Replies

3. Shell Programming and Scripting

Difficulty in understanding sort command

Hi, I have used the following command to get result using some program and got desired output but I am unable to understand the last part: sort -k5,5 -rn|head -n 10 grep Centre groups.dat |sort -k5,5 -rn|head -n 10 Please guide. Thanks (2 Replies)
Discussion started by: bioinfo
2 Replies

4. Shell Programming and Scripting

difficulty with awk

hello folks, i am stuck with this awk command. i need to calculate the sum of a column of values on a flatfile and i am using the following command : awk -F"|" '{x += $10} END {print "Sum: "x}' standard_csv_file1.out that flatfile contains 180 fields and i am getting the... (5 Replies)
Discussion started by: jdsony
5 Replies

5. Shell Programming and Scripting

difficulty in formatting a file.

a file containing following data (part of it).... 1907594 201012 31 11 5837737 201012 41 18 257402.88 201101 31 11 7500 201101 33 1 115618.5 201101 41 11 556330 201102 31 12 481783.5 201102 41 20 2827732.13 201103 31 71 85253 201103 33 2 4479588.07 201103 41 90 7120 201104 21 1 ... (4 Replies)
Discussion started by: guptam
4 Replies

6. Shell Programming and Scripting

Perl Difficulty

Hi, I am trying to upload a file to a SQL database table. The column type is IMAGE. I am looking for a solution to upload a word doc file. I tried 3 approaches. 1) my $fileToStore = "mytest.doc"; open IPFILE, "<", $name; binmode IPFILE; while (<IPFILE>) { $fileToStore .= $_; } close... (1 Reply)
Discussion started by: b.paramanatti
1 Replies

7. UNIX for Dummies Questions & Answers

Reverse Proxy difficulty

Hi I am trying to set up two hosts in a reverse proxy. The reverse proxy already has 8 servers running perfectly, but they are all simply mapping pure addresses, which I have registered internally and externally. The latest two I wish to add are a bit different, they are app servers, one... (1 Reply)
Discussion started by: rboekdrukker
1 Replies

8. UNIX for Dummies Questions & Answers

Having difficulty with UNIX concept. Please help!

Hi, I would be very happy if someone could help me please. I am relatively new to UNIX, and still learning. My understanding of things are: Say I have a PC running Windows. This machine has a name. If I have 10 PC's, then I have 10 names, one for each PC. Each PC is independent of the other.... (4 Replies)
Discussion started by: ALon
4 Replies

9. Post Here to Contact Site Administrators and Moderators

Difficulty in Posting URLs in my threads

Hi, I have a problem in pasting a url in the thread. I tried many a times and i keep failing. Can u tell me what's wrong? I go by the instructions that appear after clicking the http:// but still i fail. The link does not appear properly in the threads that i post. Thanks, Nisha (3 Replies)
Discussion started by: Nisha
3 Replies
Login or Register to Ask a Question