![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to giv two conditions in IF statement..?? | RRVARMA | Shell Programming and Scripting | 6 | 04-25-2008 06:33 AM |
| reduce the or conditions | hitmansilentass | Shell Programming and Scripting | 8 | 05-03-2007 02:27 PM |
| multiple conditions in if/then | grandtheftander | UNIX for Dummies Questions & Answers | 4 | 07-21-2006 10:58 AM |
| if statement with two conditions | cin2000 | Shell Programming and Scripting | 1 | 01-23-2006 12:21 PM |
| if statement with two conditions -e, && | yongho | Shell Programming and Scripting | 16 | 06-14-2005 01:46 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 Code:
if [ "$h" -gt 9 -a "$h" -lt 21 ]; then ... fi Code:
if [ $((h > 9 && h < 21)) == 1 ]; then ... fi Code:
if [[ $h > 9 ]] && [[ $h < 21 ]]; then ... fi |
|
#3
|
|||
|
|||
|
Try this:
Code:
if [ $h -gt 9 -a $h -lt 21 ]; then |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Thank you guys for your help!
|
|||
| Google The UNIX and Linux Forums |
| Tags |
| conditions if statement |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|