How to use logic NOT operator for multiple AND conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use logic NOT operator for multiple AND conditions
# 1  
Old 12-10-2010
How to use logic NOT operator for multiple AND conditions

Hi, my requirement is that my builds should not be built if the current hour is greater 3 but not (between 12 and 15), I'm trying to write a shell script for this but there is always an error
Code:
hour=$1 
echo "hour:$hour" 
if [ $hour -ge 3 ]  &&  ! [[ $hour -ge 12] && [$hour -le 15 ]]; then 
echo "exit" 
else 
echo "enter" 
fi

When I execute this, I get the below error:
Code:
$ ./test.sh 13 hour:13 ./test.sh: 
line 3: [[: 12]: syntax error: invalid arithmetic operator (error token is "]") exit

My question is how to use NOT operator for a combination of AND conditions Thanks

Last edited by Scott; 12-12-2010 at 04:36 AM.. Reason: Please use code tags
# 2  
Old 12-10-2010
Try following
Code:
if [ $hour -ge 3 ]  && ! [ $hour -ge 12 -a $hour -le 15 ]; then

# 3  
Old 12-10-2010
korn shell
Code:
$ cat ts1
hour=$1
echo "hour:$hour"
if [ $hour -ge 3 ] && ( ! [[ $hour -ge 12 && $hour -le 15 ]] ); then
echo "exit"
else
echo "enter"
fi

Code:
$ for i in {0..23}; do ksh ts1 $i; done
hour:0
enter
hour:1
enter
hour:2
enter
hour:3
exit
hour:4
exit
hour:5
exit
hour:6
exit
hour:7
exit
hour:8
exit
hour:9
exit
hour:10
exit
hour:11
exit
hour:12
enter
hour:13
enter
hour:14
enter
hour:15
enter
hour:16
exit
hour:17
exit
hour:18
exit
hour:19
exit
hour:20
exit
hour:21
exit
hour:22
exit
hour:23
exit
$

# 4  
Old 12-10-2010
Excellent, it worked - Thanks anurag & ctsgnb

I always have a confusion why the syntax in shell scripting is very sensitive - even if we don't put a space between ! and [ , it complains. :-)

Thanks for the help
# 5  
Old 12-11-2010
Code:
if [ $hour -ge 3 ] && ! ( [ $hour -ge 12 ] && [ $hour -le 15 ] ); then

# 6  
Old 12-11-2010
Code:
hour=$1 
echo "hour:$hour" 
case $hour in
1|2|12|13|14|15)    # Not allowed
        exit
        ;;
esac

# 7  
Old 12-12-2010
@methyl

Code:
1|2|1[2-5])
...
;;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple If conditions

I am analyzing one of the scripts written by another person.script is having multiple if conditions and everything are nested.The code is not formatted properly.Is there any way to identify in Unix to identify begin and end of a particular if block? (6 Replies)
Discussion started by: vamsi.valiveti
6 Replies

2. UNIX for Beginners Questions & Answers

Logic for multiple if conditions.

Hi Gurus, Is there a way we can set a logic for this problem ? The input file looks like below; 1 15 17 2 8 12 3 18 24 4 21 23 5 2 4 6 11 25 So, I would like to print for any row of the input file where the range of value between $2 to $3 lies within the min and max values of Min=10... (2 Replies)
Discussion started by: Indra2011
2 Replies

3. Shell Programming and Scripting

Multiple conditions in IF

Fellas, Am new to unix os/ and here the situation , I am trying to write multiple condition statement inside if but it throws me a error here is my piece of code , if ] && ] && ] then commands fi error : line 15 : ` can someone please advise me how to fix it Please use... (7 Replies)
Discussion started by: xeccc5z
7 Replies

4. Shell Programming and Scripting

Using sed and if multiple conditions

Hi I've been trying to get this to work but so far no luck. #!/usr/bin/ksh unset EXP APP=$1 EXP=`sed -n "/${APP}/p" tIclrpt.out |awk '{print $7}'|sed '/^$/d'` EXT=`sed -n "/${i}/p" ${SESLOG} |awk '{print $4}'|grep "${i}"` EXEM=/path/to/fail ACTI=/path/to/success if || then... (10 Replies)
Discussion started by: techy1
10 Replies

5. UNIX for Dummies Questions & Answers

If + multiple conditions

Hello Unix-Forums! It has been a long time since my last post, but finally I've got a new question: I know in case you can use multiple patterns by case $var in a|b|c|ab) and so on. But how would I place an OR between if ] then ... if ] then ... I want to execute the "..." if... (3 Replies)
Discussion started by: intelinside
3 Replies

6. Shell Programming and Scripting

Missing Assigned Variable within logic operator

Hey , I'm trying to perform the following command, however it cannot read the variable assigned earlier. I'm not sure why this happen. Please help thanks while : do echo "what's ur name? (if none just press )" read name changeName = echo $name | sed "s/on/ey/" echo $changeName #this... (8 Replies)
Discussion started by: sexyTrojan
8 Replies

7. Shell Programming and Scripting

Help regarding multiple conditions

Hi All, I am new to shell scripting. Can any one say what is wrong in this if statement, that uses multiple conditions if then *************** else if ( -z $pcs && "$night_time_calc" > "$night_time" ) then ******************************** ... (4 Replies)
Discussion started by: ssenthilkumar
4 Replies

8. Shell Programming and Scripting

multiple if conditions

Guys, Im trying to have a script that evaluates multiple conditions : test.sh: if then echo "host $1" else if then echo "host $1" else echo $1 not valid exit 1 fi when I do ./test.sh brazil1 I get: (4 Replies)
Discussion started by: bashshadow1979
4 Replies

9. Shell Programming and Scripting

multiple conditions in if using && operator

VARIABLE="project" if && ] then echo "VARIABLE is not empty" fi this is not working what is wrong in the syntax?? (2 Replies)
Discussion started by: codeman007
2 Replies

10. UNIX for Dummies Questions & Answers

multiple conditions in if/then

Hello, I am having trouble with the syntax with a conditional statement in a BASH script involving multiple conditions. Any suggestions would be greatly appreciated! if ; then array=("${array}" "$dnNum" ) fi i receive this error: ./testscript: ' (4 Replies)
Discussion started by: grandtheftander
4 Replies
Login or Register to Ask a Question