nested logical expression in bash shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nested logical expression in bash shell
# 8  
Old 02-28-2012
This is not the same, for the single bracket version; there should be single = signs and double quotes around the left variable and the negation operator should work on the entire logical comparison, so one would need to use grouping to accomplish this:

Code:
if ! ( [ "$1" = "foo" ] || [ "$1" = "bar" ] )
then

The use of parentheses means using a subshell, so I think it is more efficient to use block grouping:

Code:
if ! { [ "$1" = "foo" ] || [ "$1" = "bar" ] ;}
then

Or use the logical equivalent:

Code:
if [ "$1" != "foo" ] && [ "$1" != "bar" ]
then

The equivalent using the features of test that are obsoleted in the POSIX specification would be:
Code:
if [ ! \( "$1" = "foo" -o "$1" = "bar" \) ]
then

-or-
Code:
if ! [ "$1" = "foo" -o "$1" = "bar" ]
then

---------- Post updated at 11:42 ---------- Previous update was at 09:24 ----------

Quote:
Originally Posted by ysrini
Using the recommended operators how can i write the following?
Code:
if NOT [ $var -eq 'a' || $var = 'b' || $var = 'c' ]
then
 ....
fi

I want to keep the '||' clause and do not want to translate into
Code:
if [ $var -ne 'a' && $var -ne 'b' && $var -ne 'c' ]

because using 'NOT' equivalent ahead upfront is much easier to understand the logic

I am using Korn shell

Thanks,
-srinivas y.
Code:
if ! { [ "$var" = a ] || [ "$var" = b ] || [ "$var" = b ] ;}
then

If you are writing a Korn shell script and do not need POSIX compliancy you can use this:

Code:
if ! [[ $var == a || $var == b || $var == c ]]
then


Last edited by Scrutinizer; 02-28-2012 at 06:24 AM..
# 9  
Old 02-28-2012
To answer post #1 :
The correct syntax in your context is:
Code:
if [ 1 -eq 1 -a \( 2 -eq 2 -o 2 -eq 3 \) ]
then
  echo "true"
else
  echo "false"
fi

Note that the parentheses are escaped because we want to give them to "test" and not start a subshell.

@Scrutinizer
The word is "depreciated" not "deprecated" !
Anyway there is no way that the "-a" and "-o" syntax or the brackets will be "depreciated" no matter how often the Posix police mention it.

Last edited by methyl; 02-28-2012 at 10:13 AM..
# 10  
Old 02-28-2012
@methyl: Deprecation
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 02-28-2012
Oops. I stand corrected. Word must have crept in from America. No such meaning in a UK English dictionary.
# 12  
Old 02-28-2012
Data

will post my question in new thread

Last edited by ysrini; 02-28-2012 at 10:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Nested loop -bash

I am using the following nested loop for i in {1..3} do for y in {1..3} do if ; then echo P0${i}R${y}.fas mv P0${i}R${y}.fas P${i}R${y}.fas read -t 5 fi done done I was wondering if I can use a character such as * or ? instead of my second variable y. I tried R in... (3 Replies)
Discussion started by: Xterra
3 Replies

2. Shell Programming and Scripting

Help with nested $s and quotations in bash / awk

Folks - newbie bash coder here and I'd like to get your help to make the code below work. As you can see, I was trying to count the total number of lines with the 3rd value >= 15 in a file and wanted to make the threshold "15" configurable, but apparently the $THRESHOLD value was not populated... (3 Replies)
Discussion started by: bashzipper
3 Replies

3. UNIX for Dummies Questions & Answers

Im new to bash scriping and i found this expression on a bash script what does this mean.

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi espeacailly the top regex part? ---------- Post updated at 06:58 PM ---------- Previous update was... (1 Reply)
Discussion started by: kevin298
1 Replies

4. Shell Programming and Scripting

Logical expression in POSIX compliant Korn Shell

Hi, i want to check if a variable var1 is not a or b or c pseudo code: If NOT (var1 = a or var1 = b or var1 = c) then ... fi I want to use POSIX complaint Korn shell, and for string comparison For the following code, logical.sh #!/usr/bin/ksh var="j" echo "Var : $var" if ! { || ||... (12 Replies)
Discussion started by: ysrini
12 Replies

5. Shell Programming and Scripting

Using Logical Expression in an AWK statement

I'm would to create a script that would give me the results below. Please note the spaces in the log file are actually commas(",".) Log file Data 0:00 21:15 899 43 31 12 25.39 0:00 21:20 736 34 19 15 35.39 0:00 21:20 776 41 28 13 ... (3 Replies)
Discussion started by: ravzter
3 Replies

6. Shell Programming and Scripting

Nested for loop in bash

Hi, How to use values in one for loop to other for loop. say "$sf_rel" variable has values "2011/W2 2011/G2" I want to use these values in inner for loop to process properly. $branch variable has G2 and 6 What is happening is outer for loop $i has 2011/W2 it is entering into inner... (3 Replies)
Discussion started by: Anjan1
3 Replies

7. Shell Programming and Scripting

Nested if question BASH

Just started learning bash ,and I am confused with sintaksis line 16: syntax error near unexpected token `else' thanks #!/bin/bash echo -n "Enter: " read num if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try again elif rem=$(echo $num | tr -d ) ... (7 Replies)
Discussion started by: lio123
7 Replies

8. Shell Programming and Scripting

Need help in Regular expression in bash shell

hi, I have written a script to search MAC address in a given directory. MAC address would be in format XX.XX.XX.XX. The digits contain hexadecimal numbers. For this i have used grep as follows. grep -rn '^\{1,2\}\.\{1,2\}\.\{1,2\}\.\{1,2\}\$' * This is not working as required.... (17 Replies)
Discussion started by: flamingo_l
17 Replies

9. Shell Programming and Scripting

Can you use logical operators in a case statement (bash)?

I'm pretty sure I already know the answer to this, but I want to make sure I'm not overlooking anything. I'm working on a log monitoring script and every 10 lines I want to display a summary of events. The thing is, there are a lot of possible events, that likely won't have happened, so I only want... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

10. Shell Programming and Scripting

how to use regular expression in Bash Shell Scripting

Hi, Actually i have written one test.sh (shell program) in bash. Here i have a variables $a which stored the value package1. Now I want to write a regular expression inside the if command that "if $a variable contains letter p in the begining of the value package1 then it is coming true.... (5 Replies)
Discussion started by: sunitachoudhury
5 Replies
Login or Register to Ask a Question