LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-17-2003, 01:38 PM   #1
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Rep: Reputation: 15
bash script


i have the following script i am writing to convert and move some files after an upload...
The first part of renaming the files works ok but then it does not get to the conversion part
can anyone point out what i am missing?

tia

#!/bin/bash
#####################################################################
# setup variables
#####################################################################
IMAGEDIR='/home/image/convert'
DATE=`date +%m-%d`
JPG='/usr/local/apache/htdocs/imagegallery/admin/mainimages/tmp/'
TIF='/usr/local/apache/htdocs/imagegallery/admin/mainimages/'
ERRORS='/tmp/errors'
LIST='/tmp/list'
RESULTS='/tmp/results'
CONVERTED='/tmp/converted'
#####################################################################
# Convert files and log actions
#####################################################################
#
# Create three files - error, list and results.
#
# list = file listing of all files converted that day (size and name)
# errors = errors generated by conversion
# converted = output of file conversion
# results = results or errors of the conversion process
#
#
#####################################################################
cd ${IMAGEDIR}
# Rename files with spaces - replace the spaces with a _
#
find ${IMAGEDIR} -name '* *' -type f | sort | while read FILE
do
NEWFILE=`dirname "${FILE}"`/`basename "${FILE}" | sed 's/ /_/g;'`
mv "${FILE}" "${NEWFILE}"
#
# Populate list of files > generated each time and cleaned out at the end of the daily script
ls -lh ${IMAGEDIR} | awk ' { print $9 " " $5 } '>>$LIST
#
for files in ${IMAGEDIR}
do
(
echo converting ${files} >>$CONVERTED
#
# echo output from conversion to converted log file/convert the files/move the files and set permissions
#
echo "convert ${files} -resize 800x600 -colorspace rgb ${files}.jpg >>$CONVERTED 2>&1"
#
# mv files to web space and set permissions (echo put in for testing)
#
echo "mv ${files} ${TIF}${files}_${DATE}.tif"
echo "mv ${files}.jpg ${JPG}${files}_${DATE}.jpg"
echo "cd ${TIF}"
echo "chown nobody:nobody *.tif"
echo "chmod 664 *.tif"
echo "cd ${JPG}"
echo "chown nobody:nobody *.jpg"
echo "chmod 664 *.jpg"
)
done
#
# Generate Result section (Part 4)
#
#
cat $CONVERTED |egrep "converting|error" >>$RESULTS
#
# Generate Errror section (Part 5)
#
cat $RESULTS |egrep "error|delegate" |awk ' { print $ 2 }' >>$ERRORS
#
cat /dev/null >$CONVERTED
cat /dev/null >$LOGFILE
 
Old 07-17-2003, 01:49 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
You do realise that doing:
echo "mv ${files} ${TIF}${files}_${DATE}.tif"
echo "mv ${files}.jpg ${JPG}${files}_${DATE}.jpg"
echo "cd ${TIF}"
echo "chown nobody:nobody *.tif"
echo "chmod 664 *.tif"
echo "cd ${JPG}"
echo "chown nobody:nobody *.jpg"
echo "chmod 664 *.jpg"

will not carry out the commands
 
Old 07-17-2003, 02:03 PM   #3
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
yup

look above the statements...

# mv files to web space and set permissions (echo put in for testing)
 
Old 07-17-2003, 02:11 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
So.. does it not get to that point or something?

If so why not change your for statement from:
for files in ${IMAGEDIR}

to:
IFS="
"
for files in `cat $LIST`
 
Old 07-17-2003, 02:39 PM   #5
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
could you explain what that statement means please?
(can't use it but i'm curious :-)

also , $LIST is also populated throughout the day as a master list of what files were changed that day. It will contain more that what is in the IMAGEDIR as conversions happen from other locations..

I need to be able to say for files in the $IMAGEDIR do ..... but i'm not sure how to specify the files in the directory..

thanks for your help.

S.
 
Old 07-17-2003, 03:06 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Why not use the ls command in the for loop.

The:
IFS="
"

Just sets the field delimitor to a newline.
 
Old 07-17-2003, 03:15 PM   #7
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
not sure of the syntax for it, i need to convert only the files...

also, do you know how i would pause between the first commands that search and rename the files if they contain spaces before moving onto the conversion? As i test it is not waiting for this process to complete, the files with the spaces in their names then crap out when i try to move them...
 
Old 07-18-2003, 06:32 AM   #8
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
You have 2 do loops but only one done statement. Maybe this is just a typo?
Quote:
do you know how i would pause between the first commands that search and rename the files if they contain spaces before moving onto the conversion?
echo "Press enter to continue...";read
or
echo "Taking a short nap...";sleep 5


What part doesn't work?

Last edited by slapNUT; 07-18-2003 at 06:34 AM.
 
Old 07-18-2003, 07:15 AM   #9
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 18
One line messes it all up:

for files in ${IMAGEDIR}

You probably mean

for files in ${IMAGEDIR}/*

The difference is that characters like * and ? are expanded by the shell to make them match with the contents of your file system. The rules involved are called globbing and take place after the IMAGEDIR variable is substituted for its value.

If you need more info, try man bash and man glob.
 
Old 07-18-2003, 07:34 AM   #10
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
thanks guys you were both right - i was missing the done statement (my bad

Also, thanks for the globbing info - you learn somethin new every day...

the script now works as needed.

Cheers.

S.
 
  


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 script Linh Programming 4 04-21-2004 05:19 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script brian0918 Programming 7 06-12-2003 06:06 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

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

All times are GMT -5. The time now is 07:42 AM.

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