下面我检查退出状态,如果它不是零(即失败)我将通过 echo 语句显示它
while read -r record
reccount=$((reccount + 1 ))
/usr/bin/java -jar NSGalinaMail.jar "$email_text" "$email_subject" "$contact_email" "[email protected]" $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##
如果没有失败,我期望至少是 0 值,但它显示空白
emailCountFailure=
有没有什么解决办法?谢谢
答案1
至少你忘了while ... ; do
。这是我的版本,可以实现你所期望的功能:
#!/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"
该文件lines
仅包含几行数字,并且被 0 阻塞。
希望有所帮助。