LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2011, 06:11 AM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
Order of "attack" in BASH scripts?


Let me explain what I mean with this example.

I have a script that is the "slimming down and joining" of two other scripts written to perform very similar tasks on JPEG files. One of the scripts read in filenames from a text file and annotated, while the other trimmed four-letter "hint" substrings from the file names. Here's the code of the amalgamated script:
Code:
#!/bin/bash
exec 5<fields.txt
while IFS="," read -u5 FILE CATE CRED SOUR WRIT TRANS FIXID OBJNM LOCN
do
	fname=`basename "$FILE"`
	echo -e "Working on item $fname now."
		exiv2 -kM"set Iptc.Application2.Category String $CATE" $FILE
		exiv2 -kM"set Iptc.Application2.Credit String $CRED" $FILE
		exiv2 -kM"set Iptc.Application2.Source String $SOUR" $FILE
		exiv2 -kM"set Iptc.Application2.Writer String $WRIT" $FILE
		exiv2 -kM"set Iptc.Application2.TransmissionReference String $TRANS" $FILE
		exiv2 -kM"set Iptc.Application2.FixtureId String $FIXID" $FILE
		exiv2 -kM"set Iptc.Application2.ObjectName String $OBJNM" $FILE
		exiv2 -kM"set Iptc.Application2.SubLocation String $LOCN" $FILE
pic=$[pic+1]
done
exec 5<&-
for file in $(ls *.jpg);
do
	striM="$file"
	j=${file:(-4)}
	k=${file%????????}
	newname="$k$j"
	echo -e "File $file's new name will be $newname."
done
for file in $(ls *.jpg);
	do
		striM="$file"
		j=${file:(-4)}
		k=${file%????????}
		newname="$k$j"
		mv $file $newname
		exiv2 -M"set Xmp.iptc.SubjectCode XmpBag $striM" -k $newname
		echo -e "File $file has been renamed."
done
When I run it, what seems to happen is that the shell (subshell?) performs the renaming and then invokes Exiv2 to do the tagging. But of course at that point I'm getting a lot of
Code:
Error: fooswitter99900blon.jpg - Unable to open file
type messages from the latter.

As I look at this script this morning, it occurs to me that the BASH shell is performing the task which it can do in its default IFS -- the renaming -- and then giving that up and tagging under the "while ifs=','" customized one.

Is there a way to change the "order of attack," ie, write the script to perform the tagging function first and then rename (which is what I had in mind when I wrote it) the files?

BZT

Last edited by SilversleevesX; 06-21-2011 at 06:13 AM.
 
Old 06-21-2011, 11:07 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722
Could you at least tell me at what line in the script does the error occur?
 
1 members found this post helpful.
Old 06-22-2011, 01:34 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,832

Rep: Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089
So i think we have covered this before, but using ls to feed a loop is generally a bad idea if word splitting can occur.
The first for loop does nothing so I am not sure why that is there.

Other than that, I am with MTK358 in that we need to be told where the issue is occurring before we can really help as your explanation is a little vague.

Also, have you tried putting set -x at the start of the script to see if what you expect is occurring where you want (or at least provide exact location of where the process
is breaking down)?
 
1 members found this post helpful.
Old 06-22-2011, 03:09 AM   #4
SilversleevesX
Member
 
Registered: May 2009
Posts: 181

Original Poster
Blog Entries: 9

Rep: Reputation: 15
I was thinking about all this AFK, and I realized that having two loops in the script was the wrong way to go about it. If it tagged in the "while IFS= / do" loop, I reasoned, then adding the renaming task to that loop should make everything work without errors. Whatever the "do-this first, do this-second" inherent in BASH's handling of tasks, if you put everything in one loop, it should (and did) work.

The script as I rewrote it is on another machine, but I'll try to approximate the way I fixed it here:
Code:
...
j=${FILE:(-4)}
k=${FILE%????????}
newname="$k$j"
echo -e "File $FILE's new name will be $newname."
mv $FILE $newname
exiv2 -M"set Xmp.iptc.SubjectCode XmpBag $FILE" -k $newname
echo -e "File $FILE has been renamed."
All the foregoing appears in between the last exiv2 -kM"set..." command and the lines done and exec 5<&-, in other words the end of both the loop and the reading in/out of the file "fields.txt".

At any rate it works. Just took a little time to think about how to approach it.

BZT

Last edited by SilversleevesX; 06-22-2011 at 03:11 AM. Reason: Missed a changed variable name -- corrected it.
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Seeking for "Synchronise" and "patients" projects/scripts of Visual Basic LAN-Dominator.nl Programming 2 07-05-2008 07:21 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Using BASH redirects and "prompt" files to interact with commands/scripts ?? swedish_lunacy Linux - Newbie 22 07-27-2006 08:33 AM
.kde/Autostart bash scripts "file not found" Nice-n-Slow Linux - Software 1 07-02-2004 05:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:44 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