Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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..
#!/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 ~]#
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....
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.
[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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.