An curious idea, how to make it by the powerful script?

I use a simple script to do some quantum calculations with gaussian package. the script as follows

#!/bin/sh

#put a gaussian input file into a new folder in the same name
#and submit this new job

for i in *.gjf
  do
        FN=$( echo $i | sed 's/.gjf//')
        mkdir $FN
        mv $FN.gjf ./$FN
        cp ./g03.sh ./$FN
        mv ./$FN/g03.sh ./$FN/OPT-$FN
        cd $FN
        qsub -V OPT-$FN
        cd ..
  done

#END

It works fine. The general function of this simple script is going: 1. to list the input file in the present folder; 2. to create a folder with the same name; 3. to move the input file into this newly created folder; 4. to submit it.

I would like to know how to add two extra functions into this script:

  1. If the job fails, it always leave a very large file name core.*. How to remove it automatically.
  2. A checkpoint file named <jobname>.chk will be generated by gaussian package. How to do execute the following command "formchk <jobname>.chk" immediately after the job finished.

It should not be a strange idea. what I don't know is how to know when the job finished.

Please helps.

Thank you in advance!

ZHEN, from Shanghai, China.

from the two point mentioned:

  1. removing core files:
rm -f core # for removing single core file

find . -type f -name "core" -exec rm -f {} \; # for removing core files present in the current dir or subdir.
  1. if the <jobname>.chk is present in the current dir.
if [ -f "<jobname>.chk" ]; then
 formchk <jobname>.chk
fi

you can put the commands in order to how you want to execute them.
if you are having directory other them current dir, then use the absolute path for the file.