0

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"
        fi
        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

emailCountFailure=

any solution for this? Thank you

0

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

#!/bin/bash

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

reccount=0
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
    _rc=$?
    # re-enable exit on non-zero status
    set -e

    idx=$[$reccount-1]
    if [ $_rc -eq 0 ]; then
        # I don't know if you need this
        emailCountFailure[$idx]=''
    else
        emailCountFailure[$idx]="Failure: division by $record"
        # do what you need
    fi
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.