Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-04-2010, 10:17 AM   #1
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
BASH Script, command line, same commands, different returns (need help).

I'm trying to write a bash script to process jpeg files and replace one IPTC4 tag, "Fixture Identifier," that I know all of them have in them, using Andreas Huggel's exiv2 command-line utility.

I confess to using the IPTC fields my own unique way, and I have been using "Fixture Identifier" (aka "Event" in MS Expression Media) as a 'refinement' on "Transmission Reference," to which for years I've been applying short, often catchy descriptive two- and three-word phrases to describe the content of the pictures.

Okay, enough background. Here's the code for the script so far. What I decided to do first was have the user type in the full filename, with extension when applicable, and have exiv2 return the Fixture ID tag as it was presently written in the file. I've been, for at least three and a half hours, using the same jpeg file as a test file. I happen to know that the value of 'FixtureID' in this file is "Bust-ing Out." What Im puzzled by is how nearly identical code can give back such un-identical information.

Pasted into rxvt, this code works:

exiv2 -PInv bv1329-014.jpg | grep 'FixtureId' | sed 's/^FixtureId[       ]*//'
Which means it answers back with both words in the FixtureID tag "Busting" and "Out" (no quotes oc).

In the script I'm writing, the same code looks like this (employing a variable to be echo'd later)

oldf1=(`exiv2 -PInv $gfile | grep 'FixtureId' | sed 's/^FixtureId[       ]*//'`)
When the script is run, I only see "Busting" (nq).

What's missing? Or is there a better way to go about it?

Old 02-04-2010, 10:34 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Originally Posted by SilversleevesX View Post
oldf1=(`exiv2 -PInv $gfile | grep 'FixtureId' | sed 's/^FixtureId[       ]*//'`)
The ( ) tells bash to start a subshell, to run the given commands and to write the output to stdout (the ` ` does not change anything) and then to assign the expression to $oldf1. Problem is, bash parses the expression so the effect is
oldf1=Busting Out
which makes bash assign Busting to $oldf1 and then run an Out command with that variable assignment in effect. Probably there is no Out command in the directories defined by $PATH so it should (TM) produce an error message.

You can achieve what you want with
oldf1=$(exiv2 -PInv $gfile | grep 'FixtureId' | sed 's/^FixtureId[       ]*//')
I have replaced ` ` with $( ) because it is preferred, for the reasons explained here.

Last edited by catkin; 02-04-2010 at 10:37 AM. Reason: Speeling and added about probably no Out command.
1 members found this post helpful.
Old 02-04-2010, 11:02 AM   #3
Registered: May 2009
Posts: 181
Blog Entries: 9

Original Poster
Rep: Reputation: 15
Many thanks catkin.

I honestly wasn't expecting such a quick reply. I'll try the better syntax in a few minutes; no doubt it will improve things drastically.

And here, after making my alpha post to this thread, I went Google-ing to see if anyone else had suggestions. I would have been better off to stick around these parts.

I have to agree with GreyCat. To my way of thinking, we all routinely use the other punctuation mark (the tilde) on the same key as the backtick, and that is more than enough. One shouldn't strain one's pinky if there's a better way to do something.

Provided this change works (and there's no reason at all to think it shouldn't), I can get on with the part of the script I was more confident about to begin with: having exiv2 trash the old FixtureId value for a new one read in from user input. Of course, I'll write in an "Are you sure you want to make this change?" as a good, working 'bail out' option, but on the whole, I don't think it will see much use.

Thanks again.

Old 02-04-2010, 11:47 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
By the way, there's no need to use both grep and sed here, as sed has equivalent line-matching ability. Though you do have to invert it with -n and the 'p' command so that it prints only the lines that match.
oldf1=$(exiv2 -PInv $gfile | sed -n '/FixtureId/ s/^FixtureId[       ]*//p')
And really, you don't even really need the address field with the above at all, since the "s///" part will match anyway. But it does make sed a tad more efficient when it doesn't have to attempt substitutions on every line.
Old 02-04-2010, 03:46 PM   #5
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 98
Blog Entries: 1

Rep: Reputation: 19
The "$( ....)" in the assignment probably best done as:
Because if it returns more than on white spaced tokens you want all of them. If you want to reference the those tokens in a random fashion, then
variable=( $(.....) )
Which is a array assignment. ${#variable} returns the number of items in the array. ${variable[0]} returns the first item in the array and ${variable[*]} returns all the values as a string.


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
works on command line but not in bash script tara Linux - General 7 02-09-2009 03:57 AM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
[SOLVED] [bash]difference between script on command line and in file Wim Sturkenboom Programming 4 09-08-2008 02:20 AM
Command line image viewing for bash script? Romanus81 Programming 3 08-10-2008 11:39 PM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:48 PM.

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