Matching a file with extension tmp

Hi all,

I am writing a shell script for processing files in a directory.

I need to read files in the directory and process them and write it to another file.

For example, if the directory contains the following files,
file1,file2,file3

I want to process these files and create file1_tmp,file2_tmp,file3_tmp.

If the "_tmp" files are present already the should be deleted and recreated using the script.

I am using the following script.But I am not able to do it correctly.
#!/usr/ksh
COUNTER=0;
echo "Enter the directory path";
read DIRECTORY;
echo $DIRECTORY;
for FILE in `cd $DIRECTORY; ls -l | grep -v total | awk '{printf (" %-50s \n",$9) }'`;
do
A=`echo "$FILE" |sed -n -e '/_tmp$/p'`
if [ "$A" = "*_tmp" ]
then
COUNTER=`expr $COUNTER + 1`;
array[$COUNTER]=$A;
fi
done;

Can any one help me on this?

Thanks in advance

Hi, first of all, do You only want the file names? Because I don't see much processing other than that going on. Then Your ls|grep|awk is unnecessary, and not portable. And maybe You are only interested in files with names starting with file and ending with a number? Then use that instead. And I think that the use of a wildcard in the if statement isn't really doing what You expect it to do. And You are only trying to "do" something with files called *_tmp, even though You said that that is what they should be renamed to after You have processed the original file? And I see no renaming going on.

I think You better rephrase Your question and give us a more detailed description. :wink: What You want to do with the files before You rename them for example.

As for the start of Your script:

#!/usr/ksh
COUNTER=0;
echo "Enter the directory path";
read DIRECTORY;
for FILE in $DIRECTORY/file*[0-9];
do
...

/Lakris

try this
#!/bin/sh

COUNTER=0
echo -n "Enter the directory path: ";
read DIRECTORY
echo $DIRECTORY
cd $DIRECTORY

for FILE in `ls | grep -v _tmp$ `
do

echo $COUNTER $FILE

if [ -e ${FILE}_tmp ]
then
COUNTER=`expr $COUNTER + 1`
array[$COUNTER]=${FILE}_tmp
fi
done

try this
#!/bin/sh

COUNTER=0
echo -n "Enter the directory path: ";
read DIRECTORY
echo $DIRECTORY
cd $DIRECTORY

for FILE in `ls | grep -v _tmp$ `
do

echo $COUNTER $FILE

if [ -e ${FILE}_tmp ]
then
COUNTER=`expr $COUNTER + 1`
array[$COUNTER]=${FILE}_tmp
fi
done