while - loop does not work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while - loop does not work
# 1  
Old 03-24-2010
Tools while - loop does not work

Hey guys,

maybe you can help me.. what am i doing wrong?
Why this doesn`t work in ksh.

Code:
        while [ "${decision}" != "y"  ||  "${decision}" != "n" ]
        do
        echo "Type y or n"
        read decision
                if [ "${decision}" = "y" ]
                then
                doInstall
                fi
        done

i also tried this

Code:
        while [ "${decision}" != "y" ] || [ "${decision}" != "n" ]
        do
        echo "Type y or n"
        read decision
                if [ "${decision}" = "y" ]
                then
                doInstall
                fi
        done

I want the loop to be endet as soon as the user types in y or n.
But it just doesnt end.

Thanks a lot in advance.

Regards
xcitan
# 2  
Old 03-24-2010
Code:
#!/bin/ksh
while true
        do
        echo "Type y or n"
        read decision
                if [[ "${decision}" = "y" ]]
                then
                  echo doInstall
                else
                  exit
                fi
        done

# 3  
Old 03-24-2010
try with the following
Code:
        while [ "${decision}" != "y"  -o  "${decision}" != "n" ]
        do
        echo "Type y or n"
        read decision
                if [ "${decision}" = "y" ]
                then
                echo "execute it"
                                exit 0
                                else
                                        continue
                fi
        done

# 4  
Old 03-24-2010
hi malcom,

thank you for your answer, but that is not what i am looking for.
May you tell me if something like this works in a while loop?
Code:
while [ "${decision}" != "y" ] || [ "${decision}" != "n" ]

Regards
xcitan
# 5  
Old 03-24-2010
Quote:
Originally Posted by xcitan
hi malcom,

thank you for your answer, but that is not what i am looking for.
May you tell me if something like this works in a while loop?
Code:
while [ "${decision}" != "y" ] || [ "${decision}" != "n" ]

Regards
xcitan
check what expert suggested you above.
# 6  
Old 03-24-2010
Hi Expert,

am i right, that -o means or?!

Code:
  while [ "${decision}" != "y"  -o  "${decision}" != "n" ]

I dont know why, but although i type in y or n the while loop doesn`t end. *grml*
I even check for it and i can see that
Code:
${decision}

is y or n.

Thanks for your help.
xcitan
# 7  
Old 03-24-2010
ofcourse, the while loop will not end because it always return true.
Assume, you entered "n" after it asks you to type y or n.
Then it compares that if decision is not "y", it returns true because decision is "n"(not y) and it checks the next one if decision is not "n", it returns false because it is "n". So either way, you entered y or n, it returns true because

Quote:
True OR True = True
True OR False = True
False OR True = True
False OR False = False
i hope you get what i mean.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why doesnt if inside a while loop work ?

I am looping through a file in bash and performing some operations on it. Here is the code. cat test.dat - One Two Three Case 1: With if inside while loop Output: One file found :) ------------------ isn't it supposed to print following output !!! one (3 Replies)
Discussion started by: qwarentine
3 Replies

2. Shell Programming and Scripting

Script doesn't work in loop but does if not

I have a script that only works if I remove it from the looping scenario. #!/bin/bash # Set the field seperator to a newline ##IFS=" ##" # Loop through the file ##for line in `cat nlist.txt`;do # put the line into a variable. ##dbuser=$line echo "copying plugin..." ... (6 Replies)
Discussion started by: bugeye
6 Replies

3. Shell Programming and Scripting

Why does my for loop does not work right in ksh?

hi all, i have a for loop statement in my ksh code. it only returns the first value retrieved not the value for the other rows. ex: acct_id value = returned value in the for loop 1 = 1 2 = 1 (instead of 2) 3 = ... (1 Reply)
Discussion started by: ryukishin_17
1 Replies

4. Shell Programming and Scripting

tail -XXX with grep doesn't work in while loop

Hi all, I need some help. my shell script doesn't work especially in the loop. #!/bin/sh -xv export ORA_ADMIN=/oracle/home/admin export ORACLE_SID=ORA_SID cat ${ORA_ADMIN}/param_alert_log.ora | while read MSG do #echo $MSG #echo "tail -400... (8 Replies)
Discussion started by: sidobre
8 Replies

5. Shell Programming and Scripting

loop doesnt work

It just does the break...even though the files are not the same... # Compare extracts #========================================== count=0 while (( count < 5 )) do (( count+=1 )) echo "Try $count" file1=$(ls -l /tmp/psjava.xml|... (5 Replies)
Discussion started by: sigh2010
5 Replies

6. UNIX for Dummies Questions & Answers

Top level TCSH while Loop doen't work

Hey guys... I'm learning some shell scripting on OS X using the tcsh shell. For some reason... my while loop isn't executing right (or more likely I am doing something wrong.) Something as simple as this doesn't work: #!/bin/tcsh set g = 0 while ($g <10) echo "this" $g @ g =... (2 Replies)
Discussion started by: sprynmr
2 Replies

7. Shell Programming and Scripting

for loop doesn't work

I have a script which uses below for loop: for (( i = 0 ; i <= 5; i++ )) do echo "Welcome $i times" done But when I run the script, it gives error message: Syntex Error : Bad for loop variable Can anyone guide to run it? Thanks in advance. (10 Replies)
Discussion started by: naw_deepak
10 Replies

8. Shell Programming and Scripting

Work CSV file with awk loop

I need to converts a CSV file from Format "A" into Format "B", where the first field is repeated in a loop with a counter. I'am quite sure that this is with AWK possible burt the "Forum Search Function" doesn't work at the moment. Format "A" A1;B1 ;B2 ;B3 A2;C1 ;C2 ;C3 ... (2 Replies)
Discussion started by: frieling
2 Replies

9. UNIX for Dummies Questions & Answers

The loop that dosent work

I have a serious problem and Im sure im just doing something stupid ######Start Load while do /export/home/mldwh/rkem_refresh/int_load/scripts/rkem_refresh.sh sleep 10 while do sleep 5 done done ######Log and Runstats sleep... (12 Replies)
Discussion started by: jadionne
12 Replies

10. UNIX for Dummies Questions & Answers

Why script For...Loop doesn't work. Seek help

I have written a script to run on UNIX server. When I tested, it always hanged on after "date +"%D %T: XXXXXX script started." part. Then it wouldn't go further. UNIX server gave me one error message. I used the same code in another script. It works fine. I think the major problem may be in... (3 Replies)
Discussion started by: duke0001
3 Replies
Login or Register to Ask a Question