LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Suse/Novell
User Name
Password
Suse/Novell This Forum is for the discussion of Suse Linux.

Notices

Reply
 
Search this Thread
Old 02-09-2012, 09:15 AM   #1
s_linux
Member
 
Registered: Jul 2009
Posts: 80

Rep: Reputation: 15
multiple expression if statement


Hello All,
I wanted to use multiple expressions in if statements and checking to see if my command syntax is right.

condition is if filesize greterthan 3mb and fileid not equal to 1234&2345.
fileid is basically file. I wanted to see if any of the file size is more than 3mb except for those two files.

command is

Quote:
if [ "${size}" -ge "3145728" -a "${fileID}" -ne "1234" ];then
if [ "${size}" -ge "3145728" -a "${fileID}" -ne "2345" ] ;then
Any help appreciated.

Thanks
 
Old 02-09-2012, 09:33 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
as long as you are on bash, use the [[ notation, not the older, clunkier [, it's better in many many ways:

if [[ 1 -eq 1 && 3 -eq 3 ]]; then echo yes; fi
 
Old 02-11-2012, 05:49 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
What acid_kewpie said. The old brackets are not really designed for complex evaluations. If you must use them, however, use multiple individual tests instead of -a/-o.

Code:
if [ "$size" -ge 3145728 ] && { [ "$fileID" -ne 1234 ] || [ "$fileID" -ne 2345 ]; }; then
The newer double-bracket test is much more robust and convenient when it comes to complex conditions. You can more easily do expression grouping, for one thing.
Code:
if [[ "$size" -ge 3145728 && ( "$fileID" -ne 1234 || "$fileID" -ne 2345 ) ]]; then
But actually, when you're doing integer comparisons like this, arithmetic evaluation brackets are even better.

Code:
if (( size >= 3145728 && ( fileID != 1234 || fileID != 2345 ) )); then
This still may not be the best solution, though. It depends on how you're generating the input list of course, but I would personally see if I could eliminate one of the conditions from the input itself during that or some other earlier step, if possible.

Last edited by David the H.; 02-11-2012 at 05:50 AM. Reason: minor corrections
 
Old 02-11-2012, 06:21 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
Quote:
Originally Posted by David the H. View Post
The newer double-bracket test is much more robust and convenient when it comes to complex conditions. You can more easily do expression grouping, for one thing.
Code:
if [[ "$size" -ge 3145728 && ( "$fileID" -ne 1234 || "$fileID" -ne 2345 ) ]]; then
Also there is no need for quote marks as bash deals with variable substitution itself, rather than leaving until after literal string replacement and globbing occurs.
 
Old 02-11-2012, 08:02 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
True, one of the benefits of the new test is that there's no word-splitting done on variable expansions. But it never hurts to keep up the practice anyway, even when not strictly necessary. Places like this where it's safe to leave them off are relatively rare.
 
  


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
Regular Expression in IF statement not working in ksh Pinglu Linux - Newbie 2 11-01-2011 09:56 AM
[SOLVED] if statement with multiple tasks ted_chou12 Linux - Newbie 3 02-02-2011 06:59 PM
[SOLVED] perl regular expression:: take decimal out of statement. knockout_artist Linux - Newbie 4 06-23-2010 02:17 PM
Regular expression doesnt work in an elsif statement? oinker Programming 2 02-04-2010 01:33 PM
How to do multiple comaprison in a IF statement pdklinux79 Linux - Newbie 4 06-11-2008 10:34 PM


All times are GMT -5. The time now is 12:18 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration