Artificial intelligent assistant

Issue with if statement in Unix I am getting the output of the first `if` script but unable to get the output of the second part of the `if` statement in the below script: #!/bin/ksh err_abc=`grep -r "XYZ" /home |wc -l` err_AB=`grep -r "XYZ" /home` > /dev/null 2>&1 err_ERR=`grep -r "ERROR" /home |wc -l` err_eRR=`grep -r "ERROR" /home` > /dev/null 2>&1 if [ $err_abc -gt 0 ] then echo "$err_AB" else echo "No errors found" if [ $err_ERR -gt 0 ] then echo "$err_eRR" else echo " \n No err files found" exit 0 fi fi

Are you sure the code is doing what you think it's doing? Let's indent it so that we can more easily see the logic:


if [ $err_abc -gt 0 ]; then
echo "$err_AB"
else
echo "No errors found"
if [ $err_ERR -gt 0 ]; then
echo "$err_eRR"
else
echo " \
No err files found"
exit 0
fi
fi


Your second `if` block is only executing if and only if the first block's test is falsy. If you want both tests to be run in all cases, this needs to be rewritten:


if [ $err_abc -gt 0 ]; then
echo "$err_AB"
else
echo "No errors found"
fi
if [ $err_ERR -gt 0 ]; then
echo "$err_eRR"
else
echo " \
No err files found"
exit 0
fi


Also, the `exit 0` you have in your final `if` statement's `else` clause is only executed if any only if the second test is falsy. If this is not your intent, that statement should be moved appropriately.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 9242aee810cf1df8ae56ba38d6fea7d7