Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
function gettext () {
echo -e "What list will I be using?"
read -e item
#Allowing for, and correcting, the trailing space in interactive mode
if [[ "$item" =~ " " ]]; then
capfile=${item% *}
else
capfile=$item
fi
if [[ "$item" != "list.txt" ]]; then
callit="${thelist%.*}"
nocred="$credit-nocredit.txt"
yescred="$credit-hascredit.txt"
whichcredit="$credit-credited files.txt"
else
nocred="nocredit.txt"
yescred="hascredit.txt"
whichcredit="credited-files.txt"
fi
}
gettext
tuna=0; catfood=0
while read item
do
file0=$item
echo "Checking file $item"
if [ -f "$file0" ];then
cred=$(exiftool -s -S -IPTC:Credit "$file0" 2>/dev/null)
if [[ -n "$cred" ]]; then
#if [[ -n "$cred" ]]; then
echo -e "$file0 \e[42mhas\e[0m Credit data."
echo "Writing to file."
echo -e "$file0^$cred">> "$yescredit"
echo "$file0">> "$whichcredit"
catfood=$((catfood+1))
fi
if [[ -z "$cred" ]]; then
echo -e "$file0 does \e[41mnot\e[0m have Credit data."
echo "Writing filename to file nocredit.txt"
echo "$file0">> "$nocredit"
fi
else
echo "File $file0 was not found in this directory."
fi
tuna=$((tuna+1))
#sleep 0.5
done<"$capfile"
echo "$catfood out of $tuna files had Credit data."
IFS=$SAVEIFS
Shellcheck gives me advice that, when added to the script, fouls it further (or so it seems). One error it gives is
Code:
line 46: : No such file or directory
(line 46 writes the YES THERE"S CREDIT item to the customised 'hascredit.txt' file.) Almost identical code worked with the Transmission Reference checking script, so why won't it work here?
Carver
Last edited by L_Carver; 02-23-2017 at 12:25 PM.
Reason: Noun-verb disagreement
Might be me, just a side note:
The script doesn't look very clear to me.
You might want to rewrite it and make the logic behind it more clear.
To give examples: Use empty lines to distinguish different actions. "while read item; do file0="$item" ... " seems pointless, redundant. use more quotes. You seem to declare variables which you don't use (callit), and use variables you didn't declare (credit, thelist).
Or:
Explain what you are trying to achieve, by which means, and give an example of the textfile you want to parse (assuming that is what you want, i still can't fully figure it out. You seem to read two times from stdin, once from the function then once again from capfile.txt. I don't get it).
I tould you already: use www.shellcheck.net to check your script. You do not need to wait for any response, it will tell you immediately what is the problem.
Code:
Line 15:
nocred="$credit-nocredit.txt"
^-- SC2154: credit is referenced but not assigned.
Also you can try to add not only set -xv, but set -u and set -e too.
I tould you already: use www.shellcheck.net to check your script. You do not need to wait for any response, it will tell you immediately what is the problem.
Code:
Line 15:
nocred="$credit-nocredit.txt"
^-- SC2154: credit is referenced but not assigned.
I have shellcheck installed. "shellcheck -s bash ~/bin/creditcheck" returns
Code:
In /home/sXXXX/bin/creditcheck line 10:
if [[ "$item" =~ " " ]]; then
^-- SC2076: Don't quote rhs of =~, it'll match literally rather than as a regex.
Fixed that.
Code:
In /home/sXXXX/bin/creditcheck line 16:
callit="${thelist%.*}"
^-- SC2154: thelist is referenced but not assigned.
Fixed that just now (sloppy C&P from the other script). Here's hoping these changes make a difference.
Carver
Last edited by L_Carver; 02-25-2017 at 03:32 PM.
Reason: Took a new course with the script.
Here's what the latest version of the script looks like:
Code:
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
function themule () {
echo -e "What list will I be using?"
read -e item
#Allowing for, and correcting, the trailing space in interactive mode
if [[ "$item" ]]; then
capfile=${item% *}
else
capfile=$item
fi
if [[ "$capfile" != "list.txt" ]]; then
callit="${capfile%.*}"
nocall="$callit-nocredit.txt"
yescall="$callit-hascredit.txt"
whichcall="$callit-credited files.txt"
else
nocall="nocredit.txt"
yescall="hascredit.txt"
whichcall="credited-files.txt"
fi
echo $nocall
#touch $yescall
#touch $whichcall
}
themule
tuna=0; catfood=0
while read file0
do
echo "Checking file $file0"
#if [ -f "$file0" ];then
cred=$(exiftool -s -S -IPTC:Credit "$file0" 2>/dev/null)
if [[ -n "$cred" ]]; then
#if [[ -n "$cred" ]]; then
echo -e "$file0 \e[42mhas\e[0m Credit data."
echo "Writing to file."
echo -e "$file0^$cred">> "$yescall"
echo "$file0">> "$whichcall"
catfood=$((catfood+1))
fi
if [[ -z "$cred" ]]; then
echo -e "$file0 does \e[41mnot\e[0m have Credit data."
echo "Writing filename to file $nocall"
echo "$file0">> "$nocall"
fi
#else
# echo "File $file0 was not found in this directory."
#fi
tuna=$((tuna+1))
#sleep 0.5
done<"$capfile"
echo "$catfood out of $tuna files had Credit data."
IFS=$SAVEIFS
$nocall is given a value (see the "echo" on line 23), but the "nocredit.txt" file is not being written. So far, I've only tested it on a list.txt list, but even there the file listing the JPEGs without Credit metadata should be created and written to, shouldn't it? The touch commands, now commented out, proved as ineffective as the echo command I did keep.
without test data we cannot say anything. It may happen that your script worked perfectly using your input, I have no any idea about that.
If you still do not want to give us some example you need to use set -x, set -v to see what's happening.
I've put off anything serious for a few days anyhow. but I did want to clear something up.
The comment you often see in my scripts, "#Allowing for, and correcting, the trailing space in interactive mode," has more to do with tab completion than anything else. It's been my experience that when tab completion finds a single match to a string that is part of a file name, there's always a trailing space. DYMV?
I've put off anything serious for a few days anyhow. but I did want to clear something up.
The comment you often see in my scripts, "#Allowing for, and correcting, the trailing space in interactive mode," has more to do with tab completion than anything else. It's been my experience that when tab completion finds a single match to a string that is part of a file name, there's always a trailing space. DYMV?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.