problem with case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with case
# 1  
Old 03-14-2012
problem with case

hello everyone.. i want to make a file which, when u run it, it asks for your name and how many times you want to see your name displayed.. the program ever 10 times it will ask you if you want to continue and u have to answer with Y or N. if you type Y it continues displaying your name from where it stopped, if N it breaks.
for example i choose 45 times..
it will show
Name 1
Name 2
...
Name 10
Do you want to continue? Type Y/N
etc..
here is the code..
Code:
#!/bin/sh
echo "Hello user! What's your name?"
read name
echo "How many times do you want to see your name displayed?"
read times
i=1
while [ "$i" -le "$times" ]
do
echo $name $i
i=`expr $i + 1`
case $i in
1) echo "Do you want to continue? Give Y/N"
read ans 
if [ $ans = "Y" ] ; then 
continue
elif [ $ans = "N" ] ; then
break
else
echo You gave wrong parameter and the program is gonna be terminated.
fi ;;
esac
done

the program works fine except for the part of the question to continue or stop.. some help please?

---------- Post updated at 02:23 AM ---------- Previous update was at 02:21 AM ----------

i want to mention that i only created the code for the first 10 pack of displays..
# 2  
Old 03-14-2012
Your problem is that when you reach the desired count, the while loop exits. You need to wrap your while inside of an outer while and move your prompt outside of your inner loop.

Along the lines of:

Code:
Prompt for name
read name
prompt for count
read count
while count > 0                  # outer loop
do
   i = 10
   while i > 0 
   do 
       print name
       i = i - 1
   done

   prompt  "do you want to continue"
   read answer
   validate answer

   count = count - 1
done

This logic is a bit flawed inasmuch as it will prompt if count is reached and is a multiple of 10, but it should give you an idea.
This User Gave Thanks to agama For This Post:
# 3  
Old 03-14-2012
It's possible to add spaces at the front of your code to allow yourself to organize it logically.

Code:
#!/bin/sh

echo "Hello user! What's your name?"
read name
echo "How many times do you want to see your name displayed?"
read times
i=1

while [ "$i" -le "$times" ]
do
        echo $name $i
        i=`expr $i + 1`
        case $i in
        1)
                echo "Do you want to continue? Give Y/N"
                read ans 

                if [ $ans = "Y" ] ; then 
                        continue
                elif [ $ans = "N" ] ; then
                        break
                else
                        echo You gave wrong parameter and the program is gonna be terminated.
                fi ;;
        esac
done

The very first thing your program does is increment i, so that by the time you check it, it's skipped straight past 1 and gone to 2, so it never asks. So, check first, then add after the case statement.

As for stopping every 10 times, I see two obvious approaches.

1) Have two independent variables. One of them trips the case statement every 10 loops and drops back to 1 when it does. The other just gets incremented continually until the while statement becomes false.

2) Use the modulus ( % ) mathematical operator:

Code:
$ for X in 0 1 2 3 4 5 6 7 8 9 ; do expr $X % 5 ; done
0
1
2
3
4
0
1
2
3
4

$

It's the integer remainder when dividing. You can see it drops to zero whenever it divides evenly. You can use that to see whenever the number is an even multiple of something.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 03-14-2012
problem solved..
this is what i had to do..
Code:
#!/bin/sh
echo "Hello user! What's your name?"
read name
echo "How many times do you want to see your name displayed?"
read times
i=0
o=10
while [ "$i" -lt "$times" ]
do
echo $name
i=`expr $i + 1`
o=`expr $i % 10`
if [ $o = 0 ] ; then
    echo "Do you want to continue? Give Y/N"
    read ans 
    if [ "$ans" = "Y" ] ; then 
        continue
    elif [ "$ans" = "N" ] ; then
        break
    elif [ "$ans" = "" ] ; then
        echo "You gave wrong parameter. Do you want to continue? Give Y/N"
        read anss 
        if [ "$anss" = "Y" ] ; then 
            continue
        elif [ "$anss" = "N" ] ; then
            break
        elif [ "$ans" = "" ] ; then
            echo "You gave wrong parameter again and the program is going to be terminated."
            break
        else
            echo "You gave wrong parameter again and the program is going to be terminated."
            break
        fi
    else
        echo "You gave wrong parameter. Do you want to continue? Give Y/N"
        read ansss 
        if [ "$ansss" = "Y" ] ; then 
            continue
        elif [ "$ansss" = "N" ] ; then
            break
        elif [ "$ansss" = "" ] ; then
            echo "You gave wrong parameter again and the program is going to be terminated."
            break
        else
            echo "You gave wrong parameter again and the program is going to be terminated."
            break
        fi
    fi 
fi
done

# 5  
Old 03-15-2012
Instead of checking the same parameter 9 times, you can use another loop, within the loop, and check it once...

Code:
ANS=""
while true
do
        echo "Do you want to continue [Y/N]"
        read ANS
        case "$ANS" in
        [YN]) break ;;
        *)    echo "Answer $ANS not recognized"
                ;;
        esac
done

[ "$ANS" == "N" ] || break;

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 03-15-2012
Or a function:

Code:
#!/bin/sh
function yorn
{
    yn=""
    while [ "${yn}:" = ":" ]
    do
        printf "$1"
        read yn
        case $yn in
            [yY]|[Yy][Ee][Ss]) true;;
            [nN]|[Nn][Oo]) false;;
            *) printf "Invalid response - please enter Yes or No\n" ; yn=""
        esac
    done
}
printf "Hello user! What's your name? "
read name
printf "How many times do you want to see your name displayed? "
read times
i=0
o=10
while [ "$i" -lt "$times" ]
do
    echo $name
    let i=i+1
    let o=i%10
    [ $o -ne 0 ] || yorn "Do you want to continue? " || break
done

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 03-15-2012
Functions can't be depended on in sh. bash and ksh have them, but a plain bourne sh wouldn't.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case execution problem

hi guys....in my code the echo statements inside case statements are not printing the required display info....pls help me out #!/bin/bash tput clear tput cup 3 15 tput setaf 3 echo "BACKUP PROJECT" tput sgr0 tput cup 5 17 tput rev echo "select option" tput sgr0 tput cup 7 15 echo... (12 Replies)
Discussion started by: diw10
12 Replies

2. Shell Programming and Scripting

Problem using bash case statement

I have the following bash script and it is not accepting the lines "--"|"--""-") "--""-"") while do echo "Current Argument is ${1}" case "$1" in "--"|"--""-") echo "Argument is ${1}" shift # Skip ahead one to the next argument. ... (1 Reply)
Discussion started by: kristinu
1 Replies

3. Shell Programming and Scripting

Problem using 'Case' statement

Hi, When I execute the below script, I am getting the error as ' is not expected.ror at line 3 : `in #!/bin/sh case $1 in -r) echo Force deletion without confirmation ;; -i) echo Confirm before deleting ;; *) echo Unknown argument ;; esac I could not see any problem with... (1 Reply)
Discussion started by: Sathish_Obla
1 Replies

4. Shell Programming and Scripting

Problem with CASE statement

Guys, Here is the script syntax which is not accepting the parameters & not performing the said activity. $ ./routing.sh xyz123-ra str enable ********************************************************************** Preparing to service the request for Device xyz123-ra in Question... (9 Replies)
Discussion started by: raghunsi
9 Replies

5. Shell Programming and Scripting

Problem with case statement

Hi, I need modify the all lines in a file into one format. cat file htec.twe34c.ATI .hesh.twdghu..ATI ..hesh.twdghu..ATI htec.twe3 hjsct14567ati Output should have 16 characters htectwe34c ATI heshtwdghu ATI heshtwdghu ATI htectwe3 ATI hjsct14567 ATI (4 Replies)
Discussion started by: kartheek
4 Replies

6. Shell Programming and Scripting

Problem with my case statements

Hi there, Im having some problems with this function, I pass two arguments to the function $1 $2 (Arguments are month and date inputted by the user) for some reason the case always fails... however in the cases defined below where it shouldnt fail the result is: if it fails with input... (6 Replies)
Discussion started by: Darklight
6 Replies

7. Shell Programming and Scripting

problem with CASE pattern matching

I am using ksh on a HP Ux. I have a simple script but am having problem with the case statement:- #!/usr/bin/sh Chl=”SM.APPLE_SWIFT_DV” LoConfirm=”” case $chl in ) LoConfirm=”Using channel at Building 1” echo “test conditon1” echo $LoConfirm;; ) LoConfirm=”Using... (2 Replies)
Discussion started by: gummysweets
2 Replies

8. UNIX for Advanced & Expert Users

Case statement problem

I need to display this menu and accept variables. Can someone tell me why i am having a problem with this case statement, please # TAPE MANAGER MAIN MENU tapemgr_Main_Menu() { echo "Legato Tape Management System Menu" echo " This system is used to report Legato ERV Offsite and Tapes... (6 Replies)
Discussion started by: gzs553
6 Replies

9. Shell Programming and Scripting

ksh case problem

I'm trying to run a ksh script with a case condition to handle parameters. #!/bin/ksh db_start(){ *code } db_shut(){ *code } case "$1" in up) db_start TRNG ;; down) db_shut TRNG ;; *) echo "Usage: $0 { up | down }" (3 Replies)
Discussion started by: digiteck
3 Replies

10. UNIX for Dummies Questions & Answers

Problem w. case structure

Hello, I am having a problem setting a range of numbers for the "case" structure. I can use with no problems, but when I use it doesn't work??? Does the case struture allow numeric ranges? eg: echo -e "enter number between 0 and 60: \c" read $answer case $answer in ) echo... (2 Replies)
Discussion started by: Joe54321
2 Replies
Login or Register to Ask a Question