LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-21-2008, 08:28 PM   #1
compused
Member
 
Registered: Oct 2006
Location: Melbourne Australia
Distribution: centos and redhat 8
Posts: 91

Rep: Reputation: 15
trying to link commands...looping


Hello there...
Could someone please help?

I am wanting a script to see if there are any .tif files in a directory and if so, to run the imagemagick program to convert them to .gif and then to delete the .tif originals. I can do the imagemagick conversion manually via command line. I am calling the script via cron, every one minute. But it seems to be looping and results in the computer crashing. Probably one minute is too frequent for a start...could someone please check this script. I put it together using examples found. I am sure there are some syntax errors. OS is centos 4. something

Thanks for anyone who can help.
Michael

#!/bin/sh
for file in $(ls /home/server_dir/_scan/*tif)
do
[ -s $file ] || continue

while true
do
ps -ef | grep /usr/local/bin/convert |grep -v grep
if test $? -ne 0
then
for i in /home/server_dir/_scan/*tif; do /usr/local/bin/convert $i /home/server_dir/_scan/$(basename $i tif)gif; done > /dev/null 2>&1
find /home/server_dir/_scan/*tif -exec /bin/rm -f {} \; > /dev/null 2>&1
else
: do nothing
fi

sleep 15
done
:
 
Old 12-21-2008, 09:05 PM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Your script I think is a bit too complex. How about something like:

Code:
#!/bin/sh
for file in $(ls /home/server_dir/_scan/*tif) do
	/usr/local/bin/convert $file /home/server_dir/_scan/$(basename $file tif)gif; 
	rm $file
done
(please use code tag when you post code).

Of course, with the above script I assume that the script finishes until next time it is started. You could avoid that somehow, but I would say it is easier just to increase the time between starts (to, let's say 5 minutes or something).

PS: your script can delete sometimes more files... When you do the find for tif you could find 'new' files, not processed by your script. My simple one doesn't have this problem..
 
Old 12-22-2008, 12:25 AM   #3
compused
Member
 
Registered: Oct 2006
Location: Melbourne Australia
Distribution: centos and redhat 8
Posts: 91

Original Poster
Rep: Reputation: 15
Thanks very much..this is the code I used:
Code:
#!/bin/sh
for file in $(ls /home/server_dir/_scan/*tif); do /usr/local/bin/convert $file /home/server_dir/_scan/dddd/$(basename $file tif)gif; > /dev/null 2>&1
	rm $file
done
It worked but gave error messages, esp in relation to the rm command (which failed to rm), and the "> /dev/null 2>&1" command failed to suppress output (line spaces inserted by myself):

Quote:
convert: unable to open image `20081212044800.tif': No such file or directory @ magick/blob.c/OpenBlob/2418.
convert: missing an image filename `/home/server_dir/_scan/dddd/20081212044800.gif' @ wand/convert.c/ConvertImageCommand/2710.
: ambiguous redirectf_new2.sh: line 2: 1
rm: cannot remove `20081212044800.tif\r': No such file or directory
convert: unable to open image `/home/server_dir/_scan/sarah': No such file or directory @ magick/blob.c/OpenBlob/2418.
convert: missing an image filename `/home/server_dir/_scan/dddd/sarahgif' @ wand/convert.c/ConvertImageCommand/2710.
: ambiguous redirectf_new2.sh: line 2: 1
rm: cannot remove `/home/server_dir/_scan/sarah\r': No such file or directory

convert: unable to open image `byrnes': No such file or directory @ magick/blob.c/OpenBlob/2418.
convert: missing an image filename `/home/server_dir/_scan/dddd/byrnesgif' @ wand/convert.c/ConvertImageCommand/2710.
: ambiguous redirectf_new2.sh: line 2: 1
rm: cannot remove `byrnes\r': No such file or directory

convert: unable to open image `20081202032924.tif': No such file or directory @ magick/blob.c/OpenBlob/2418.
convert: missing an image filename `/home/server_dir/_scan/dddd/20081202032924.gif' @ wand/convert.c/ConvertImageCommand/2710.
: ambiguous redirectf_new2.sh: line 2: 1
rm: cannot remove `20081202032924.tif\r': No such file or directory
: ambiguous redirectf_new2.sh: line 2: 1
rm: cannot remove `/home/server_dir/_scan/sh....20081124033036.tif\r': No such file or directory
[root@asterisk1 ~]#
 
Old 12-22-2008, 05:19 AM   #4
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Do you have spaces in your filenames ? If so this is one problem.

The problem with the output is that you put a ";" before the two redirects.

This should work then:

Code:
#!/bin/sh
IFS="\n"
for file in $(ls -1 /home/server_dir/_scan/*tiff); do
  /usr/local/bin/convert "$file" "/home/server_dir/_scan/$(basename "$file" tiff)gif" ;
  rm "$file";
done
IFS means internal field separator (I put it as newline). The "ls -1" puts each file on a different line. So, now, file will contain the correct file name with spaces. Because of this you need a lot of extra quoting.

To supress the output I would suggest putting ">/dev/null 2>&1" in the crontab....
 
Old 12-22-2008, 06:19 AM   #5
compused
Member
 
Registered: Oct 2006
Location: Melbourne Australia
Distribution: centos and redhat 8
Posts: 91

Original Poster
Rep: Reputation: 15
Thanks!
Will see how I go!
Michael
 
Old 12-22-2008, 08:09 AM   #6
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
The above solutions have a few issues.
  • Using a for with a ls can be a bit dodgy as it's all done through command line expansion which can fail if the expansion exceeds the maximum command line length. e.g. a large number of files.
  • They don't ensure that the matched filename is a regular file and not just a directory with .tiff on the end of its name.
  • Worst of all, no check for a successful conversion is done before removing the original file, which could result in data loss!
The following is a slightly more robust solution, which avoids the above issues and will also cope with filenames with spaces in them without having to mess with $IFS.
Code:
#!/bin/bash
#
# Directory to process
  CONVDIR='/home/server_dir/_scan/'    

find "$CONVDIR" -type f -name "*.tiff" | while read filename
  do
    /usr/local/bin/convert "$filename" "${filename%.tiff}.gif" && \
      rm "$filename"
  done
As is, it will process all sub-directories within the directory. If you want it to only process the directory passed and ignore sub-directories add -maxdepth 1 to the find command.
 
Old 12-22-2008, 02:58 PM   #7
compused
Member
 
Registered: Oct 2006
Location: Melbourne Australia
Distribution: centos and redhat 8
Posts: 91

Original Poster
Rep: Reputation: 15
Thanks very much Gazl!
The script does need to be reliable!
Rgds
Michael
 
Old 12-22-2008, 09:20 PM   #8
compused
Member
 
Registered: Oct 2006
Location: Melbourne Australia
Distribution: centos and redhat 8
Posts: 91

Original Poster
Rep: Reputation: 15
error message

Hi I get this error message:
Quote:
[root@asterisk1 ~]# sh /home/scripts/tif2gif_new2.sh
: command not foundif_new2.sh: line 7:
/home/scripts/tif2gif_new2.sh: line 15: syntax error near unexpected token `done'
/home/scripts/tif2gif_new2.sh: line 15: `done'
for this code:
(some comment lines removed for the copy paste)

Code:
#!/bin/sh

# Directory to process
  CONVDIR='/home/server_dir/_scan/'    

find "$CONVDIR" -type f -name "*.tif" | while read filename
  do
    /usr/local/bin/convert "$filename" "${filename%.tif}.gif" && \
      rm "$filename"
done
any ideas?
Michael
 
Old 12-23-2008, 05:00 AM   #9
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by compused View Post
Hi I get this error message:


for this code:
(some comment lines removed for the copy paste)

Code:
#!/bin/sh

# Directory to process
  CONVDIR='/home/server_dir/_scan/'    

find "$CONVDIR" -type f -name "*.tif" | while read filename
  do
    /usr/local/bin/convert "$filename" "${filename%.tif}.gif" && \
      rm "$filename"
done
any ideas?
Michael
it may be that the ${filename%.tif}.gif syntax isn't supported by whatever shell /bin/sh is on your system. If you change it back to /bin/bash it should work.

PS. If you have to stick with the /bin/sh shell then you can try the following instead.

Code:
newfile=$( echo "$filename" | sed 's/.tif$/.gif/' )
/usr/local/bin/convert "$filename" "$newfile" && \

Last edited by GazL; 12-23-2008 at 05:23 AM.
 
  


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
Not able to do looping in linux Amey Joshi Linux - Newbie 7 08-13-2008 03:25 AM
Can anyone post link to all the possible commands in linux with examples??(detailed) raga4223688 Linux - General 4 02-13-2007 04:13 AM
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 03:54 PM
login looping plaircpa Slackware 3 10-31-2005 08:10 PM
[C] for looping problem wuck Programming 6 10-19-2003 04:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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