SoFunction
Updated on 2025-03-10

A brief analysis of the number of parameters passed in the main function in the shell script cannot be obtained by the script.

For Linux shell scripts, sometimes when we run shell scripts, we will pass parameters to the script. For logical rigor, we may make some logical judgments or processing in the script, such as judging the number of parameters passed in the script. Generally, we will use $# to get the number of passed parameters. If we judge the number of passed parameters in the main function of the shell script, similar to the following:

.........
function main()
{
    if [ $# != 1 ]; then
      echo "This script must be run with one parameter"
      echo "Usage:mysql_slowlog_monitor.sh 6h"
      exit 1
    fi
    check_enviroment;
    send_slow_rpt;
    return 0;
}
main;

If you debug this shell script, you will find that the value of $# in the main function is always 0. If you adjust the script, put the script that determines the number of parameters passed outside the main function (it cannot be placed in other functions), as shown below, so it will be OK

.............
if [ $# != 1 ]; then
  echo "This script must be run with one parameter"
  echo "Usage:mysql_slowlog_monitor.sh 6h"
  exit 1
fi
.............
function main()
{
    check_enviroment;
    send_slow_rpt;
    return 0;
}
main;

So why does this happen? Before answering this question, let's first understand the purpose of $#. $# represents the number of parameters passed in the script, and also represents the number of parameters passed in the function when a function is called. It also has a scope range. If it is inside a function, it represents the number of parameters passed in the function call.

Let's answer this question again. In the above shell script, the main function is written as main when calling; means that no parameters are passed in the function call, so the value of $# is 0 in main, and the number of parameters passed in the script mysql_slowlog_monitor.sh should be obtained in the script, and its value should be obtained outside the function in the script.

So how to solve this problem?

Solution 1:

Put the script that determines the parameters passed in when the script is called outside the function, just like the example script above.

Solution 2:

With the help of global variables, first obtain the number of parameters passed in the script outside the function, assign them to a global variable, and then perform logical judgment and processing in the mian function.

.............
ARGS=$#
.............
function main()
{
    if [ $ARGS != 1 ]; then
      echo "This script must be run with one parameter"
      echo "Usage:mysql_slowlog_monitor.sh 6h"
      exit 1
    fi
    check_enviroment;
    send_slow_rpt;
    return 0;
}
main;

This is the introduction to this article about the number of parameters passed in the script by $# in the main function in the shell script. For more related shell scripts, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!