Hi. I really know very little about linux scripting. And I need help writing this script. The script is to get one parameter which is <directory>. And than the following will ensue;

for each image within directory
[INDENT]if not(image exist in temp-directory)
[INDENT]cp image to temp-directory and resize to 1024 pixels[/INDENT]
[/INDENT]

And I have a slight other problem :( the names may contain spaces and parenthesis I will need to somehow get over that barrier too.

Any help would be welcomed.

Thank you in advance

Recommended Answers

All 3 Replies

I think this has to be moved to shell scripting? Sorry for the trouble... :(

I wrote I python script for what I want but I would rather have a shell version as the Python version is dirty...

#! /usr/bin/python
# This Python file uses the following encoding: utf-8


#argv[1] needs to be send formatted meaning spaces and paranthesis

import os
import sys

__author__="john"
__date__ ="$Aug 16, 2010 1:36:23 PM$"

server_directory="/var/www/html/"

#for turkish characters
def tr(utf):
    return utf.decode('utf-8')

img_directory=sys.argv[1]
img_directory_orig=img_directory.replace("\ ", " ")
img_path=server_directory+"databackup/photos/"+img_directory_orig
dirList=os.listdir(img_path)
for fname in dirList:

    fake=fname.replace(" ", "_")
    fake1=fake.replace("(","_")
    fake_name=fake1.replace(")","_")

    fake_dir=img_directory_orig.replace(" ", "_")
    fake_dir1=fake_dir.replace("/", "_")
    
    if os.path.isfile(server_directory+"temp/"+"m_"+fake_dir1+"_"+fake_name):
        print "Exists!"
    else:
        if (fname<>"Thumbs.db"):
            
            real=fname.replace(" ", '\ ')
            real1=real.replace("(", '\(')
            real_name=real1.replace(")", '\)')
            command_string= 'cp '+server_directory+"databackup/photos/"+img_directory.replace(" ","\ ")+"/"+real_name+" "+server_directory+"temp/m_"+fake_dir1+"_"+fake_name
            os.system(command_string)
            second_command_string = 'mogrify -resize 1024 '+server_directory+"temp/m_"+fake_dir1+"_"+fake_name
            os.system(second_command_string)

Take a look at the "find" command and also TEST when used with "if [ ] then"
For example the following find command will generate a list of files starting in the directory name provided as the first parameter and include the backslash to escape characters in the name:

#!/bin/bash
find $1 -type f -ls | awk '{ print $11 }'
#!/bin/bash
for x in `find . -type f -ls | awk '{ print $11 }'`
do
if [ -s $x ] then
echo "Found file $x "
if
done

from a command line do:
man find
man if
man test

for some help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.