issues with a condition in a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting issues with a condition in a while loop
# 1  
Old 12-02-2008
Network issues with a condition in a while loop

Hi,

I am facing issues with test condition. I had a compound condition to write for both if and while,

In one of the texts i referred, with a korn shell we can write compound statements like: [[condition1 || condition 2 ]], however this doesn't worked for me. For example:

if [[ "$go_2_it" == "Y" || "$go_2_it" == "y" ]] doesn't works, but
if [ "$go_2_it" = "Y" ] || [ "$go_2_it" = "y" ] worked.

Now, similarly in a while loop I have something like,

while [[ "$go_2_it" != "Y" || "$go_2_it"t != "y" || "$go_2_it" != "N" || "$go_2_it" != "n" ]] or

while [ "$go_2_it" != "Y" ] || [ "$go_2_it" != "y" ] || [ "$go_2_it" != "N" ] || [ "$go_2_it" != "n" ]

None of the above works, do not understand why.

Also, if anybody can suggest whats [[ ]], and does it behaves differently than []. In the text, I referred, it says [[]] is for korn and [] is for bash. I am working with korn shell, but [[]] doesn't help me.

Thanks
# 2  
Old 12-02-2008
Hello,
it may not be much of a comfort but both Your versions of while loop conditions worked on my machine, in both bash (3.2) and ksh (93s) on Linux.

As in
Code:
while [[ "$go_2_it" != "Y"  ||  "$go_2_it" != "y"  ||  "$go_2_it" != "N"  ||  "$go_2_it" != "n" ]];  do echo hey; done

or
Code:
while [ "$go_2_it" != "Y" ] || [ "$go_2_it" != "y" ] || [ "$go_2_it" != "N" ] || [ "$go_2_it" != "n" ];  do echo hey; done

I am not sure but I think the single [ is actually a command, equal or similar to the test command, but [[ is a grouping mechanism and has different rules for wildcard and variable expansion. I guess You should Google on it. I have used both and not sure why I did it, maybe just because an example I had to build from used either and it worked so I never really bothered.

/Lakris
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While Loop with if else condition

Hi, I was trying to write a shell script which reads csv file and sends mail in html format along with tables. Hope i have completed 1st part , but while sending mail i was trying to highlight some rows in the table based on the egrep outcome. If the string exists in line/INPUT, i am trying to... (4 Replies)
Discussion started by: varmas424
4 Replies

2. UNIX for Advanced & Expert Users

While loop only if a condition is met

All, I wrote the following section of code (which logically in PHP would of worked): tmpPATH=${1} tmpTAG=${2} if then while read tmpTAG tmpPATH do fi echo $tmpTAG echo $tmpPATH if then done < ./config.cfg fi (4 Replies)
Discussion started by: Cranie
4 Replies

3. Shell Programming and Scripting

Use of -z in while loop condition

Hi, Could you please tell what is the meaning of -z in while loop condition. For example, while ; do echo "*** Enter the age " readage (3 Replies)
Discussion started by: vidyaj
3 Replies

4. Shell Programming and Scripting

if condition in a while loop

Gurus, I need to read a line from a file and strip the characters from it and compare the stripped value with the value I pass to the script while executing it. Below is the code for the same. But when i execute the code, it is throwing an error. #!/bin/ksh . /home/.i_env ... (14 Replies)
Discussion started by: svajhala
14 Replies

5. Programming

performance issues of calling a function in if condition

Hi, I have written a program in C and have to test the return value of the functions. So the normal way of doin this wud b int rc rc=myfunction(input); if(rc=TRUE){ } else{ } But instead of doing this I have called the function in the if() condition. Does this have any... (2 Replies)
Discussion started by: sidmania
2 Replies

6. Shell Programming and Scripting

condition inside a for loop

I have a for loop in my script as shown below. for file_path in $file_list ; do ........my code .......... ...... done Can i restrict the number of files parsing to the variable file_path as 50? That is, even if I have some 100 files in file_list, I need to take only 50 files for... (7 Replies)
Discussion started by: Vijay06
7 Replies

7. UNIX for Dummies Questions & Answers

Testing For Loop condition

How do I test the return condition in the script if no files are found: for file in `Find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN}` do ... .. done I want to capture the return code so I can echo the error or condition. Using if ] always returns zero no matter where it's placed. ... (4 Replies)
Discussion started by: mavsman
4 Replies

8. UNIX for Dummies Questions & Answers

What condition to be put in the while loop?

i have got a file where the env command is appended 5 times. i have to now look for the username and display it in the form of 1) PWD=/home/lee.ballancore 2) USER=lee.ballancore 3) MAIL=/var/spool/mail/lee.ballancore 4) LOGNAME=lee.ballancore 5) HOME=/home/lee.ballancore 6)... (1 Reply)
Discussion started by: nehaquick
1 Replies

9. Shell Programming and Scripting

while loop issues

Hi guys, Been sifting through these forums for awhile, but never had an account or needed to post. Shoutouts to a great forum with heaps of useful info. Now i consider myself a noob when it comes to linux and to bash scripting. I recently started to learn to use Vmware ESX server which uses... (4 Replies)
Discussion started by: ryath
4 Replies
Login or Register to Ask a Question