Two conditions in one if statement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Two conditions in one if statement
# 1  
Old 07-28-2008
Two conditions in one if statement

I'm totally new with bash programming and I don't get it how to put two conditions in one if statement. My code looks like this:
Code:
h=`date +%k`
if [ [ $((h>9)) ] && [ $((h<21)) ] ]; then

$h is 10 but I don't get into my if statement. What's wrong here?
# 2  
Old 07-28-2008
The shell has an eerie set of syntax alternatives for conditionals.

The [ is not a traditional delimiter, but the name of a command. The way to use it for multiple expressions is like this:

Code:
if [ "$h" -gt 9 ] && [ "$h" -lt 21 ]; then
  ...
fi

A slightly newer (that is, post-1979 or so ...) syntax is

Code:
if [ "$h" -gt 9 -a "$h" -lt 21 ]; then
  ...
fi

The $((...)) syntax is much newer, and introduces proper arithmetic (including the > and < operators) but is not directly usable as a condition. It simply expands to 0 for false and 1 for true. But of course, you can mix and match:

Code:
if [ $((h > 9 && h < 21)) == 1 ]; then
  ...
fi

Finally, there is the [[ ... ]] conditional, which is probably what you are after:

Code:
if [[ $h > 9 ]] && [[ $h < 21 ]]; then
  ...
fi

The characters which make up the [[ delimiter cannot be separated by whitespace (to the best of my knowledge).
# 3  
Old 07-28-2008
Try this:

Code:
if [ $h -gt 9 -a $h -lt 21 ]; then

# 4  
Old 07-28-2008
Oh boy! I think I'm to young for this. I'm from the Java generation, bash seems to be quite tricky.

These two seem to work. Is that really the same?

Code:
if [ $h -gt 9 -a $h -lt 21 ]; then

Code:
if [ "$h" -gt 9 ] && [ "$h" -lt 21 ]; then

# 5  
Old 07-28-2008
The double quotes are mainly for safety (good habit in case $h ends up containing an empty string by mistake, for example) and in theory, the && variant could create two external processes where -a would only create one. (It's theory because [ is probably handled internally in moderns shells, so there is no external process involved.)

Shell scripts are not parsed much, the syntax is more shallow than in many other scripting languages and this creates some complications, but also helps make the shell extremely versatile. The lack of standard, built-in arithmetic operators right from the start is another source of complexity in this case. POSIX attempts to fix some of the issues but historically, different shells have developed different extensions which have then created, as it were, even more different ways to skin a cat.
# 6  
Old 07-28-2008
Thank you guys for your help!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Check Multiple conditions in IF statement?

I wish to check two conditions inside the if statement Condition 1: The two file contents should be identical // using cmp command for this. Condition 2: The two filenames should NOT be the same. This is what i did in vain. if ]; then where entry1 and entry2 are ls *.txt | while... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. UNIX for Dummies Questions & Answers

If statement with two boolean conditions

#!/bin/bash if && then echo "True" else echo "False" fi Hi everyone, I am new to UNIX, here I have a if statement elevating two boolean conditions. I thought the output should be True because there are + in the statement. But it turns out to be False. Can anyone... (3 Replies)
Discussion started by: mryuyu1111
3 Replies

3. Shell Programming and Scripting

Multiple conditions inside if statement

I was trying to write multiple conditions inside the if statement but its not working. export VAR_NM=abc.txt export CURR_DT=20131011 export PREV_DT=20131012 if && then echo "Yes" else echo "NO" fi It should return Yes but returning NO always.Appreciate any help. (3 Replies)
Discussion started by: dr46014
3 Replies

4. Shell Programming and Scripting

Multiple conditions inside my if statement

Hello, I am using shell scripting and I am recieving odd results from my if statement if I want it to enter the loop only if L1 is equal to zero and one of the other criteria are filled, however it is entering at other times as well. What can i do to fix this? i tried seperating it... (6 Replies)
Discussion started by: ryddner
6 Replies

5. Shell Programming and Scripting

Multiple Conditions Perl if Statement

Hello, I'm trying to put together a script that involves pulling data from a config file. I'm attempting to write an if statement to validate one of the pieces of data from the config file, but I think I'm fat fingering it somehow. $config{VALUE} is being pulled from a config file but can only... (4 Replies)
Discussion started by: Picch
4 Replies

6. Shell Programming and Scripting

Multiple conditions in a CASE statement

is it possible to use multiple conditions in a CASE statement? And if so, what is the syntax? I'm trying to use one but can't seem to get it right. I want the statement to be CASE $vendor OR $alias condition 1) statements; condition 2) statements; etc. esac but I keep... (25 Replies)
Discussion started by: Straitsfan
25 Replies

7. Shell Programming and Scripting

Multipe conditions in if statement

I have a script that runs every 15 minutes in cron that builds a web page. It runs at 15, 28, 45 and 58 minutes past the hour. (pretty much evry 15 mins). Every 2 hours from 6:28 to 18:28 it sends out emails if it finds errors. I do not want it sending email every single time it runs, every 15... (5 Replies)
Discussion started by: spacemancw
5 Replies

8. Shell Programming and Scripting

If statement with multiple conditions

I have a script that runs on multiple servers. What I want to do is have the script do the following: if $(hostname) is equal to server or server2 then TO_DIR=go else TO_DIR=stop fi I have tried: if if ] Server is hpux. any ideas? (1 Reply)
Discussion started by: cpolikowsky
1 Replies

9. Shell Programming and Scripting

How to giv two conditions in IF statement..??

Actually i need to satisfy both the condition.. lik i'm lookin for two different files.. and if BOTH the files r not present then it should run the followin script.. For example inputfile1=data/in/inputfile1.txt inputfile2=data/in/inputfile2.txt if then echo " " echo... (6 Replies)
Discussion started by: RRVARMA
6 Replies

10. Shell Programming and Scripting

if statement with two conditions

Hi, I am trying to use && set up to match two conditions within ksh: if && then '''Do something if somehow, I keep getting error message telling me that ] is missing. What's wrong with my code? Thanks a lot for your help! (1 Reply)
Discussion started by: cin2000
1 Replies
Login or Register to Ask a Question