LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Another non-working script (https://www.linuxquestions.org/questions/programming-9/another-non-working-script-4175623130/)

L_Carver 02-05-2018 11:36 AM

Another non-working script
 
Doesn't work:
Code:

#!/bin/bash

function dothedeed () {
echo -e "Here's the Keyword string for file $f:\n$s\n"
echo -e "Adding SupplementalCategories to \e[5;42m$f\e[0m."
supcount=$(echo "$s" | tr ',' ' ' | wc -w)
echo "$thef: There are $supcount SupplementalCategories."
echo "Here's the Supplemental Category string."
echo -e "$s"
        exiftool -sep "," -fast5 -overwrite_original_in_place -q -P -SupplementalCategories+="$s" -SupplementalCategories+="$s" "$f"
        echo -e "SupplementalCategories added to file \e[4m$f.\e[0m"
}

function dav () {
echo -e "What text file will I be using?"
read -e item
while IFS="^" read f s
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
((x++))
dothedeed
done<"$thef"
}
echo "Uses Exiftool."
echo

dav

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

IFS=$SAVEIFS

(scriptname:addsupcats )
Above is an edit of:
Code:

#!/bin/bash

function dothedeed () {
echo -e "Here's the Keyword string for file $f:\n$k\n"
echo -e "Adding Keywords to \e[5;42m$f\e[0m."
keycount=$(echo "$k" | tr ',' ' ' | wc -w)
echo "$thef: 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"
        echo -e "Keywords added to file \e[4m$f.\e[0m"
}

function dav () {
echo -e "What text file will I be using?"
read -e item
#Allowing for, and correcting, the trailing space in interactive mode
if [[ "$item" =~ " " ]]; then
        thef=${item% *}
else
        thef=$item
fi
while IFS="^" read 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
((x++))
dothedeed
done<"$thef"
}
echo "Uses Exiftool."
echo

dav

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

IFS=$SAVEIFS

(scriptname:addkeys )
Konsole execution error is
Code:

line 14: : No such file or directory
Shellcheck returns the following:
Code:

n /home/steve/bin/addsupcats line 16:
read -e item
^-- SC2162: read without -r will mangle backslashes.

n /home/steve/bin/addsupcats line 16:
read -e item
^-- SC2162: read without -r will mangle backslashes.

I can't puzzle out why these could be causing an error, as read without "-r" works fine in the addkeys script.

Carver

rtmistler 02-05-2018 12:46 PM

Moving this to Programming to get your question better exposure.

Recommend you use "set -xv" to add debug inline in your script. I realize you used shellcheck, I've never used that and don't know how helpful it is.

You can even insert a "set -xv" before line 16 and then a "set +xv" after line 16 to minimize the debug output.

grail 02-05-2018 10:28 PM

The shellcheck message is a warning to advise of good habits, so can be ignored if you don't believe this is an issue.

Where are you expecting the 'item' variable to be populated from?

I would add that there is nowhere in the first script presented that populates the 'thef' variable that the while loop is trying to read from.
The second script does but then I am back to my first question as the assignment is based on 'item' being populated.

pan64 02-06-2018 01:59 AM

you can read a description about all the shellcheck messages like this: https://github.com/koalaman/shellcheck/wiki/SC2162 (just replace SC-number at the end). This will explain why -r is recommended and when can you omit or ignore it.
Just an additional remark to shellcheck: shellcheck cannot check if your script really works, so no errors does not mean your script is ok. It means only there were no semantic problems found.
You can also use set -xv as it was already suggested (for debugging), and additionally you can use set -u and set -e too.

L_Carver 02-06-2018 03:35 AM

Yes, I see where 'thef' stays empty (mistake now remedied)
 
Quote:

Originally Posted by grail (Post 5816268)
Where are you expecting the 'item' variable to be populated from?

In addkeys, I left the old (bash 4.0 and earlier) routine to trim any deadspace from the end of a value caught by read. The thef variable was set there, from the previous variable item. As that "trimming" routine (and here I use the term routine loosely) isn't in addsupcats, I acknowledge that thef wasn't set. I'll make that correction and see if it improves anything.

Quote:

Originally Posted by grail
The second script does but then I am back to my first question as the assignment is based on 'item' being populated.


L_Carver 02-06-2018 04:56 AM

I made the corrections and they worked. Marking thread as solved.


All times are GMT -5. The time now is 06:00 PM.