LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-02-2017, 11:31 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
Question bash: why don't my "double if's" ever work?


Code as example:
Code:
comm1=$(exiftool -fast5  -s -S -Comment "$file0")
	if [[ $comm1 =  *"creator:gd-jpeg"* ]] || [[ $comm1 =  *"CREATOR:GD-JPEG"* ]] ; then
    		echo -e "$file0 has a \"libgd\" Comment. Writing to file."
			echo -e "$file0">>had-GD-comment.txt
			jhead -dc "$line"
			echo -e "Comment data removed from $file0."
			echo -e "$file0">>comment-cleared.txt
	fi
Two pipes between conditions in an if statement still means "or," doesn't it? Has the word "or" been totally worked out of bash 4? Anyway, when the script is run, and the conditions are met, it fails to create either text file in the above "if-then" to record which files did have JPEG comments with the gd-jpeg string (lower- or uppercase) in them.

And so I'm thinking I might be writing these "double ifs" incorrectly.

Any thoughts from the Peanut Gallery?

Carver

Last edited by L_Carver; 03-02-2017 at 11:32 AM. Reason: Bad phrasing
 
Old 03-02-2017, 11:37 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
easy to check if your OR logic works, which it does:

Code:
#!/bin/bash -x
# type some jibberish, then some jibberish with string1 in it, then some jibberish with string2 in it
# works fine
read comm1
if [[ $comm1 =  *"string1"* ]] || [[ $comm1 =  *"string2"* ]] ; then
echo YES - i found either one string or the other
else
echo NO - i did not find either string
fi
So it must be something with your exiftool output.

Can you provide an example of what $comm1 is being set to before the comparison? ie:

Code:
comm1=$(exiftool -fast5  -s -S -Comment "$file0")
echo $comm1

Last edited by szboardstretcher; 03-02-2017 at 11:39 AM.
 
Old 03-02-2017, 12:40 PM   #3
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
How about you try Bash's regex construct such as:

Code:
if [[ $comm1 =~  "string1" ]] ...
 
Old 03-02-2017, 03:03 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,452

Rep: Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061
Posix-compliant is ==
Code:
if [[ $comm1 == *"string1"* ]] || [[ $comm1 == *"string2"* ]] ; then
The EREG allows a | or
Code:
if [[ $comm1 =~ "string1"|"string2" ]]
then
  ...
fi
Or the classic
Code:
case $comm1 in
(*"string1"*|*"string2"*)
  ...
;;
esac
is quite like the == glob style but further allows a | or.
But all this is more or less cosmetic. Your code works as intended.
 
Old 03-02-2017, 04:47 PM   #5
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
Posix-compliant is ==
Code:
if [[ $comm1 == *"string1"* ]] || [[ $comm1 == *"string2"* ]] ; then
I don't think that = or == is Posix-compliant for pattern matching, one would use case instead.
And [ instead of [[ too, of course.
Perhaps i am wrong or you meant something different.

http://mywiki.wooledge.org/Bashism
(headline Conditionals)
 
1 members found this post helpful.
Old 03-03-2017, 10:49 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,452

Rep: Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061
You are right:
http://pubs.opengroup.org/onlinepubs/9699919799/
says:
Quote:
The KornShell-derived conditional command (double bracket [[]]) was removed from the shell command language description in an early proposal. Objections were raised that the real problem is misuse of the test command ([), and putting it into the shell is the wrong way to fix the problem. Instead, proper documentation and a new shell reserved word (!) are sufficient.
What a pity! The real problem is that [ ] requires additional quoting and X-ing and other ugly safety measures.
 
1 members found this post helpful.
Old 03-03-2017, 01:10 PM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
You need more quotes and less wild cards *O*. Just a guess, but normally solves my bash woes. I still find it difficult to read Bash as C has = to "SET" a value (always returns true) and == to "COMPARE" a value. These days speed of development matters more than speed of appliance, so I often dumb down my compares and make more of them.

Compare1 = ( "string1" = "string2" )
Compare2 = ( "string3" = "string4" )
OrCompare = 0;
if ( $Compare1 ); then OrCompare=1; fi
if ( $Compare2 ); then OrCompare=1; fi
if ( $OrCompare ); then
# actual useful stuff here;
fi

But I started early and breaking 80 columns was a crime. And debugging bash gets simpler this route too as you can check each condition based on VarNames in a single echo.

echo "DEBUG - C1: "$Compare1" - C2: "$Compare2" - OC: "$OrCompare
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Keyboard and mouse don't work after typing "startx", athenian200 Gentoo 4 05-13-2016 04:13 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
BASH problems: "configure, make, make install" commands don't work ditch* Linux - General 3 07-19-2005 04:37 PM
chrooted user: "write" and "talk" don't work. ldp Linux - Software 2 04-12-2005 02:05 AM
"depmod" and "modprobe" commands don't work The1PatO Fedora 7 06-10-2004 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration