LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-14-2017, 02:14 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
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.
 
Old 05-14-2017, 02:36 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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.
Old 05-14-2017, 02:41 AM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Did you try:
Code:
#!/bin/bash
set -x
 
Old 05-14-2017, 05:19 AM   #4
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
[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
 
Old 05-14-2017, 05:23 AM   #5
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-14-2017, 05:28 AM   #6
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
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."
 
Old 05-14-2017, 06:18 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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.
Old 05-14-2017, 02:57 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,027

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
The real mistake is certainly the "$line" where line is not defined.
Some optimizations
Code:
IFS=$(echo -en "\n\b")
is short
Code:
IFS=$"\n\b"
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.
Old 05-15-2017, 09:48 PM   #9
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
Smile 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
 
Old 05-16-2017, 01:12 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,966

Rep: Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887
IFS was already suggested:
http://www.linuxquestions.org/questi...4/#post5613102
set -xv was already suggested (several times):
http://www.linuxquestions.org/questi...4/#post5611481
shellcheck was suggested:
http://www.linuxquestions.org/questi...1/#post5626779

why do you open several threads about the same script?
 
1 members found this post helpful.
Old 05-25-2017, 02:40 AM   #11
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
Exclamation 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
 
Old 05-25-2017, 02:51 AM   #12
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-25-2017, 03:32 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,966

Rep: Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887Reputation: 7887
I do not really understand, but probably:
Code:
if [[ "$thefile1" = list.txt ]]; then
   <something>
else
  <something else>
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Understanding how := works in Bash JockVSJock Programming 16 12-29-2015 02:58 PM
[SOLVED] bash color that works for any shell Hoxygen232 *BSD 8 01-22-2013 12:31 PM
Bash script works in terminal but not in cron. rivacom Linux - General 21 06-16-2011 10:22 AM
[SOLVED] rsync works differently under tcsh than bash? Vanyel Linux - Software 2 09-01-2009 09:55 AM
Running the bash with email and the works Dralnu Linux - Software 5 08-15-2005 05:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration