LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-23-2017, 01:23 PM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 212

Rep: Reputation: Disabled
bash script doesn't work.


Code:
#!/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
Attached Files
File Type: txt creditcheck.txt (1.3 KB, 8 views)

Last edited by L_Carver; 02-23-2017 at 01:25 PM. Reason: Noun-verb disagreement
 
Old 02-23-2017, 01:28 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,521
Blog Entries: 3

Rep: Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567
If you add the following two lines below the first line, you can see exactly what is being presented to the shell by the script:

Code:
set -x
set -e
See the manual page for bash for info and other options.
 
Old 02-23-2017, 11:29 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,565

Rep: Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425
I usually use 'set -xv'.
Try this page to see what the set cmd options do https://www.gnu.org/software/bash/ma...he-Set-Builtin
 
Old 02-24-2017, 01:15 AM   #4
nodir
Member
 
Registered: May 2016
Posts: 179

Rep: Reputation: Disabled
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).

Last edited by nodir; 02-24-2017 at 01:36 AM.
 
Old 02-24-2017, 04:23 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,510

Rep: Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456
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.

Last edited by pan64; 02-24-2017 at 04:25 AM.
 
Old 02-24-2017, 07:24 AM   #6
nodir
Member
 
Registered: May 2016
Posts: 179

Rep: Reputation: Disabled
shellcheck is a good starting point, but it doesn't cover style or general workflow problems.
 
Old 02-25-2017, 04:25 PM   #7
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 212

Original Poster
Rep: Reputation: Disabled
Confusing (confused by?) shellcheck

Quote:
Originally Posted by pan64 View Post
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 04:32 PM. Reason: Took a new course with the script.
 
Old 02-25-2017, 06:50 PM   #8
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 212

Original Poster
Rep: Reputation: Disabled
"nocredit.txt" not being written

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.

Carver
 
Old 02-26-2017, 03:18 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,510

Rep: Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456
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.
 
Old 02-26-2017, 04:01 AM   #10
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
Quote:
Originally Posted by L_Carver View Post
[code]#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
function gettext () {

Carver
When creating a function, either do:

function funcName {
...
}

or

funcName(){
...
}

As far as I know, you can't have both the 'function' keyword and the parentheses. May worth a try.
 
Old 02-27-2017, 03:30 AM   #11
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 212

Original Poster
Rep: Reputation: Disabled
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?

Carver
 
Old 02-27-2017, 03:31 AM   #12
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 212

Original Poster
Rep: Reputation: Disabled
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?

Carver
 
  


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] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 06:57 AM
[SOLVED] My bash script doesn't work massy Programming 10 02-03-2014 07:59 AM
bash script to start process doesn't work? shams Linux - General 4 08-19-2013 12:09 AM
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 01:05 AM
Root user check in bash script doesn't work tawalker Programming 6 12-18-2011 04:06 AM

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

All times are GMT -5. The time now is 10:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration