nested logical expression in bash shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nested logical expression in bash shell
# 1  
Old 01-21-2012
nested logical expression in bash shell

Please tell me how to nest logical expressions in bash. I would like to nest logical expressions for arguments of the "test" command on bash.

The following pseudo-code shows my intention.

Code:
// pseudo code

if (exp1 AND (exp2 OR exp3))
{
  Output true;
}
else
{
  Output false;
}


The following bash code failed.

Code:
if test 1 -eq 1 -a ( 2 -eq 2 -o 2 -eq 3 )
then
  echo "true"
else
  echo "false"
fi

The above bash code ended up with the following error message.

syntax error near unexpected token `('


Many thanks, in advance.
# 2  
Old 01-21-2012
Code:
if [[ 1 -eq 1 && ( 2 -eq 2 || 2 -eq 3 ) ]]
then
  echo "true"
else
  echo "false"
fi

--ahamed
# 3  
Old 01-21-2012
-a and -o are deprecated... For POSIX compliancy use:

Code:
if test 1 -eq 1 && ( test 2 -eq 2 || test 3 -eq 3 )
then
  echo "true"
else
  echo "false"
fi

or the synonym:
Code:
if [ 1 -eq 1 ] && ( [ 2 -eq 2 ] || [ 3 -eq 3 ] );

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-21-2012
hi,

Quote:
Originally Posted by Scrutinizer
-a and -o are deprecated
Shell & Utilities - Single UNIX Specifications warns to not use them if script's input may be such strings.
I didn't find where it tells they're deprecated.

edit: or is it what means "deprecated"

Last edited by daPeach; 01-21-2012 at 07:25 PM..
# 5  
Old 01-22-2012
They advise not to use them, but the specification also says they have been marked obsolescent and that script should be converted:
Quote:
The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts using these expressions should be converted to the forms given below. Even though many implementations will continue to support these obsolescent forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations like:

test "$1" -a "$2"

should be written as:

test "$1" && test "$2"
test
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-28-2012
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.

Last edited by ysrini; 02-28-2012 at 12:45 AM..
# 7  
Old 02-28-2012
I think either of these are acceptable in Ksh or bash depending on your preference/need for single or double bracketed expressions:

Code:
if   ! [ $1 == "foo" ] || [ $1 == "bar" ]
then
    echo true
else
    echo false
fi

if  ! [[ $1 == "foo"  ||  $1 == "bar" ]]
then
    echo true
else
    echo false
fi

This User Gave Thanks to agama For This Post:
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