LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-21-2009, 10:27 AM   #1
dz-015
LQ Newbie
 
Registered: Apr 2009
Location: UK
Distribution: Red Hat Enterprise Linux Server release 5.2 (Tikanga) / Fedora 10 KDE Desktop
Posts: 6

Rep: Reputation: 0
Bash syntax help: WHILE loop with multiple comparison operators


Hi,

I'm trying to code the following WHILE loop in Bash:

While [string1 is non zero] and [string2 is non zero] and [counter < max]
Do
...
Done

I can't get the syntax right. I've tried:

Code:
while [ -n "$STRING1"] -a [ -n "$STRING2"] -a [$COUNTER -lt $MAX ]
But I get 'Too may arguments'

Can anyone help?

Thanks,

DZ
 
Old 04-21-2009, 10:54 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Too many square brackets!
Code:
while [ -n "$STRING1" -a -n "$STRING2" -a $COUNTER -lt $MAX ]
do
  commands
done
 
Old 04-21-2009, 10:57 AM   #3
kentyler
Member
 
Registered: Dec 2008
Location: Newark Ohio
Distribution: Fedora Core
Posts: 270

Rep: Reputation: 37
Try like:

while [ -n "$STRING1" && -a -n "$STRING2" && -a $COUNTER && -lt $MAX ]
 
Old 04-21-2009, 11:04 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Quote:
Originally Posted by kentyler View Post
Try like:

while [ -n "$STRING1" && -a -n "$STRING2" && -a $COUNTER && -lt $MAX ]
Nope. && and -a would have the same meaning. It should be
Code:
while [ -n "$STRING1" ] && [ -n "$STRING2" ] && [ $COUNTER -lt $MAX ]
but I prefer the -a operator.
Code:
while [[ -n "$STRING1"  &&  -n "$STRING2"  &&  $COUNTER -lt $MAX ]]
also works.

Last edited by colucix; 04-21-2009 at 11:06 AM.
 
Old 04-21-2009, 11:13 AM   #5
dz-015
LQ Newbie
 
Registered: Apr 2009
Location: UK
Distribution: Red Hat Enterprise Linux Server release 5.2 (Tikanga) / Fedora 10 KDE Desktop
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks guys.
Shell scripts are powerful, but the syntax takes some getting used to!!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
string comparison in for loop harsshal Linux - Software 15 03-03-2008 11:39 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
? on logical operators in BASH eroica Linux - Software 10 02-01-2005 01:12 PM
C++, comparison operators, segfault exodist Programming 1 04-24-2004 03:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration