LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-15-2020, 08:21 PM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
Been a long time: another broken script


These commands
Code:
cap=$(exiv2 -g Iptc.Application2.Caption  -Pv "$fg")
	if [[ -f "$cap" ]]; then
		echo -e "$fg \e[1mhas\e[0m a Caption."
	clen=${#cap}
	echo -e "The caption for $fg is $clen characters long (spaces included)."
	echo -e "$fg:\\t$clen" >> captionlengths.txt
	fi
work when C&P'd (cut and pasted), but not when run as a script (see attached).
(Aside: I'm too much of a cheesehead nowadays to learn printf without having a book like the ABS, sorry about that.)

As usual, please advise corrections (preferably using echo's, .)
Oh and my DM is now Kubuntu 20.04 and change (whatever the latest maint. upgrade is).

Carver
Attached Files
File Type: txt caplength2list.txt (407 Bytes, 18 views)

Last edited by L_Carver; 11-15-2020 at 10:58 PM.
 
Old 11-15-2020, 08:25 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
It would help enormously if you stated what you expect from the script and what happens when you run it as a script. The attached file doesn't show anything failing.

Also, I doubt the meaning of C&P is clear to everybody (not to me anyway), so you may want to clarify that.
 
1 members found this post helpful.
Old 11-15-2020, 11:03 PM   #3
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
It would help enormously if you stated what you expect from the script and what happens when you run it as a script. The attached file doesn't show anything failing.

Also, I doubt the meaning of C&P is clear to everybody (not to me anyway), so you may want to clarify that.
My issue is, when I run it, the script hangs and doesn't echo the length of each caption or write the text file (at all). I get that when I cut and paste line 20, but again, not when the script runs.

I hope that explains what I'm looking for help with.

Carver
 
Old 11-15-2020, 11:55 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Have you tried running the script with the -x option? E.g. bash -x nameofyourscript. It outputs the details of each command it executes.

If the script doesn't execute the echo's, I would assume that there is no file named $cap and investigate why. When you leave the echo $fg in, is the output as expected?
 
Old 11-16-2020, 12:55 AM   #5
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
The attached file and the code you pasted are not identical; how would they give the same results?
I don't understand what you're getting at.
What if you reduce the script to the exact same code that works when "C&P"'d on the command line?
How can it even work on the command line when $fg isn't defined?

Please always show us actual output of actual commands ("C&P" the terminal output) instead of praphrasing.
 
Old 11-16-2020, 01:17 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
cap is not a file (if I understand well) so [[ -f $cap ]] is just wrong.
but as it was already told: we cannot suggest you anything if we have no idea: what is it all about.
 
Old 11-16-2020, 01:08 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Thumbs up

Quote:
Originally Posted by pan64 View Post
cap is not a file (if I understand well) so [[ -f $cap ]] is just wrong.
but as it was already told: we cannot suggest you anything if we have no idea: what is it all about.
And what is the script supposed to do if "$cap" doesn't refer to a file? Or is a null string?

I'd suggest the OP's coding be more defensive and issue error messages when bad/incorrect data is encountered (which could very be the norm and not the exception) rather than simply forging ahead and feeding it into the rest of the script where weird errors can occur.

Something like:
Code:
if [ -z "$cap" ]; then
    echo "FATAL: You must specify a file!"
    RC=1
else
    if [ ! -f "$cap" ]; then
        echo "FATAL: \"$cap\" is NOT a file!"
        RC=1
    else
        #
        # Do something using the file...
        #
        ...
        RC=0 (unless other errors occur)
    fi
fi
exit $RC
as a start. Too much code gets written as though nothing will ever fail. Burns you every time.
 
Old 11-16-2020, 03:02 PM   #8
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,606

Rep: Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549
Quote:
Originally Posted by rnturn View Post
And what is the script supposed to do if "$cap" doesn't refer to a file? Or is a null string?
You've misread something.

What the script appears to be attempting is appending a list of image filenames and caption lengths to a file, but only when the caption contains a value.

What Pan is saying is that the command "[font=monospace]cap/font]" is populated from doesn't give a filename - it gives IPTC Caption metadata - and nothing in the rest of the script implies that caption contains anyhting other than just text, so the file test is most likely meaningless, and (based on the then output) should probably be -n instead.


Another problem with the script is using "white read fg" when it should probably be "while IFS= read -r fg" (or similar).


Last edited by boughtonp; 11-16-2020 at 03:04 PM.
 
  


Reply



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
Long time member, long time *nix user, about time to say hi. stuman LinuxQuestions.org Member Intro 0 10-15-2013 07:35 AM
Aboout "with very long lines",how long is very long? yun1st Linux - Newbie 4 07-20-2012 03:38 PM
long long long: Too long for GCC Kenny_Strawn Programming 5 09-18-2010 01:14 AM
Another long time member, first time poster jsteve LinuxQuestions.org Member Intro 1 11-26-2008 02:05 PM
It's been a while... a long, long while CharlieG General 4 01-21-2005 02:39 AM

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

All times are GMT -5. The time now is 11:10 AM.

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