Why you should care
Checking arguments is a critical component of Shell script. The $# variable indicates the number of arguments. If this variable is improperly implemented, then the arguments are left unchecked which can lead to improper execution and bugs in the code.Business Impacts
Leaving arguments unchecked on multiple shell scripts can be a blow to the overall portfolio. This can cause productivity issues in an agile-based environment where some developers are trying to debug their code but end up over-complicating the process and leaving other developers without any progress to work towards. [nz_btn text="Production Risk" target="_self" animate="false" animation_type="ghost" color="pink" size="small" shape="rounded" type="normal" hover_normal="opacity" hover_ghost="fill" link="http://casthighlight.wpengine.com/category/product/indicators-methodology/innovation/" icon="icon-office"][nz_btn text="Complexity" target="_self" animate="false" animation_type="ghost" color="mulled_wine" size="small" shape="rounded" type="ghost" hover_normal="fill" hover_ghost="fill" link="http://casthighlight.wpengine.com/category/product/indicators-methodology/innovation/"]CAST Recommendations
CAST recommends checking arguments by adding spaces between its arguments like the following example below - Incorrect -if [$# -ne 1];
then echo "illegal number of parameters"
fi
Correct -
if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
fi
Instead of leaving arguments unchecked, they can be removed by adding shift like the following example below -
if (( $# < 3 )); then
echo "$0 old_string new_string file [file...]"
exit 0
else
ostr="$1"; shift
nstr="$1"; shift
fi
These errors usually occur because the style-guide in the company is not followed by development teams or has not been updated for future projects. For more help on checking arguments, refer to "Scripting Guide for Bash"
References
https://stackoverflow.com/questions/18568706/check-number-of-arguments-passed-to-a-bash-script?rq=1 https://unix.stackexchange.com/questions/174566/what-is-the-purpose-of-using-shift-in-shell-scripts[nz_btn text="Shell Script Guide" target="_self" animate="false" animation_type="ghost" color="turquoise" size="small" shape="rounded" type="ghost" hover_normal="fill" hover_ghost="fill" link="http://tldp.org/LDP/abs/html/" icon="icon-book"]How we detect
This code insight counts one violation each time the following patterns are encountered :- <indentifier> = $#
- $# <op> where <op> is -eq, -ne, -lt, -gt, -le, -ge, =, !=
- case $#
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.
For reference only. For the complete details please refer the original article
https://doc.casthighlight.com/alt_checkargs-always-check-arguments/
https://doc.casthighlight.com/alt_checkargs-always-check-arguments/
Comments