Inside shell script it is picking some files from UNIX directory. Sometimes when any of these file missed then throw error & shell script got failed. I need to add Exceptional Handling in shell script. Here is the below shell script where it is picking files from directory.
#!/bin/ksh
..........
while read file
do
upd_date=$((`date +%s`-`stat -c %Y ${file}`))
file_nm=`basename "${file}"`
if [[ `lsof | grep "${file_nm}"` != '' || $upd_date -lt 60 ]];
then
log_info "Waiting for the file ${file} to complete transfer/skip"
else
log_info "Data file found ${file}"
fi
done<${TEMP}/filepath
...............
exit 0
In the line upd_date=.. throwing error whenever file missed & shell script got failed. So I need to add exception handling if file missed then it will show in log & script will execute successfully.
kshis notbashthough. Paste your script at shellcheck.net for validation/recommendation.kshdoesn't really have exception handling. Verify that$fileactually expands to a valid file name before trying to use it.while ...; do [ -f "$file" ] || continue; ...; done?while ...; do upd_date=$( ... ) || continue(note$()vice$(()))continueing seems reasonable, but I think the whole approach should be changed. Ideally, the producer (the process writing the file) would not allow any ambiguity. Write the file somewhere else and then create the final final by creating the link (hard link) only after the file is complete and will not be modified. This allows you to avoid the whole problem of checking update time.