cannot properly employ "or" operator in an if statement (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cannot properly employ "or" operator in an if statement (bash)
# 1  
Old 08-12-2010
cannot properly employ "or" operator in an if statement (bash)

Hi,

I have a variable, $sername, and I would like to display this variable only if it *does not* contain either of these two tags: *DTI*FA* or *DIFF*FA*.

I think the syntax for my 'or' operator is off. The variable $sername is continuously changing in an outer loop (not shown), but at the moment, as an example, let's say:

Code:
sername=field_mapping

if [[ $sername == *DIFF*FA* || *DTI*FA* ]]; then echo "Found an FA tag in sername"; else  echo "The variable sername is FA-free"; fi

But this statement keeps outputting, "Found an FA tag in sername", which isn't right. Can anyone help me with the syntax? It would be nice if it could be done in a case-insensitive manner, too.

Thanks in advance!
# 2  
Old 08-12-2010
What shell are you using? Checking if a string contains a string isn't a single-operator task I think.
[edit] ah, bash.

Code:
function contains # haystack needle
{
        local H="$1"
        local N="$2"
        local C="${#H}"
        H="${H/$N/}"

        [ "${#H}" -lt "${C}" ] && return 0
        return 1
}

contains ASDF G || contains ASDF H || echo "Doesn't contain these things"

contains ASDF A || contains ASDF B || echo "Doesn't contain these things"


Last edited by Corona688; 08-12-2010 at 07:27 PM..
# 3  
Old 08-12-2010
to use 'or' in if in bash or bourne , do this:

if [ blah ] || [ blah ] || [ blah ]; then
blah blah blah

fi
# 4  
Old 08-12-2010
If I try sheelscripter's suggestion:

Code:
$sername=field_mapping

if [ $sername == *map* ] || [ $sername == *DTI*FA* ]; then echo "Found an FA tag in sername"; else  echo "The variable sername is FA-free"; fi

it is not specific enough, i.e., even though there's the tag "map" in the variable $sername, and I am now calling that in the 'if' statement, it still says that that variable does not contain the tag. In other words, the echo command delivers: "The variable sername is FA-free". So it didn't work.

Any other suggestions? This is bash, by the way. Thanks!
# 5  
Old 08-12-2010
Your original if statement was nearly correct. There must be a complete expression on either side of the or:

Code:
if [[ $sername == *DIFF*FA* || $sername ==  *DTI*FA* ]]

Your second attempt isn't working because

Code:
[ expression ]

is very much different than
Code:
[[ expression ]]

The double bracket expression is interpreted by the shell and the right hand side is treated as a pattern (if not quoted). Single brackets are treated just like any any other command (remember that [ is a link to the test command). If wildcards (* and ?) are used inside of single brackets, they are expanded as file names in the current directory and NOT treated as patterns. The filename expansion is then passed to the test command.

In my opinion it is best to avoid using single brackets in if and while statements in Kshell and bash. Forking to the test command is very inefficient not to mention the enhancements to the expressions that Kshell and bash provide.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

3. Shell Programming and Scripting

Equivalent to Perl's and Bash's "=~" Operator?

Hello All, Alright, so this is driving me absolutely insane. I can't seem to find this ANYWHERE... I've tried every combination and synonym I can think of for this trying to search Google. What is the Expect/Tcl equivalent to Perl and Bash's "=~" operator, (i.e. the "contains" operator).... (2 Replies)
Discussion started by: mrm5102
2 Replies

4. Shell Programming and Scripting

"if" statement based off "grep"

Hello, I am somewhat new to Linux/Unix. I am currently working on a shell script that is suppose to cat a file, grep the same file for a certain line, if that line is found save the file in a different location, else remove the file. This is a rough example of what I want. $Dating = False... (13 Replies)
Discussion started by: Amzerik
13 Replies

5. UNIX for Dummies Questions & Answers

What is the meaning of "-s" option in "if" statement?

Hi Guys, I'm sorry but I can't find answer for this, what is the meaning of -s option in "if" statement on unix scipting. Please see sample below: opath=/home/output for i in N1 N2 N3 N4 do echo $i if then grep $i $opath/N5_CRAI > $opath/N5_$i.crai chmod 777 $opath/N5_$i.crai ... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

What "-a" operator means in "if" statement

Hi I am trying to figure out what the following line does, I work in ksh88: ] && LIST="$big $LIST" Not sure what "-a" means in that case. Thanks a lot for any advice -A (1 Reply)
Discussion started by: aoussenko
1 Replies

8. Red Hat

"if" and "then" statement is not working in RedHat

Dear experts, I'm trying to write a script to calculate the usage of Log Archive in a directory, so if it gets to a point where the directory size is 60%, then send out an FYI.. email. So if then it reaches to 80%, move the logs from that directory. I have written the script as follow but... (10 Replies)
Discussion started by: Afi_Linux
10 Replies

9. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

10. UNIX for Advanced & Expert Users

pf not working properly even with only "pass in all" and "pass out all" rules

i have two rules in my pf.conf file, "pass in all" and "pass out all" i was having issues with getting pf working to begin with, so i went with starting from nothing and working on up. i have an ultrasparc ultra1 200e, with an added 4-port fast ethernet sbus card, running "3.4 GENERIC#85... (4 Replies)
Discussion started by: xyyz
4 Replies
Login or Register to Ask a Question