Home > Blockchain >  Exit shell script if file doesn't exist
Exit shell script if file doesn't exist

Time:09-23

Ive got a .sql job which creates files depending on certain criteria, it writes these with a prefix of TEMP_ as we then have an adaptor that picks up the files and we dont want them picked up before writing is complete. I need to put a post job in place which renames these files, i have it set up with a number of other job but they all create the files each time they run. This job only creates the files sometimes it runs, depending on the data in the system. I need to do a check if the file exists and then exit if no file exists.

Ive found a few examples but they all seem to fail, this is where i have got to which i thought was checking if no file, if no file then exit but it fails and displays: "syntax error at line 16: `TEMP_SUBCON*.csv' unexpected"

This is what i have currently with line 16 being the first line - Above that is just comments:

    if [[ ! -f $OUT_DIR -name TEMP_SUBCON*.csv ]] ; then
    exit $?
    fi
    
    TEMP_DATA_FILE=$(find $OUT_DIR -name TEMP_SUBCON_QTY_output*.csv)
    DATA_FILE=$(basename $TEMP_DATA_FILE | cut -c6-)
    
    echo $TEMP_DATA_FILE
    echo $DATA_FILE

## Rename the file name, remove the phrase TEMP_, so that EAI can pick the file ##
mv $TEMP_DATA_FILE $OUT_DIR/$DATA_FILE

Can you help guide what ive done incorrectly?

Thanks

CodePudding user response:

That syntax is not valid for the [[ ... ]] test.

Why not use the result of the subsequent find command to check if there were any matching files in the specified directory instead, and quit if no files are returned (in other words, quit if the result variable is empty)?

Example:

TEMP_DATA_FILE=$(find $OUT_DIR -name "TEMP_SUBCON_QTY_output*.csv" )
if [[ -z ${TEMP_DATA_FILE:=""} ]]  ; then
   exit 1
fi

Note 1: you should quote the pattern argument for the find command as shown.

Note 2: it is useful to use set -u in your ksh scripts to cause ksh to abort if variables are unitialized when used (often the cause of errors) , instead of using a default value. However, if you use use set -u then in any test you should explicitly give your own default value. That is the reason for using ${TEMP_DATA_FILE:=""} instead of ${TEMP_DATA_FILE} - to support the often very useful set -u option. Even when you do not use set -u the ${TEMP_DATA_FILE:=""} inside tests makes it explicit what should happen instead of relying on implicit behaviour.

Note 3: you should use set -x when debugging and study every line of the output, it will show you exactly what commands ran with which arguments and what was the result. This helps you to learn how to code in ksh and similar shells.

CodePudding user response:

If I understand it right, you want to find the files with TEMP_ prefix in your $OUT_DIR, and then if any rename them without the prefix. Then that should do the trick

for file in $OUT_DIR/TEMP_SUBCON_*.txt; do
    if [[ -e $file ]]; then
        mv $file $OUT_DIR/${file#*SUBCON_}
    fi
done
exit

It will go through the directory finding each TEMP_ file and rename them without it. If there is none, it won't do anything.

  • Related