LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Anyone here ever scripted the exiv2 executable (in BASH)? (https://www.linuxquestions.org/questions/linux-software-2/anyone-here-ever-scripted-the-exiv2-executable-in-bash-4175641431/)

L_Carver 10-30-2018 10:05 AM

Anyone here ever scripted the exiv2 executable (in BASH)?
 
I've been trying to move a script that adds keywords from Exiftool (which seems to be getting slower -- natch; it's written in Perl) to Exiv2, 20+ times faster as it's written in 'good' (just ask the author) C++. But the issues I'm having are the following:
  • Breaking the string along comma delimiters, then reading the 'pieces' of that array in a loop.
  • Result 1: No keywords get written to the JPEG.
  • Result 2: One odd single keyword gets written to the JPEG.
Here's the script in its current iteration:
Code:

#!/bin/bash

tuna=0; catfood=0
function dav () {
echo -e "What text file will I be using?"
read -er item
f=$item
while IFS="^" read -r f k
do
echo -e "File is \e[31m$f\e[0m"
if [[ ! -f "$f" ]]; then
        echo "I don't see $f in this directory."
        echo "Moving to next list item."
        continue
fi
((catfood++))
dothedeed
done<"$f"
}
function twokey () {
        taxt=$(echo "$k" | cut -d, -f2)
        keycount=$(echo "$k" | tr ',' ' ' | wc -w)
for keys in $(echo "$k" | tr ',' ' ' | wc -w)
do
echo "${keys}"
/usr/bin/exiv2 --keep -M"add Iptc.Application2.Keywords $taxt" -M"add Xmp.dc.subject $taxt" "$f"
((taxt++))
done
/usr/bin/exiv2 -g Iptc.Application2.Keywords -Pv "$f"
}
function dothedeed () {
echo -e "Adding Keywords to \e[5;42m$f\e[0m."
echo -e "$f\t$k"
keycount=$(echo "$k" | tr ',' ' ' | wc -w)
echo "$f: There are $keycount keywords."
echo "Here's the Keyword string."
echo -e "$k"
#        exiftool -sep "," -fast5 -overwrite_original_in_place -q -P -Keywords+="$k" -Subject+="$k" "$f"
twokey
((tuna++))
}

echo "Uses Exiftool."
echo

dav
echo "$tuna out of $catfood files were annotated with Keywords this time."

#putk
#echo "Here's the Keyword string."
#echo -e "$k"

Hope there's someone with experience in this area who can help me.

and to answer the question before it's asked: I have tried several times to "restore" (I was on it years ago) my credentials on Exvi2 Forum. I have even corresponded via email with Andreas Huggel, to no avail as of this date (30102018).

Carver

pan64 10-30-2018 10:26 AM

as far as I remember I suggested you to use shellcheck to check your script. Actually there is a serious issue, which might cause your problem. But first I would like to tell you I have no idea what is it all about. But I know it is not related to exiv2 at all, but the shell script itself. If you wish to see how it works please insert set -xv at the beginning and you will see how your lines are evaluated/understood by bash and also how are they really executed (which was told already too).
But return back to the error (what shellcheck found):
Code:

Line 11:
if [[ ! -f "$f" ]]; then
          ^-- SC2094: Make sure not to read and write the same file in the same pipeline.
 
Line 18:
done<"$f"
    ^-- SC2094: Make sure not to read and write the same file in the same pipeline.

This is the function:
Code:

4 function dav () {
 5 echo -e "What text file will I be using?"
 6 read -er item
 7 f=$item
 8 while IFS="^" read -r f k
 9 do
10 echo -e "File is \e[31m$f\e[0m"
11 if [[ ! -f "$f" ]]; then
12        echo "I don't see $f in this directory."
13        echo "Moving to next list item."
14        continue
15 fi
16 ((catfood++))
17 dothedeed
18 done<"$f"
19 }

You specified $f as filename and tried to read that, but in line 8 (not detected by shellcheck) $f is overwritten.
Later you use $f but I have no idea if it was the right value or not... (you could easily check it with set -xv)

L_Carver 10-30-2018 10:56 AM

Sorry, I forgot to mention that I ran shellcheck (both local and online), and it gives
Code:

SC2094: Make sure not to read and write the same file in the same pipeline.
for an error. The gitHhub page does refer to "read and write the same," but what I fail to understand is: that's what the script is supposed to do. Creating a dummy file in this instance is useless. Of course, there is the sidecar option (file extension .exv, which is really just a headerless JPEG), which the script could write and write back to the JPEG later on in the code.

I will give set -xv a shot and see what it shows.

Carver

pan64 10-30-2018 11:01 AM

you still don't understand. You use the same variable $f for two different purposes. I'm really sure it is not what the script supposed to do.
(actually the shellcheck message is a bit misleading, it recognized there is something wrong, but the info is not really correct. Probably I will report it to the team).

L_Carver 10-30-2018 11:52 AM

The line of code, as I understand it
Code:

while IFS="^" read -r f k
looks for the file and the string of keywords.
The line
Code:

/usr/bin/exiv2 --keep -M"add Iptc.Application2.Keywords $taxt" -M"add Xmp.dc.subject $taxt" "$f"
is supposed to write a word found between the commas (as set by line 21, taxt=$(echo "$k" | cut -d, -f2), and the for loop gives the next value of taxt, doesn't it? Or should the 'cut -d, -f2' be written differently?
I'm thinking the whole routine, maybe the whole script, makes use of a "false array" and that's why I still get odd bits of the string written as one and only one keyword. Example: the second word, blonde, in the test file I'm using was the only one written to the JPEG the last time I ran the script (using set -xv).

Carver
PS: Please do report the misinformation to shellcheck/github. I would appreciate it.

L_Carver 11-15-2018 03:52 AM

Again, I think the problem is in that second line in function twokey, line 5 of the whole script. It keeps "picking" the first string after the first comma. I remember my original script (from years ago, not the one I posted here) had an echo showing which keyword was being added to foo.jpg. I added that echo and with a test file it echo'd one word (blonde, in this case).

Now I'm thinking of reverting to a bash 3 or earlier bash 4 method: invoking the comma as a second IFS, for the string of keywords. I just need a nudge in the right direction.

Carver

L_Carver 11-16-2018 01:06 PM

Solved elsewhere, with the help of one of the maintainers of exiv2.

Thanks all the same.

Carver

scasey 11-16-2018 03:22 PM

Quote:

Originally Posted by L_Carver (Post 5926824)
Solved elsewhere, with the help of one of the maintainers of exiv2.

Thanks all the same.

Carver

Please share the solution here to help the next person with the problem.
Thank you.


All times are GMT -5. The time now is 04:22 PM.