LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script missing matches to conditionals (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-missing-matches-to-conditionals-4175593850/)

L_Carver 11-19-2016 08:10 AM

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

goumba 11-19-2016 08:15 AM

Quote:

Originally Posted by L_Carver (Post 5632329)
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.

grail 11-19-2016 08:23 AM

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.

rknichols 11-19-2016 08:44 AM

Quote:

Originally Posted by L_Carver (Post 5632329)
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.

goumba 11-19-2016 10:12 AM

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

rknichols 11-19-2016 10:33 AM

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

BW-userx 11-19-2016 11:01 AM

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


BW-userx 11-19-2016 11:23 AM

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


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