Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bash script only works once

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
2tux-zx7r



Joined: Thu Apr 14, 2005 12:37 pm
Posts: 66

PostPosted: Sun Jul 02, 2006 7:37 pm    Post subject: Bash script only works once Reply with quote

After months of nagging from my missus I have gotten round to sorting out the near thousand digital pictures we have been storing on our hard disk

I've written a little script so that once I've collected related pictures with nifty and memorable titles like p100101.jpg into a temporary folder I can batch rename them to something that makes sense. I'm no programming guru and getting the following to work took me about two hours today !

So it worked on a load of test pictures and I was made up. But the second test failed with the error message:

mv: cannot stat `*': No such file or directory

Here's my bash script:

#! /bin/bash
# script to rename a collection of photos in a single dir

echo "What is the working directory? (include the final backslash)"
read workdir

echo "What is the base filename?"
read basefile

echo "What is the number of the last picture?"
read filecount

cd $workdir
for file in *
do
let filecount=filecount+1
mv $file "$basefile$filecount.jpg"
done

Someone please tell me what I've done wrong

John
Back to top
View user's profile Send private message
nordle
LXF regular


Joined: Fri Apr 08, 2005 10:56 pm
Posts: 1500

PostPosted: Sun Jul 02, 2006 9:06 pm    Post subject: RE: Bash script only works once Reply with quote

for file in *; do


Also, you can add
set -x
at the start of your script to enable debugging, makes it easy to see eactly what the problem is...most of the time Smile
_________________
I think, therefore I compile
Back to top
View user's profile Send private message
TheDoctor
LXF regular


Joined: Mon Jan 02, 2006 9:02 pm
Posts: 325

PostPosted: Sun Jul 02, 2006 9:19 pm    Post subject: Reply with quote

Code:

#! /bin/bash
# script to rename a collection of photos in a single dir

echo "What is the working directory? (include the final backslash)"
read workdir

echo "What is the base filename?"
read basefile

echo "What is the number of the last picture?"
read filecount

cd $workdir
for file in *
do
let filecount=filecount+1
mv $file "$basefile$filecount.jpg"
done


You've not said what your input was in each of your two cases.

A couple of points:

You don't check that you've pointed to the working directory that you've put the files into. Are you sure you didn't mistype it? The error loks suspiciously like what you get if you try the script in an empty directory.

Code:

for file in *; do


is the same as what yu've got
Back to top
View user's profile Send private message
nordle
LXF regular


Joined: Fri Apr 08, 2005 10:56 pm
Posts: 1500

PostPosted: Sun Jul 02, 2006 9:27 pm    Post subject: Reply with quote

oh ok, I thought it was fussy about having the semi colon, guess not.

But yeah, a dir test would help:

if [ ! -d $workdir ]; then
echo "$workdir does not exist"
exit 1
fi
cd $workdir
_________________
I think, therefore I compile
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8453
Location: Warrington, UK

PostPosted: Sun Jul 02, 2006 9:39 pm    Post subject: Reply with quote

It is fussy about the semi-colon, but only if the two commands (for and do) are on the same line.

Replace
Code:
echo "What is the working directory? (include the final backslash)"
read workdir

with
Code:
workdir=$(basename $(pwd))

It saves you screwing things up with a typo.

Code:
for file in *

will cause problems if you have no files, or some directories. A safer approach is

Code:
find -type f | while read file do...


Or you could install KPhotoAlbum and assign multiple attributes to every file, individually or in groups Smile
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
nordle
LXF regular


Joined: Fri Apr 08, 2005 10:56 pm
Posts: 1500

PostPosted: Sun Jul 02, 2006 10:01 pm    Post subject: Reply with quote

If you know the files are always going to be jpg, then
for file in *.jpg; do
should be ok too.
_________________
I think, therefore I compile
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8453
Location: Warrington, UK

PostPosted: Mon Jul 03, 2006 12:58 am    Post subject: Reply with quote

Not if there are no .jpg files in the current directory.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
TheDoctor
LXF regular


Joined: Mon Jan 02, 2006 9:02 pm
Posts: 325

PostPosted: Mon Jul 03, 2006 1:29 am    Post subject: Reply with quote

nelz wrote:
Not if there are no .jpg files in the current directory.


Which is what I think the problem is. And so I vote for nelz's amendment (find etc).
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast