Help with script.. it Just doesn't work

Hello,,

Im verry new to scripting and have some problems with this script i made..

What it does:
It checks a directory for a new directory and then issues a couple of commands.

checks sfv - not doing right now

checks rar - it checks if theres a rar file and when there is it skips to the next command. when there isnt it will rar the files in the directory and create an sfv file. [commented becuase doenst work]

checks par - it checks for par2 files and when there are no pars it will create them.

post to usenet

My problem:

When i uncomment the rarring part it will rar a directory with files in it.. But when the directory has subdirectorys with rars it also rars the directory and files instead of skipping.

The script works without the rar part but i need it to rar unpacked files..
It looks like the grep part doenst work if there are subdirectorys.. I hope someone here can make this script work..

Thx in advance

#!/bin/bash -x

echo start Autopost script...

#Monitor Path
checkpath="/home/upload" 

#Monitor Folder for activity
ignore_folder="sample,Sample"
inotifywait --monitor --format %f -e moved_to -e create $checkpath | while read releasedir; do echo "New directory detected: Found $releasedir";

which cksfv >/dev/null
#Check SFV 

cd "$checkpath/$releasedir"
echo "Checking $checkpath/$releasedir for SFV Please wait...";
 if [ "$1" == "" ]; then
	echo -ne ""
else
	if [ -d "$1" ]; then
		cd "$1"
	else
		echo $1 is not a directory
		exit 2
	fi
fi
ignore_folder=$(echo $ignore_folder | sed -e 's/\,/\ -e\ /g')
start_dir=$(pwd)

find -type d |grep -v -e $ignore_folder | while read path
do
	if find "$path" -maxdepth 0 -empty | read; then 
		echo -e "INFO There are no files present in $releasedir"
	else
		result=$(ls -al "$path" |egrep '.*\.sfv')
		if [ "$result" == "" ]; then
			echo -e "WARNING! No sfv file present in $releasedir.
Skipping directory..."
		
else
			cd "$path"
# 			echo result=$(cksfv -f *.sfv 2>/dev/null)


# #Create rar Files if rar exist skip
# files=$(ls -al "$checkpath/$releasedir/$path" |egrep '.*\.rar')
# if [ "$files" == "" ]; then
# echo -e "No rar files present in $releasedir. Going to rar $releasedir"
# cd "$checkpath/$releasedir/$path"
# 
# rar a "$releasedir".rar -v50000k -vn -r -m0 -ep -df -cl "$checkpath/$releasedir/$path"/*
# 
# filename=$(ls -1 "$checkpath/$releasedir/$path" |egrep '.*\.rar')
# cksfv -b "$checkpath/$releasedir/$path"/* > "${filename%.*}".sfv
# 
# else
# echo -e "RAR found:$files"
# echo -e "Skip $releasedir"
# fi


#Create Par Files if par exist skip
files=$(ls -al "$checkpath/$releasedir/$path" |egrep '.*\.par2')
if [ "$files" == "" ]; then
echo -e "No par2 files present in $releasedir. Going to par2 the shit out of
$releasedir"
cd "$checkpath/$releasedir/$path"


filename=$(ls -1 "$checkpath/$releasedir/$path" |egrep '.*\.sfv')
	echo $filename

par2create -r5 -n7 -m500 "${filename%.*}".par2 "$checkpath/$releasedir/$path"/*

else

echo -e "PAR2 files found..Skip $releasedir"

fi

if [ $? -eq 0 ]; then	
				echo -e "OK...No errors detected in $releasedir"
echo "Checking for more directorys.. Please wait.."			
else
				echo -e "\nERROR! SFV inconsistency detected in
$releasedir"
				echo -e "\t $result"
				echo -ne "\n"
			exit
fi
			cd $start_dir
		fi;
	
fi

done

echo "Posting $releasedir Please wait.."

python /home/newsmang/poster.py -c /home/newsmang/sample.conf -f "$releasedir" "$checkpath/$releasedir"/* "$checkpath/$releasedir"/cd1/* "$checkpath/$releasedir"/cd2/* "$checkpath/$releasedir"/cd3/* "$checkpath/$releasedir"/sample/* "$checkpath/$releasedir"/subs/* "$checkpath/$releasedir"/CD1/* "$checkpath/$releasedir"/CD2/* "$checkpath/$releasedir"/CD3/* "$checkpath/$releasedir"/Sample/* "$checkpath/$releasedir"/Subs "$checkpath/$releasedir"/Cd1/* "$checkpath/$releasedir"/Cd2/* "$checkpath/$releasedir"/Cd3/* "$checkpath/$releasedir"/SAMPLE/* "$checkpath/$releasedir"/SUBS "$checkpath/$releasedir"/DiSC1/* "$checkpath/$releasedir"/DiSC2/* "$checkpath/$releasedir"/DiSC3/*

echo "$releasedir Posted.."
echo "Waiting for new release. Scanning Folder..."

done



We need to get certain quirks out of the script first to make it understandable.

1) Please remove the semi-colon at the end of any line where it is the last character. This is unix script not Oracle.

2) I am nervous about the line spacing as posted. Was this script created as a unix text file using a unix text editor?
If you do this command does every line end in a $ character (meaning standard unix line terminator)?

sed -n l the_name_of the_script

3) Tip: If you have called a script with variables, save those variables in meaningfully-named environment variables as soon as possible in case a subsequent command changes those $n variables or the script just becomes difficult to follow. In this script we have no ideas what is in $1 except that it is a directory name and that if it is not provided we can proceed with the script after displaying a meaningless message.

Perhaps better?

if [ "$1""X" = "X" ]; then
	echo "Usage: scriptname <directory_name>"
             exit 1

4) Please re-test after removing extraneous semi-colons and removing any extraneous carriage-return characters from the script file (if present). Then please re-post the current version of the script showing exactly how it was called and all output produced (including any error messages).

5) Each time you issue an "ls" where the files may not be present, you need to stop the command failing by redirecting the error channel.

result=$(ls -al "$path" 2>/dev/null |egrep '.*\.sfv')

6) Within the commented-out "rar" section, this line is strange:

What do you want the test to achieve?