Hi Friends,
i am new to unix,i have a big doubt/help.
I have files in folders SER1 and SER2 with naming convention as below
file_2010-03-19.txt and so on
the file naming format is file_<date>.txt. I would like to copy the files to directory "Landing"
Its indicate that the files ,named using the date(first filed) not extracted(copied)successfully from the folders SER1 (2nd) and SER2 (3rd)
Now using this log file I want look into the failed folders and try to extract the files that named with date in1st filed of log file. And the script that I we are going to develop should use this log.txt file and extract all files from respective failed folders(SER1/SER2 or from Both).
If the file extraction once again failed for any date/SER1/SER2 , my script should prepare same log file (log.txt) at the end.
If the extraction success from both folders, no need of entry in log file. If any one one folder or both failed, I need a entry in th log file.
You have and had several infractions cause of code tags by different mods; if you have 30 points, you are out. Writing this here in the thread since it seems you ignore PMs about it.
Your post is totally messed up with formatting tags (where there is no reason for it - there is not only the bold tags) - you'll have to replace them with code tags your own.
LogFile=log.txt
TargetDir=Landing
> ${LogFile}.new
while read date infos
do
set -- ${infos}
eval status=\$$#
# Keeps entries with status not equal to failed
if [ "${status}" != "failed" ]
then
echo "${date} ${infos}" >> ${LogFile}.new
continue
fi
# Extract file in each failed directory
new_failed_dirs=""
while [ $# -gt 1 ]
do
dir=$1
shift
file="${dir}/file_${date}.txt"
echo "\nExtracting file ${file} ..."
if cp ${file} ${TargetDir}/
then
echo "Ok."
else
echo "Failed."
new_failed_dirs="${new_failed_dirs} ${dir}"
fi
done
# Write failed directory into new log
if [ -n "${new_failed_dirs}" ]
then
echo "${date} ${new_failed_dirs} failed" >> ${LogFile}.new
fi
done < log.txt
mv ${LogFile} ${LogFile}.old && mv ${LogFile}.new ${LogFile}
Extracting file SER1/file_2010-02-19.txt ...
cp: SER1/file_2010-02-19.txt: No such file or directory
Failed.
Extracting file SER2/file_2010-02-19.txt ...
Ok.
Extracting file SER2/file_2010-02-10.txt ...
cp: SER2/file_2010-02-10.txt: No such file or directory
Failed.
Extracting file SER1/file_2010-04-14.txt ...
cp: SER1/file_2010-04-14.txt: No such file or directory
Failed.
Extracting file SER1/file_2010-04-26.txt ...
Ok.
Extracting file SER2/file_2010-04-26.txt ...
cp: SER2/file_2010-04-26.txt: No such file or directory
Failed.
This command reads a line from the input file, and stores the first word in the variable date and the remaining in the variable infos.
set -- ${infos}
eval status=\$$#
This command parses the contents of the variable info,words are stored into variables $1 $2 $3... the number of words is given by $#
The value of the last word is stored in the variable status