mercredi 21 juillet 2021

Protect against unary operator expected error when no return value?

How do I protect against this type of error ./checkConfigs.sh: line 22: [: -gt: unary operator expected when my command statement only sometimes returns an integer value?

duplicatesConfig=`sort ${dirNames[0]}"$bf" | uniq -cd | sort -nr | awk '{sum+=$1} END{ print sum}'`
duplicatesBox=`sort ${dirNames[1]}"$bf" | uniq -cd | sort -nr | awk '{sum+=$1} END{ print sum}'`
if [ $duplicatesConfig -gt 0 ]
then
    createCopy ${dirNames[0]}"$bf" 0
fi
if [ $duplicatesBox-gt 0 ]
then
    createCopy ${dirNames[1]}"$bf" 1
fi
done

Line 22 is the first if statement.

I am using awk to capture a sum of all of the duplicates lines in my file. The code works perfectly when I the duplicate count is > 0 but it throws that error when there is no duplicates. The code doesn't break but I would like to prevent that error from popping up.

Aucun commentaire:

Enregistrer un commentaire