| View previous topic :: View next topic |
| Author |
Message |
2tux-zx7r
Joined: Thu Apr 14, 2005 12:37 pm Posts: 66
|
Posted: Sun Jul 02, 2006 7:37 pm Post subject: Bash script only works once |
|
|
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 |
|
 |
nordle LXF regular

Joined: Fri Apr 08, 2005 10:56 pm Posts: 1497
|
Posted: Sun Jul 02, 2006 9:06 pm Post subject: RE: Bash script only works once |
|
|
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 |
|
| Back to top |
|
 |
TheDoctor LXF regular
Joined: Mon Jan 02, 2006 9:02 pm Posts: 325
|
Posted: Sun Jul 02, 2006 9:19 pm Post subject: |
|
|
| 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.
is the same as what yu've got |
|
| Back to top |
|
 |
nordle LXF regular

Joined: Fri Apr 08, 2005 10:56 pm Posts: 1497
|
Posted: Sun Jul 02, 2006 9:27 pm Post subject: |
|
|
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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Sun Jul 02, 2006 9:39 pm Post subject: |
|
|
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.
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  _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
nordle LXF regular

Joined: Fri Apr 08, 2005 10:56 pm Posts: 1497
|
Posted: Sun Jul 02, 2006 10:01 pm Post subject: |
|
|
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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Mon Jul 03, 2006 12:58 am Post subject: |
|
|
Not if there are no .jpg files in the current directory. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
TheDoctor LXF regular
Joined: Mon Jan 02, 2006 9:02 pm Posts: 325
|
Posted: Mon Jul 03, 2006 1:29 am Post subject: |
|
|
| 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 previous topic :: View next topic |
|