if statement negation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if statement negation
# 1  
Old 02-21-2011
if statement negation

Hi all,

I've been searching the internet and can't find an answer to this. Im trying to figure out how to negate a whole expression befor an if. I'll explain below.

Let's say

x=0
y=1

[[ x = 0 && y = 0 ]] (this is false) I would like to negate this whole boolean condition. To negate the above expression in other languages I can do something like this

![[ x = 0 && y = 0 ]] returns true

is there something similar in UNIX?

I tried

if ![[ x = 0 && y = 0 ]]; then

............

fi

but this doesn't work it doesn't go into the if statement.

Thanks
# 2  
Old 02-21-2011
There needs to be a space between the ! and the first [
# 3  
Old 02-21-2011
Dont forget the $ in front of variable names and space after !

Code:
if ! [[ $x = 0 && $y = 0 ]]; then

or
Code:
if [[ ! ($x = 0 && $y = 0) ]]; then

# 4  
Old 02-21-2011
Also in square brackets in bash and ksh93 double == are preferred for a string comparison in combination with double brackets..
Code:
if ! [[ $x == 0 && $y == 0 ]]; then

Numerical comparison:
Code:
if ! [[ $x -eq 0 && $y -eq 0 ]]; then

Arithmetic evaluation:
Code:
if ! (( x == 0 && y == 0 )); then

Or POSIX tests (works in many other shells):
Code:
if ! ( [ "$x" = 0 ] && [ "$y" = 0 ] ) ; then

Code:
if ! ( [ $x -eq 0 ] && [ $y -eq 0 ] ) ; then


Last edited by Scrutinizer; 02-21-2011 at 07:09 PM..
# 5  
Old 02-22-2011
assuming x and y as positive numeric, this will return true if either x or y != 0 :
Code:
if ((x+y)); then

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Negation in Bash Globbing

$ ls -1 a.1 b.1 x_a.1 x_b.1 $ ls -1 * b.1 x_a.1 x_b.1 $ ls -1 ** a.1 b.1 x_a.1 x_b.1The last result is not as expected. Why? Thanks. (2 Replies)
Discussion started by: carloszhang
2 Replies

3. Homework & Coursework Questions

Negation

Hello, have anybody any idea, how to negate some expression? I have code that greps somethnig and I want filter from it some results...How to make it? IDontWantThisExpression=$1 grep 'some regex' | grep '$IDontWantThisExpression' testfile.txt Thanks for any advices. Faculty of... (2 Replies)
Discussion started by: Dworza
2 Replies

4. Shell Programming and Scripting

Negation in "tr"

Is there an option for negation in the "tr" command? For eg: echo hi | tr "h" "i" will print "ii". But if I want to covert all characters that are not "h" to "j", how do I do that? Is there something like "!" in tr? Or any switch? Thanks, Prasanna (17 Replies)
Discussion started by: prasanna1157
17 Replies

5. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

6. UNIX for Dummies Questions & Answers

grep with negation in "if" statement

I have an "if" statement which checks for certain pattern in the file: if grep -i dblist.comdbg /bb/bin/rstrt then copydb else exit 1 fi How can I use the same statement with negation, checking if file does not exist, something like if ! grep -i dblist.comdbg /bb/bin/rstrt then ... (2 Replies)
Discussion started by: aoussenko
2 Replies

7. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

8. Shell Programming and Scripting

negation in find path

Guys, Pl suggest me how to ignore a path in find command... I am aware of using "!" for other option... but how can i use it like this exampl... I want to search something for in / but not in Pl help. Thanks.. (2 Replies)
Discussion started by: clx
2 Replies

9. Shell Programming and Scripting

Bitwise negation

I am taking an online course on Unix scripting. The topic is Unix arithmetic operators and the lesson is Logical and bitwise operations. It is not clear how much storage space Unix uses to represent integers that are typed. Bitwise negation caused me to question how many bits are used to... (3 Replies)
Discussion started by: dLloydm
3 Replies
Login or Register to Ask a Question