In the below i am checking the exit status , if it is not zero(which is a failure) i am displaying it by echo statment

while read -r record
 reccount=$((reccount + 1 ))
/usr/bin/java -jar NSGalinaMail.jar "$email_text"  "$email_subject" "$contact_email" "billingdept@ups.com" $lang  $cny_cd $MY_WORK/"Notify_$2.pdf"
        if [ $? -ne 0 ]; then
            emailCountFailure[$reccount-1]="Failure: Email to $contact_email for $ref_nr"
            echo "$emailCountFailure"
        echo "record_count=$reccount" >> $MY_WORK/"raw_data_$2"
        echo "emailCountFailure=$emailCountFailure" >> $MY_WORK/"raw_data_$2"
done < fileName ## a file with 10 records##

what ia m expecting is at least a 0 value if there is no failure, but it displays blank


any solution for this? Thank you


at least you forgot while ... ; do. Here is my version doing what you may expect:


# proposed
set -e
set -u
# disable in production code
#set -v
# disable in production code
set -x

declare -a emailCountFailure
while read -r record; do
    reccount=$((reccount + 1 ))

    # do not exit on non-zero status
    set +e
    # execute something
    perl -Mstrict -Mwarnings -E "say 1/$record" > /dev/null 2>&1
    # store exit code as `set` will exit, too
    # re-enable exit on non-zero status
    set -e

    if [ $_rc -eq 0 ]; then
        # I don't know if you need this
        emailCountFailure[$idx]="Failure: division by $record"
        # do what you need
done < lines

echo "record_count=$reccount"
echo "emailCountFailure=$emailCountFailure"

The file lines contains just lines of numbers and chokes on 0.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.