LinuxQuestions.org
Review your favorite Linux distribution.
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 11-19-2016, 08:10 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 107

Rep: Reputation: Disabled
Bash script missing matches to conditionals


I call it "badcommentcheck." It uses Exiftool to find and remove what I consider unnecessary strings in the JPEG comment header of files of that type. As it is now, it neither alerts the user when it finds them nor removes them with Exiftool silently when it does.

The code looks like this. (BTW, shellcheck missed the mistakes --if there are any -- with the 'alerts' altogether)
Code:
#!/bin/bash
SAVEIFS=$IFS
IFS=$( echo -e "\n\b")

 while read -r line; do
	comm1=$(exiftool -fast5 -s -S-Comment "$line")
	echo "Checking $file0."
	if [ ! -z "$comm1" ];then
		echo "$file0 has no comment data. Moving on"
		echo "$file0">>nocomment.txt
	fi
	if [ -n "$comm1" ]; then
		echo "$file0 has Comment data."
                echo -e "$file0^$comm1" >>hascomment.txt
	fi
	if [[ "$comm1" = "Created with GIMP"  ]]; then
		echo -e "$file0 has an unwanted comment. Writing to file."
		echo -e "$file0">>has-bad-comment.txt
		exiftool -fast5 -overwrite_original_in_place -q -P -Comment="$file0"
		echo -e "Comment data removed from $file0."
		echo -e "$file0">>comment-cleared.txt
	fi
	if [[ "$comm1" = "CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality =100." ]];then
		echo -e "$file0 has an unwanted comment. Writing to file."
		echo -e "$file0">>has-bad-comment.txt
		exiftool -fast5 -overwrite_original_in_place -q -P -Comment="$file0"
		echo -e "Comment data removed from $file0."
		echo -e "$file0">>comment-cleared.txt
	fi
	
done<commented-files.txt
IFS=$SAVEIFS
I've run it on batches of files where either or both of those strings were in more than one JPEG comment. I've even put them in some JPEGs myself with Exiftool to make them more 'conspicuous.'

The questions are:
Where am I making it skip returning the tip-offs to stdout? Or the 'remove' command from Exiftool ('-Comment=' with a null value is equivalent to a 'delete' or -M"del {exiv2} command.)

Carver

Last edited by L_Carver; 11-19-2016 at 08:14 AM. Reason: Command syntax (Exiftool)
 
Old 11-19-2016, 08:15 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Back to Debian, and only Debian.
Posts: 1,158
Blog Entries: 7

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
Quote:
Originally Posted by L_Carver View Post
I call it "badcommentcheck." It uses Exiftool to find and remove what I consider unnecessary strings in the JPEG comment header of files of that type. As it is now, it neither alerts the user when it finds them nor removes them with Exiftool silently when it does.

The code looks like this. (BTW, shellcheck missed the mistakes --if there are any -- with the 'alerts' altogether)

I've run it on batches of files where either or both of those strings were in more than one JPEG comment. I've even put them in some JPEGs myself with Exiftool to make them more 'conspicuous.'

The questions are:
Where am I making it skip returning the tip-offs to stdout? Or the 'remove' command from Exiftool ('-Comment=' with a null value is equivalent to a 'delete' or -M"del {exiv2} command.)

Carver
Without your data files, I can't run this myself and get the same results, but I can provide tip: have you tried running the script with tracing? Add -xv after the bash in your first line, or invoke it

Code:
bash -xv badcommentcheck
That will show you exactly what bash is doing.
 
Old 11-19-2016, 08:23 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Also, the code is incomplete as the variable 'file0' is never set. So as above, please provide example files and try the debugging option offered.
 
Old 11-19-2016, 08:44 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,235

Rep: Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402
Quote:
Originally Posted by L_Carver View Post
Code:
	if [ ! -z "$comm1" ];then
		echo "$file0 has no comment data. Moving on"
		echo "$file0">>nocomment.txt
	fi
That says, "If $comm1 is not zero-length, then the file has no comment data." Seems backwards to me.
 
Old 11-19-2016, 10:12 AM   #5
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Back to Debian, and only Debian.
Posts: 1,158
Blog Entries: 7

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
Maybe he got it confused with -n, or read the wrong thing somewhere. It's in single brackets, I'm wondering if maybe he meant to do
Code:
    if [ ! "$comm1" ]; then
 
Old 11-19-2016, 10:33 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,235

Rep: Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402Reputation: 1402
Looks to me like the big problem is the lack of a space between "-S" and "-Comment"
Code:
comm1=$(exiftool -fast5 -s -S-Comment "$line")
# should be
comm1=$(exiftool -fast5 -s -S -Comment "$line")
Without that space, nothing is returned, making it appear that no files have any comment (probably why the test got reversed to stop that darn "no comment data" message for every file).
 
Old 11-19-2016, 11:01 AM   #7
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,306

Rep: Reputation: 565Reputation: 565Reputation: 565Reputation: 565Reputation: 565Reputation: 565
you might want to re-NULL your variable too while you're at it. When it gets done checking one file to the next to clean it out.

before you're checking
Code:
comm1=''
comm1=$(exiftool -fast5 -s -S -Comment "$line")
	echo "Checking $file0."
	if [ ! -z "$comm1" ];then
then remove your ! in that code, because you're checking for NULL not NOT NULL
with the -z

Code:
comm1=''
comm1=$(exiftool -fast5 -s -S -Comment "$line")
	echo "Checking $file0."
#if NULL empty then 
	if [ -z "$comm1" ];then
             echo "$file0 has no comment data. Moving on"
             echo "$file0">>nocomment.txt
        fi
example
Code:
if [ -z "$1" ]; then
    echo "Empty Variable 1"
fi


if [ -n "$1" ]; then
    echo "Not Empty Variable 2"
fi

Last edited by BW-userx; 11-19-2016 at 11:10 AM.
 
Old 11-19-2016, 11:23 AM   #8
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,306

Rep: Reputation: 565Reputation: 565Reputation: 565Reputation: 565Reputation: 565Reputation: 565
If statments could be changed too

Code:
#!/bin/bash
SAVEIFS=$IFS
IFS=$( echo -e "\n\b")

#Changing your first set of if statements to this.

 while read -r line; do
        
	comm1=$(exiftool -fast5 -s -S -Comment "$line")
	echo "Checking $file0."
	if [ ! -z "$comm1" ];then
		echo "$file0 has no comment data. Moving on"
		echo "$file0">>nocomment.txt
	else
		echo "$file0 has Comment data."
                echo -e "$file0^$comm1" >>hascomment.txt
	fi

	# the logic behind that was if it is not empty 
          then it has to have something in it, so we 
	  use the else instead of another test. Cutting 
          down on time needed to check everything. Causing 
          a faster run. 
	
	
#changing your second if statements to this. 
	
	if [[ "$comm1" = "Created with GIMP"  ]]; then
		echo -e "$file0 has an unwanted comment. Writing to file."
		echo -e "$file0">>has-bad-comment.txt
		exiftool -fast5 -overwrite_original_in_place -q -P -Comment="$file0"
		echo -e "Comment data removed from $file0."
		echo -e "$file0">>comment-cleared.txt
	else

           if [[ "$comm1" = "CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality =100." ]];then
		echo -e "$file0 has an unwanted comment. Writing to file."
		echo -e "$file0">>has-bad-comment.txt
		exiftool -fast5 -overwrite_original_in_place -q -P -Comment="$file0"
		echo -e "Comment data removed from $file0."
		echo -e "$file0">>comment-cleared.txt
	   fi
         fi
	
done<commented-files.txt
IFS=$SAVEIFS
only the if statements were changed in this example.

compound statements

Last edited by BW-userx; 11-19-2016 at 11:42 AM.
 
  


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] Bash - Basic script to check hostname matches scratchyrat Programming 8 01-17-2013 07:32 AM
Bash script to suppress matches in two column list xsyntax Linux - General 6 09-03-2010 05:08 AM
[SOLVED] Problem with BASH conditionals - passing condition as variable smaddox Programming 8 09-18-2009 11:21 AM
bash: routine outputting both matches and non-matches separately??? Bebo Programming 8 07-19-2004 06:52 AM
bash scripting - conditionals dguy Linux - Newbie 4 01-19-2002 08:00 AM


All times are GMT -5. The time now is 08:57 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration