Bash script only works once

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Bash script only works once

Postby 2tux-zx7r » Sun Jul 02, 2006 6:37 pm

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
2tux-zx7r
 
Posts: 66
Joined: Thu Apr 14, 2005 11:37 am

RE: Bash script only works once

Postby nordle » Sun Jul 02, 2006 8:06 pm

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 :)
I think, therefore I compile
User avatar
nordle
LXF regular
 
Posts: 1500
Joined: Fri Apr 08, 2005 9:56 pm

Postby TheDoctor » Sun Jul 02, 2006 8:19 pm

Code: Select all
#! /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: Select all
for file in *; do


is the same as what yu've got
TheDoctor
LXF regular
 
Posts: 325
Joined: Mon Jan 02, 2006 9:02 pm

Postby nordle » Sun Jul 02, 2006 8:27 pm

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
User avatar
nordle
LXF regular
 
Posts: 1500
Joined: Fri Apr 08, 2005 9:56 pm

Postby nelz » Sun Jul 02, 2006 8:39 pm

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

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

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

It saves you screwing things up with a typo.

Code: Select all
for file in *

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

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


Or you could install KPhotoAlbum and assign multiple attributes to every file, individually or in groups :)
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8546
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby nordle » Sun Jul 02, 2006 9:01 pm

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
User avatar
nordle
LXF regular
 
Posts: 1500
Joined: Fri Apr 08, 2005 9:56 pm

Postby nelz » Sun Jul 02, 2006 11:58 pm

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)
User avatar
nelz
Site admin
 
Posts: 8546
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby TheDoctor » Mon Jul 03, 2006 12:29 am

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).
TheDoctor
LXF regular
 
Posts: 325
Joined: Mon Jan 02, 2006 9:02 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests