LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-09-2011, 08:42 AM   #1
fabtk
LQ Newbie
 
Registered: Mar 2011
Location: Switzerland
Distribution: Debian Squeeze
Posts: 8

Rep: Reputation: 0
Question BASH eval + command


Hi folks

Got the following problem:

Code:
ASDF=`exiv2 -g Iptc.Application2.Caption -Pv /home/fabio/picture.jpg`	
echo $ASDF
the code above is working, but it should look like the code below because I'm looping through some pictures, but I'm getting a syntaxerror:
Code:
eval ASDF${i}=`exiv2 -g Iptc.Application2.Caption -Pv /home/fabio/picture.jpg`
eval echo \$ASDF$i
./crawler.sh: eval: line 55: syntax error in conditional expression: unexpected token `('
(line 55 is the line where I execute the exiv2 command and put it into $ASDF$i)

by the way - these two lines are in my for loop that looks this way:
Code:
# search for JPG files in all ftp userfolders and create an imagelist if there were pictures uploaded
COUNTPICTURES=`find "$USERDIR"/*/ftp/*.jpg | wc -l`
if [ "$COUNTPICTURES" != 0 ]; then
	find "$USERDIR"/*/ftp/*.jpg > $SCRIPTDIR/$IMAGELIST
	# separate imageinformations into vars
	for ((i=1; i<=$COUNTPICTURES; i++)) do
		eval IMAGEPATH${i}=`cat "$SCRIPTDIR"/"$IMAGELIST" | sed -n "$i"p`
		eval IMAGENAME${i}=`cat "$SCRIPTDIR"/"$IMAGELIST" | cut -d'/' -f6 | sed -n "$i"p`
		eval USERIMAGE${i}=`cat "$SCRIPTDIR"/"$IMAGELIST" | cut -d'/' -f4 | sed -n "$i"p`

		### ASDF STUFF WOULD BE IN HERE ###

	done
fi
What I'm doing wrong?

Thanks for your answers.

Greetings from Switzerland - Fabio

Last edited by fabtk; 03-09-2011 at 08:44 AM.
 
Old 03-09-2011, 08:53 AM   #2
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
What I see is, that you "abuse" find - in fact, it's not executed in the way you expect it at all. It's feed with a list of files (expanded by the shell) and outputs exactly this list.

As you want variables name foobar1, foobar2, ...: what about using an array instead?
Code:
ASDF[$i]=`...`
 
Old 03-09-2011, 11:18 AM   #3
fabtk
LQ Newbie
 
Registered: Mar 2011
Location: Switzerland
Distribution: Debian Squeeze
Posts: 8

Original Poster
Rep: Reputation: 0
vor lauter Bäumen den Wald nicht mehr sehen ...
// not see the forest for the trees...

Ok did it with arrays and it works. What did I do wrong with the find?

My directorytree (where users can upload stuff) looks like this:

/home/users/donvitocorleone/ftp/
/home/users/michaelcorleone/ftp/
/home/users/tomhagen/ftp/

ofc my find command should only crawl these ftp directories in /home/users/XY/ftp. Tons of pictures in e.g. /home/users/donvitocorleone/backup/* should be ignored in my search.

Greetings and thanks for your hint!
 
Old 03-09-2011, 12:12 PM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
The find will search in the given locations. The given locations are in your case:
Code:
"$USERDIR"/*/ftp/*.jpg
But there is no expression given which will constrain the output. You could also use:
Code:
ls "$USERDIR"/*/ftp/*.jpg
in place of the find. To use find in a way where it actually searches the directory could be:
Code:
$ find "$USERDIR" -name "*.jpg"
or to limit it to the directories having ftp therein:
Code:
$ find "$USERDIR" -path "*/ftp/*.jpg"
It could be speed up by writing the temporary file first, and then count the lines therein.

I don't know the complete script, but the expression you used originally can also be used in the for loop, maybe it's possible to do it even without the list of file:
Code:
i=0
for NAME in "$USERDIR"/*/ftp/*.jpg; do
    IMAGEPATH[$i]="$NAME"
    IMAGENAME[$i]=`basename "$NAME"`
    USERIMAGE[$i]=`expr "$NAME" : ".*/\([^/]*\)/.*/.*"` # Get the third to last part of the path
    ...
    let i++
done
 
Old 03-09-2011, 02:42 PM   #5
fabtk
LQ Newbie
 
Registered: Mar 2011
Location: Switzerland
Distribution: Debian Squeeze
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks a lot for your repost - what a nice forum, fast and qualified answers

Code:
find "$USERDIR" -path "*/ftp/*.jpg"
Runs very fast and a big plus, no output if there are no objects found. Now I just need to get it to work with jpg + JPG, {jpg,JPG} isn't working, maybe I should read the find manpage.

No eval command is left in my script, could replace all through array-function.

Thanks a lot for your help

Fabio

// edit: just checked your solution with the find command inserted into the loop, would work as well but the find command on an extra line keeps the script more clear - maybe it would be faster but this script is so small, it wouldn't matter.

// edit2:
Code:
Just replace -path with -ipath.
thanks!

Last edited by fabtk; 03-09-2011 at 03:03 PM. Reason: big thanks :)
 
Old 03-09-2011, 02:46 PM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Just replace -path with -ipath.
 
  


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
bash if-then-else eval question kha.t.nguyen Programming 4 02-08-2011 11:32 AM
[SOLVED] Using eval in Bash pnelsonsr Programming 3 11-24-2010 02:45 PM
how to send the output of a command in a file defined by a eval $"$var1" toordog Programming 3 07-07-2010 04:26 PM
[SOLVED] Effect of using eval to execute a command as opposed to writing it on the commandline daudiam Programming 1 06-20-2010 01:11 AM
Basic Bash: How to use eval to evaluate variable names made of arbitrary strings. GrapefruiTgirl Programming 9 12-16-2009 10:25 AM

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

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