Logical OR in shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Logical OR in shell script
# 8  
Old 11-01-2014
I tried it with GNU bash, version 4.2.0(1)-release on OS X and it did not work
Code:
$ bash4-4.2$ bash4 --version
GNU bash, version 4.2.0(1)-release (i386-apple-darwin12.2.0)
bash4-4.2$ declare -a lang=(cpp sql js h); ext=cpp; if [[ ${lang[@]} =~ $(echo "\\<${ext}\\>") ]]; then echo hello; fi
bash4-4.2$

On Linux with GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) it did work
Code:
$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
$ declare -a lang=(cpp sql js h); ext=cpp; if [[ ${lang[@]} =~ $(echo "\\<${ext}\\>") ]]; then echo hello; fi
hello

--
This seems to also work with bash 4.1.2 on Linux:
Code:
$ declare -a lang=(cpp sql js h); ext="\<cpp\>"; if [[ ${lang[@]} =~ ${ext} ]]; then echo hello; fi
hello


-------------------
**EDIT**
-------------------
These (POSIX word boundaries) seem to work on all versions (also bash 3.2):
Code:
lang=(cpp sql js h)
ext='[[:<:]]cpp[[:>:]]'
if [[ ${lang[@]} =~ $ext ]]; then
  echo hello
fi


--
I prefer the case statement for this application though, like in #2

Last edited by Scrutinizer; 11-01-2014 at 07:57 AM..
# 9  
Old 11-01-2014
I knew all bets were off when using echo with any backslash.

I wonder if this would work in your first test with GNU bash, version 4.2.0(1)-release on OS, since in the several versions of bash I have in Linux, it has always worked.

Code:
ext=cpp
declare -a lang=(cpp py sql cc h)
if [[ ${lang[@]} =~ $(printf %s "\<${ext}\>") ]]; then
        echo ${ext}
fi

# 10  
Old 11-01-2014
Hi Aia, no it does not. I tried all these combinations and it does not work. Only the POSIX work boundaries work, but not directly in the expression itself. It needs to be done indirectly by a variable or by command substitution...

So this would also work, but at the cost of a subshell.
Code:
if [[ ${lang[@]} =~ $(printf "%s" "[[:<:]]$ext[[:>:]]") ]]; then
   echo hello
fi

So this would be more efficient:
Code:
mtch="[[:<:]]$ext[[:>:]]"
if [[ ${lang[@]} =~ $mtch ]]; then
   echo hello
fi


Last edited by Scrutinizer; 11-01-2014 at 08:25 AM..
# 11  
Old 11-01-2014
Hi, Scrutinizer
Thank you for trying. It was a nagging doubt I had.
# 12  
Old 11-01-2014
A bit more complex to intitialize, but easy when referencing: associative arrays. Like
Code:
declare -A lang=([cpp]=1 [sql]=1 [js]=1 [h]=1 [py]=1)
if [[ ${lang[$ext]} ]]; then echo hello; fi

# 13  
Old 11-01-2014
Quote:
Originally Posted by RudiC
A bit more complex to intitialize, but easy when referencing: associative arrays. Like
Code:
declare -A lang=([cpp]=1 [sql]=1 [js]=1 [h]=1 [py]=1)
if [[ ${lang[$ext]} ]]; then echo hello; fi

That was my initial aim, nevertheless, the newness of associative arrays in bash and the added complexity and ugly looking of the array persuaded me towards my original try. The case still remains that I never anticipated how difficult, in bash, pattern matching with variable expansion is.
# 14  
Old 11-01-2014
With regular pattern matching (not regex) you could also do this:
Code:
if [[ " ${lang[*]} " == *" $ext "* ]]; then
   echo hello
fi

or of course:
Code:
case " ${lang[*]} " in 
  (*" $ext "*) echo hello
esac

---
Note that there is no advantage in using arrays here. We might as well use a list:
Code:
lang="cpp sql js h py"

and then $lang instead of ${lang[*]}

Last edited by Scrutinizer; 11-01-2014 at 09:46 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

2. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. Shell Programming and Scripting

Shell grammar question: logical OR in test

Hi, I am trying to check if two input files exist before the rest of the scripts is run. Following is the code that I have but it gives me syntax error. if then echo "File not found" else echo "File found" fi (3 Replies)
Discussion started by: nua7
3 Replies

5. 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

6. Shell Programming and Scripting

SH Script for Logical OR function

Using good ole fashion bourne shell scripting I have to take 2 files and compare them 1:1 in a Logical OR "gate" or function and then output the info into a 3rd file. I have never tried anything like this before :eek: so any hand holding will be much "Thanked" OR Gate being: A, B, Result 0,... (2 Replies)
Discussion started by: Alivadoro
2 Replies

7. Shell Programming and Scripting

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. // pseudo code if (exp1 AND (exp2 OR exp3)) { Output true; } else { Output false; } ... (11 Replies)
Discussion started by: LessNux
11 Replies

8. Shell Programming and Scripting

How to implement the logical express --- A(B+C) within single "if" statment in shell script?

On Linux OS, bash environment, I want implement the following code: if && ( || ) A,B,C represents some compare conditions. How to realize it ? Thanks! (1 Reply)
Discussion started by: qcmao
1 Replies

9. Shell Programming and Scripting

Logical AND in shell commands

Hi:confused:, I have a file that contains : +-----------------------------------------------------------------------------+ LABEL: super1_fix EFIX FILES: 1 ABSTRACT: epkg for touch command PRE-REQUISITES: no PACKAGER VERSION: 7 REBOOT REQUIRED: no BUILD BOOT... (4 Replies)
Discussion started by: vijaya2006
4 Replies

10. Shell Programming and Scripting

How to do logical AND and logical OR with grep

Hi can someone please help me on this. I need to perform this code: Grep any lines that meets the following criteria (A AND B) OR (A AND C) I tried this code, but it didn't work Grep-I "A &&B" | "A&&C" *.* $ thanks in advance (12 Replies)
Discussion started by: Needhelp2
12 Replies
Login or Register to Ask a Question