Error Trapping

Hi,

I have one shell script as below

while read SegList
do
if test -s ${SourceFile_Path}/${Segment_List_Temp}
then
ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ|cut -d '.' -f2>>${SourceFile_Path}/${List_Temp}
 
echo "IF above statment Fail I want to Create Emtpy File How to Trapp above LS statement Fails "
else
ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ|cut -d '.' -f2>${SourceFile_Path}/${List_Temp}
echo "IF above statment Fail I want to Create Emtpy File How to Trapp above LS statement Fails "
fi
done<${Segmentlist_Path}/${Segment_List}

I want to check wheather LS Command Failed or Succedded

Please me know solution for same.

Thanks,
Sam

$? will be 0 if the previous command succeeded.

If man ls (linux) "fails" -- which I believe to mean it does not match the "${SegList}.DAT.${Datelist}.GZ" pattern -- there is no output, the file is empty.

I am trying to figure out what you are trying to do. First off, I indented your code (which makes it much easier to read):

while read SegList; do
    if test -s ${SourceFile_Path}/${Segment_List_Temp}; then
        ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ | cut -d '.' -f2 >> ${SourceFile_Path}/${List_Temp}
    else
        ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ | cut -d '.' -f2 > ${SourceFile_Path}/${List_Temp}
     fi
done < ${Segmentlist_Path}/${Segment_List}

Inside the loop, the only difference I see is that ${List_Temp} is appended to if ${Segment_List_Temp} exists and has data, otherwise ${List_Temp} is overwritten. This can be simplified by removing ${List_Temp} when ${Segment_List_Temp} does not exist or has no data, and then append to ${List_Temp}, as follows:

while read SegList; do
    if ! test -s ${SourceFile_Path}/${Segment_List_Temp}; then
        rm -f ${SourceFile_Path}/${List_Temp}
    fi

    ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ | cut -d '.' -f2 >> ${SourceFile_Path}/${List_Temp}
done < ${Segmentlist_Path}/${Segment_List}

${Segment_List_Temp} is not otherwise modified by the script, so it can be pulled out of the loop, as can the man cut (linux):

if ! test -s ${SourceFile_Path}/${Segment_List_Temp}; then
    rm -f ${SourceFile_Path}/${List_Temp}
fi

while read SegList; do
    ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ
done < ${Segmentlist_Path}/${Segment_List} \
| cut -d '.' -f2 >> ${SourceFile_Path}/${List_Temp}

BTW, what is the value of ${Datelist}? Is it a pattern? Also, does either ${FTP_Path} or ${SeqList} contain "."? If not, then the output file will contain nothing but lines of "DAT".

I do have to ask -- what is the intention of this script?

I agree with m.d.ludwig. What is the purpose of the script? Where do the values of these variables come from?

${FTP_Path}
${SourceFile_Path}
${List_Temp}
${Segmentlist_Path}
${Segment_List}

We can avoid using "ls" by using "test -f " instead. Can use unix "touch" command to ensure that the logfile is aways there.

while read SegList
do
        filename="${FTP_Path}/${SegList}.DAT.${Datelist}.GZ"
        logfile="${SourceFile_Path}/${List_Temp}"
        # Ensure logfile always exists
        touch "${logfile}"
        if [ -f "${filename}" ]
        then
                echo "${filename}"|cut -d '.' -f2>>"${logfile}"
        fi
done<${Segmentlist_Path}/${Segment_List}