LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-21-2011, 06:53 AM   #1
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
'advanced' if


hi all

i've got something to ask you guys, it's maybe a stupid mistake but i can't seem to figger out what's wrong with this :

Code:
if [ $ANYFILES -gt 0 ] && [ [ $VARERR -gt 0 ] || [ $TMP -gt 0 ] ]; then
	echo "test"
	exitstatus=$STATE_WARNING
else
	echo "test1"
	exitstatus=$STATE_OK
fi
(see the bold piece)
the names of the variables are correct, i double checked those

thanks in advance
 
Old 04-21-2011, 07:48 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,137

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Square brackets are not parens. Take a look at a bash manual to see the difference between () [] and [[]]. I think you want:
Code:
 if [[ $ANYFILES -gt 0 ]] && ( [[ $VARERR -gt 0 ]] || [[ $TMP -gt 0 ]] ); then
 
1 members found this post helpful.
Old 04-21-2011, 07:54 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
This is bash pitfall #11:
http://mywiki.wooledge.org/BashPitfa..._.3D_d_.5D_.5D

Use the bash [[ extended test command instead. Or even better, use the arithmetic operator, since this is a numeric test (pitfall #7).
Code:
if (( ANYFILES > 0 )) && (( VARERR > 0 || TMP > 0 )) ; then
     echo "test"
     exitstatus=$STATE_WARNING
else
     echo "test1"
     exitstatus=$STATE_OK
fi
Note that you don't need $ in front of the variable inside ((..)) brackets. They get expanded automatically.

Finally, see pitfall #22 about possible issues with the x && y || z pattern.
 
1 members found this post helpful.
Old 04-21-2011, 07:59 AM   #4
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416

Original Poster
Blog Entries: 2

Rep: Reputation: 12
Quote:
Originally Posted by smallpond View Post
Square brackets are not parens. Take a look at a bash manual to see the difference between () [] and [[]]. I think you want:
Code:
 if [[ $ANYFILES -gt 0 ]] && ( [[ $VARERR -gt 0 ]] || [[ $TMP -gt 0 ]] ); then
we have a winner

Code:
if [ $ANYFILES -gt 0 ] && ( [ $VARERR -gt 0 ] || [ $TMP -gt 0 ] ); then
	echo "Er zijn bestanden die ERROR bevatten of die eindigen op .tmp -> $VARERR erros en $TMP tmps"
	exitstatus=$STATE_WARNING
else
	echo "Gecontroleerd en niets aan de hand"
	exitstatus=$STATE_OK
fi
David the H. thanks for your suggestion but for some reason it didn't work for me... i changed al the [ to ( and deleted the $ and changed the -gt to > but it didn't work
But thanks anyways
 
Old 04-21-2011, 08:18 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I tested it exactly as I posted it and got the expected behavior.

What do you mean exactly by "it didn't work"? Could you post what you tried?
 
Old 04-21-2011, 09:10 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Remember that '[' is not interpreted as a paren by bash, it's actually a command (see man test for more info).

And the expression after "if" isn't special, it's just a command. If that command returns 0, then it's "true", otherwise it's "false".
 
Old 04-21-2011, 09:23 AM   #7
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416

Original Poster
Blog Entries: 2

Rep: Reputation: 12
Quote:
Originally Posted by David the H. View Post
I tested it exactly as I posted it and got the expected behavior.

What do you mean exactly by "it didn't work"? Could you post what you tried?
Sorry, i forgot something to delete

in stead of doing what you said, i did this:
Code:
  if ( ANYFILES > 0 ) && (( VARERR > 0  ) || ( TMP > 0 )); then
my bad it's also working with your solution !!

Thanks everybody !!!
 
Old 04-21-2011, 10:00 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
That won't work either but I am guessing it is just a typo ... your first test requires 2 sets of round brackets also, ie. (()).
Also, no real need to take the tests into separate brackets either:
Code:
if (( ANYFILES > 0 && ( VARERR > 0   ||  TMP > 0 ) )); then
 
Old 04-21-2011, 10:02 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I expected as much. Discovering just where the syntax errors lie is always the hardest part.

If you're satisfied with the solution, please mark the thread as solved.
 
  


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
[SOLVED] Now what? Advanced topics X.Cyclop Linux - General 1 10-04-2010 08:23 PM
advanced calculator Shaun32 Linux - Software 1 02-08-2006 09:17 PM
Linux Advanced chbin Slackware 11 03-17-2005 05:07 PM
Better buying "advanced linux prog" or "unix advanced prog" Dominik Programming 3 12-31-2003 01:11 PM
Advanced newbie zaphod_es LinuxQuestions.org Member Intro 3 06-23-2003 07:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:43 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
Open Source Consulting | Domain Registration