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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
05-14-2017, 02:14 AM
|
#1
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Rep: 
|
bash: Works here, not there. why not?
First script (here the special labeling of output files works):
Code:
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
##With regard to the Smiths song "Cemetery Gates"
keats=0; yeats=0;
function throwin () {
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
#echo $capfile
}
throwin
tuna=0; catfood=0
#wilde1=$((yeats+1))
while read file0
do
wilde1=$((tuna+1))
#file0=$item
echo -e "Checking file #$wilde1, $file0"
if [ -f "$file0" ];then
if [[ $capfile = list.txt ]]; then
callit="$capfile"
nocall="nocredit.txt"
yescall="hascredit.txt"
listem="credited-files.txt"
# echo -e "$callit\t$nocall\t$yescall\t$listem"
# exit 0
fi
if [[ ! $capfile = list.txt ]]; then
callit="${capfile%.*}"
listem="$callit-credited-files.txt"
nocall="$callit-nocredit.txt"
yescall="$callit-hascredit.txt"
# echo -e "$callit\t$nocall\t$yescall\t$listem"
# exit 0
fi
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 "$file0">>"$listem"
echo "Writing to file."
echo -e "$file0^$cred">> "$yescall"
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">>"$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
Second script (written, to my eyes, almost identically where special output files are concerned, but does not create them when called upon to do so):
Code:
#!/bin/bash -i
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
ape=0 monkey=0
function pickfile () {
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
capfile1=${item% *}
else
capfile1=$item
fi
}
pickfile
while read file0; do
if [ -f "$file0" ];then
if [[ $capfile1 = list.txt ]]; then
callit="$capfile"
nocall="nocomment.txt"
yescall="hascomment.txt"
listem="commented-files.txt"
# echo -e "$callit\t$nocall\t$yescall\t$listem"
# exit 0
fi
if [[ ! $capfile1 = list.txt ]]; then
callit="${capfile%.*}"
listem="$callit-commented-files.txt"
nocall="$callit-nocomment.txt"
yescall="$callit-hascomment.txt"
# echo -e "$callit\t$nocall\t$yescall\t$listem"
# exit 0
fi
else
echo -e "That list was not in this directory.\nExiting."
exit 1
fi
# comm1=$(exiv2 -pc "$line")
comm1=$(exiftool -s -S -Comment $file0)
comm2=$(exiftool -s -S -UserComment $file0)
if [[ -n "$comm1" ]]; then
echo -e "$file0 \e[1mhas\e[0m Comment data. Noted."
echo -e "$file0" >>commented-files.txt
echo "Writing Comment to file."
echo -e "$file0^$comm1" >>hascomment.txt
monkey=$((monkey+1))
fi
if [[ -z $comm1 ]] && [[ -z $comm2 ]]; then
echo -e "$file0 has \e[91;1mno\e[0m Comment data. Moving on"
echo "$file0">>nocomment.txt
fi
ape=$((ape+1))
done< "$capfile1"
echo -e "$monkey out of $ape files had JPEG Comments."
IFS=$SAVEIFS
Shellcheck output on second script:
Code:
In /home/carver/bin/checkmycomments line 9:
if [[ $item =~ " " ]]; then
^-- SC2076: Don't quote rhs of =~, it'll match literally rather than as a regex.
In /home/carver/bin/checkmycomments line 19:
callit="$capfile"
^-- SC2154: capfile is referenced but not assigned (did you mean 'capfile1'?).
In /home/carver/bin/checkmycomments line 28:
listem="$callit-commented-files.txt"
^-- SC2034: listem appears unused. Verify it or export it.
In /home/carver/bin/checkmycomments line 29:
nocall="$callit-nocomment.txt"
^-- SC2034: nocall appears unused. Verify it or export it.
In /home/carver/bin/checkmycomments line 30:
yescall="$callit-hascomment.txt"
^-- SC2034: yescall appears unused. Verify it or export it.
In /home/carver/bin/checkmycomments line 39:
comm1=$(exiftool -s -S -Comment $file0)
^-- SC2086: Double quote to prevent globbing and word splitting.
In /home/carver/bin/checkmycomments line 40:
comm2=$(exiftool -s -S -UserComment $file0)
^-- SC2086: Double quote to prevent globbing and word splitting.
I thought I had this resolved. I guess not.
Carver
Last edited by L_Carver; 05-14-2017 at 02:21 AM.
|
|
|
05-14-2017, 02:36 AM
|
#2
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
why are there 2 versions of the script?
what are the exact differences?
what do the scripts do?
why don't you fix what shellcheck found, then re-formulate your question?
in addition to that, i find this:
"$callit-hascomment.txt"
highly unusual.
i think it means "a string composed from the contents of the variable $callit-hascomment, and the suffix '.txt'".
you should know.
in cases like this i find it best to enclose the var name in curly brackets, like so: "${callit-hascomments}.txt"
|
|
1 members found this post helpful.
|
05-14-2017, 02:41 AM
|
#3
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
|
|
|
05-14-2017, 05:19 AM
|
#4
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Original Poster
Rep: 
|
[quote="ondoho"]why are there 2 versions of the script?[/quote}
There aren't. It's a comparison. I'm trying to figure out why almost-identical code works in one but not in the other. Please refer back to the Subject line of this thread.
Quote:
Originally Posted by ondoho
what are the exact differences?
|
You should be able to tell what the exact differences are by reading them as separate scripts (see above).
Quote:
Originally Posted by ondoho
what do the scripts do?
|
Like the script in the previous thread I linked in my OP, this one checks JPEG files using Exiftool. Like the other script, I want to customise the output files (yes, these have them or no, these don't have them) to correspond with the lists used as input by name. In addition, borrowing from the original script this was based on, I want to generate a list of "yesses" that included the content of the Comments.
Carver
|
|
|
05-14-2017, 05:23 AM
|
#5
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Original Poster
Rep: 
|
Quote:
Originally Posted by ondoho
why are there 2 versions of the script?
|
There aren't. It's a comparison. I'm trying to figure out why almost-identical code works in one but not in the other. Please refer back to the Subject line of this thread.
Quote:
Originally Posted by ondoho
what are the exact differences?
|
You should be able to tell what the exact differences are by reading them as separate scripts (see above).
Quote:
Originally Posted by ondoho
what do the scripts do?
|
Like the script in the previous thread I linked in my OP, this one checks JPEG files using Exiftool. Like the other script, I want to customise the output files (yes, these have them or no, these don't have them) to correspond with the lists used as input by name. In addition, borrowing from the original script this was based on, I want to generate a list of "yesses" to include the content of the Comments.
Quote:
Originally Posted by ondoho
why don't you fix what shellcheck found, then re-formulate your question?
|
Point taken. I'll get back to us on that. :-)
Carver
|
|
|
05-14-2017, 05:28 AM
|
#6
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Original Poster
Rep: 
|
The fixes (some shellcheck and some 'suggested by ondoho')
To wit:
Code:
#!/bin/bash -i
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
ape=0 monkey=0
function pickfile () {
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
capfile1=${item% *}
else
capfile1=$item
fi
}
pickfile
while read file0; do
if [ -f "$file0" ];then
callit="${capfile1%.*}"
listem="$callit-commented-files.txt"
nocall="${callit-nocomment}.txt"
yescall="${callit-hascomments}.txt"
fi
comm1=$(exiv2 -pc "$line")
comm1=$(exiftool -s -S -Comment $file0)
comm2=$(exiftool -s -S -UserComment $file0)
if [[ -n "$comm1" ]]; then
echo -e "$file0 \e[1mhas\e[0m Comment data. Noted."
echo -e "$file0" >>$listem
echo "Writing Comment to file."
echo -e "$file0^$comm1" >>$yescall
monkey=$((monkey+1))
fi
if [[ -z $comm1 ]] && [[ -z $comm2 ]]; then
echo -e "$file0 has \e[91;1mno\e[0m Comment data. Moving on"
echo "$file0">>$nocall
fi
ape=$((ape+1))
done< "$capfile1"
echo -e "$monkey out of $ape files had JPEG Comments."
IFS=$SAVEIFS
I have yet to do a "real-time" run of the new edit to see if it generates the desired output files.
Carver
Last edited by L_Carver; 05-14-2017 at 05:30 AM.
Reason: Forgot "fair warning."
|
|
|
05-14-2017, 06:18 AM
|
#7
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
now shellcheck has this to say:
Code:
$ shellcheck myscript
Line 7:
read -e item
^-- SC2162: read without -r will mangle backslashes.
Line 17:
while read file0; do
^-- SC2162: read without -r will mangle backslashes.
Line 25:
comm1=$(exiv2 -pc "$line")
^-- SC2154: line is referenced but not assigned.
Line 26:
comm1=$(exiftool -s -S -Comment $file0)
^-- SC2086: Double quote to prevent globbing and word splitting.
Line 27:
comm2=$(exiftool -s -S -UserComment $file0)
^-- SC2086: Double quote to prevent globbing and word splitting.
Line 30:
echo -e "$file0" >>$listem
^-- SC2086: Double quote to prevent globbing and word splitting.
Line 32:
echo -e "$file0^$comm1" >>$yescall
^-- SC2086: Double quote to prevent globbing and word splitting.
Line 37:
echo "$file0">>$nocall
^-- SC2086: Double quote to prevent globbing and word splitting.
why is this so hard?
why don't you fix it?
(the first 2 probably aren't so important)
|
|
1 members found this post helpful.
|
05-14-2017, 02:57 PM
|
#8
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,027
|
The real mistake is certainly the "$line" where line is not defined.
Some optimizations
Code:
IFS=$(echo -en "\n\b")
is short
Code:
if [[ $item =~ " " ]]; then
capfile1=${item% *}
else
capfile1=$item
fi
is short
Code:
capfile1=${item% *}
The % modifier only chops what matches.
|
|
1 members found this post helpful.
|
05-15-2017, 09:48 PM
|
#9
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Original Poster
Rep: 
|
Line IS defined
Line 16, to wit:
Code:
while read line; do
Though I suppose a logical change would be
Code:
while read file0; do
which eliminates the need to define file0 in line 17.
+! (+ ∞) to MadeInGermany for the solution to the IFS definition problem.
I simplified the whole script, made it pretty close to what it was before I tried the specialisations. Now I have a working base to add them back, or so it seems. It will need a test run, which will likely happen later on tonight (15/5).
Again, thanks for all the help.
Carver
|
|
|
05-16-2017, 01:12 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,966
|
|
|
1 members found this post helpful.
|
05-25-2017, 02:40 AM
|
#11
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Original Poster
Rep: 
|
A similar script in need of "first aid."
Code:
#!/bin/bash -i
SAVEIFS=$IFS
IFS=$"\n\b"
meat=0; dairy=0
function findtag () {
while read -r file0
do
if [[ $thefile = list.txt ]]; then
nocall="notref.txt"
yescall="hastref.txt"
listem="referenced-files.txt"
echo -e "$callit\t$nocall\t$yescall\t$listem"
fi
if [[ ! $thefile = list.txt ]]; then
callit="${thefile%.*}"
listem="$callit-referenced-files.txt"
nocall="$callit-notref.txt"
yescall="$callit-hastref.txt"
echo -e "$callit\t$nocall\t$yescall\t$listem"
fi
echo -e "Checking file #$((dairy+1)), $file0"
tref1=$(exiftool -fast5 -q -s -S -OriginalTransmissionReference "$file0")
if [ -n "$tref1" ]; then
echo -e "$file0 \e[42mHAS\e[0m a Transmission Reference"
echo -e "$file0">>"$listem"
echo "Writing to file."
echo -e "$file0:$tref1">> "$yescall"
((meat++))
fi
if [[ ! -n "$tref1" ]]; then
echo -e "$file0 does \e[41mNOT\e[0m have a Transmission Reference."
echo "Writing to file $nocall."
echo "$file0">> "$nocall"
fi
dairy=$((dairy+1))
done<$thefile1
echo -e "$meat out of $dairy files had Transmission References."
}
function nosurrender () {
echo -e "What text file1 will I be using?"
read -er item
#Allowing for, and correcting, the trailing space in interactive mode
if [[ "$item" =~ " " ]]; then
thefile1=${item% *}
else
thefile1=$item
fi
findtag
}
nosurrender
exit 0
IFS=$SAVEIFS
With this one, when any list file (list.txt or some other file without an extension) is used, the list files created begin with a hyphen (lines 14 and 21). In the script that works, creditcheck, they start with "list-"blahblah".txt" or "foo-"blahblah".txt" for other files with no extension. That hyphen makes the generated files a bit(ch) difficult to get rid of from the command line, so of course I don't want them. I've tweaked this script to resemble creditcheck more closely, but it still makes files starting with the hyphen. Where am I missing that thing that will eliminate/ignore both with list.txt (generic), or at least, like creditcheck does, precede the hyphen with the word "list"?
And just for confirmation, set -xv and the like only serve to confuse me further, so I don't see the point in using them.
Carver
|
|
|
05-25-2017, 02:51 AM
|
#12
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Original Poster
Rep: 
|
I just ran shellcheck on it (refcheck) and...
...found that my variables were of different names, specifically
Code:
In /home/carver/bin/refcheck line 10:
if [[ $thefile = list.txt ]]; then
^-- SC2154: thefile is referenced but not assigned (did you mean 'thefile1'?).
I fixed that, and now the generated files start with something besides a hyphen. I'd still like to know how to get the if/then/fi conditional that says "write just yes-this.txt or no-that.txt," without "list-" in front of it, to work better, and I'm sure there's a way because I had scripts from the time of the earliest versions of bash 4 that did it. For now I'll live with the "precedent" strings, as at least they're something different from what the scripts made before all this specialisation.
So the cliche'd question is, "How much egg is on my face now?" My bad for coming in with this one so unprepared.
Carver
Last edited by L_Carver; 05-25-2017 at 02:55 AM.
|
|
|
05-25-2017, 03:32 AM
|
#13
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,966
|
I do not really understand, but probably:
Code:
if [[ "$thefile1" = list.txt ]]; then
<something>
else
<something else>
fi
|
|
|
All times are GMT -5. The time now is 06:43 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|