Using && in if statement with 3 expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using && in if statement with 3 expressions
# 1  
Old 01-02-2013
Using && in if statement with 3 expressions

how do you write an if statement for something like
Code:
if ((expr 1 >= expr 2 && expr 3 >= expr 4) && expr 5 <= expr 6)

if ((TRUE && TRUE) && TRUE) then
condition...

i've done it this way but it doesn't seem to work.
Code:
if ([[ "$ex_year" -ge "$curr_year"  && "$ex_month" -ge "$curr_month" ]]  && "$ex_day" -le "$curr_day" ); then
       condition...

# 2  
Old 01-02-2013
Code:
if ([ $ex_year -ge $curr_year ] && [ $ex_month -ge $curr_month ]) && [ $ex_day -le $curr_day ]

# 3  
Old 01-02-2013
Quote:
Originally Posted by angilulu
how do you write an if statement for something like
Code:
if ((expr 1 >= expr 2 && expr 3 >= expr 4) && expr 5 <= expr 6)

if ((TRUE && TRUE) && TRUE) then
condition...

i've done it this way but it doesn't seem to work.
Code:
if ([[ "$ex_year" -ge "$curr_year"  && "$ex_month" -ge "$curr_month" ]]  && "$ex_day" -le "$curr_day" ); then
       condition...

You don't say what shell you're using and it makes a big difference. With a shell conforming to the standards, the following will work:
Code:
if [ "$ex_year" -ge "$curr_year" ] && [ "$ex_month" -ge "$curr_month" ] && [ "$ex_day" -le "$curr_day" ]
then    echo true
else    echo false
fi

If you're using a recent bash or ksh, the following will also do what you want:
Code:
if [[ "$ex_year" -ge "$curr_year" && "$ex_month" -ge "$curr_month" && "$ex_day" -le "$curr_day" ]]
then    echo true
else    echo false
fi

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-03-2013
Bash and ksh93 you can use (( )).
Code:
if ((   ex_year >= curr_year   &&  ex_month >= curr_month  && ex_day <= curr_day       ))
then
       condition...
fi

This work in every sh, dash, ksh, bash, = any posix-sh or old Bourne shell.
Code:
if [ "$ex_year" -ge "$curr_year" -a "$ex_month" -ge "$curr_month" -a "$ex_day" -le "$curr_day" ]
then 
    echo OK
fi

[ is test command and it include argument AND and OR = -a / -o

[
[[
((
are 3 different command.

cmd && cmd && cmd is different as [ sometest -a sometest -a sometest ] but in this case result is same.
=>
[ sometest ] && [ sometest ] && [ sometest ]
give same result as
[ sometest -a sometest -a sometest ]

Last edited by kshji; 01-03-2013 at 12:15 PM..
This User Gave Thanks to kshji For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

if statement with '&&' gives error

Hi, I'm using the && operator in if statement: if ; then exp $UID/$PWD@$ORACLE_SID FILE=./DUMP/$TODAY$CONCAT_STR$USERNAME.dmp STATISTICS=NONE LOG=./LOG/$TODAY$CONCAT_STR$USERNAME.log elif ; then expdp $UID/$PWD@$ORACLE_SID DIRECTORY=./DUMP/ DUMPFILE=$TODAY$CONCAT_STR$USERNAME.dmp... (8 Replies)
Discussion started by: priya001
8 Replies

3. Shell Programming and Scripting

How to write If statement using && and operator in Unix

Hi What is the syntax for if statement using && and || operator? if && ] || here its giving me an error to this if statement any suggestion?? (2 Replies)
Discussion started by: Avi
2 Replies

4. Programming

IF && statement problem

Hello there, My first time on the forums, glad to be here :) I'm completely new to programming in PHP and I have a question which I hope someone could help me with. I am currently using this statement: if(($session == 2) && ($item == Dagger) && ($item2 == Dagger)){ ... (5 Replies)
Discussion started by: Hero
5 Replies

5. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

6. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

7. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

8. Shell Programming and Scripting

using && in if statement ..

Hi All, Can some one tell me how to get run the following: data1="hello" data2="world" if then { echo "good afternnon" } else { echo " good morning" } fi The above code gives me an error ad below : ./if.h: line 3: ' (7 Replies)
Discussion started by: jisha
7 Replies

9. UNIX for Dummies Questions & Answers

wild card & regular expressions

Hi, guys I have a question what is wild card and regular expression, I think both of them same if not, please give me an example. thanks (1 Reply)
Discussion started by: eerener
1 Replies

10. Shell Programming and Scripting

if statement with two conditions -e, &&

Wow I'm so zoned out I don't even know if I posted this question up already (I couldn't find it in my book marks or in "yesterday's" post). My question is, I'm writing a korn script that does something like the following, but I don't yet completely understand the syntax. I need to check that... (16 Replies)
Discussion started by: yongho
16 Replies
Login or Register to Ask a Question