Analysis of the number of parameters passed to the script in the main function of shell script $# _linux shell_script home

Analysis of the number of parameters passed into the script by $# in the main function of shell script

Updated: March 18, 2024 10:58:24 Author: Xiaoxiang Yin
In Linux shell scripts, sometimes when we run shell scripts, we will pass parameters to the scripts. For logical rigor, we may make some logical judgments or processing in the scripts, such as judging the number of parameters passed into the scripts. This article mainly introduces the shell script in the main function of $# to obtain the number of script incoming parameters analysis, need friends can refer to

Linux shell script, sometimes when we run the shell script will pass parameters to the script, out of logical rigor, in the script may do some logical judgment or processing, such as judging the number of parameters passed into the script. In general, we would use $# to get the number of arguments passed into the script. If we use the main function of the shell script to determine the number of arguments passed into the script, something like this:

... function main() { if [ $# != 1 ]; then echo "This script must be run with one parameter" echo "Usage: 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 the shell script, you will find that the value of $# in the main function is always 0, if you adjust the script to determine the number of arguments passed outside the main function (not in other functions), as shown below, 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 is this happening? Before answering this question, let's first understand the use of $#, $# indicates the number of arguments passed to the script, and also indicates the number of arguments passed to the function when a function is called, and it also has a scope, if it is inside the function. It represents the number of parameters passed in when the function is called.

So to answer this question again, in the above shell script, the main function is called as main; This means that when the function is called, no arguments are passed, so $# has the value 0 in main, and the number of arguments passed in the script mysql_slowlog_monitor.sh should be taken inside the script and outside the function in the script.

So how do you 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 global variables, first get the number of parameters passed by the script outside the function, assign them as global variables, and then in the mian function, perform logical judgment and processing.

... ARGS=$# ............. function main() { if [ $ARGS != 1 ]; then echo "This script must be run with one parameter" echo "Usage: 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;

To this article about the shell script in the main function of $# to get the number of script incoming parameters of the article introduced to this, more related shell script incoming parameter number content please search the script home of the previous article or continue to browse the following related articles hope that you will support the script home in the future!

Related article

  • Linux学习之expect操作详解

    expect operations for Linux learning

    expect is a scripting language, which can replace the manual interaction with the terminal, mainly applied to the execution of commands and programs, the system requires the input of a specified string in interactive form, to achieve interactive communication. This article will talk about its use in detail through examples, interested can understand
    2022-10-10
  • Linux Shell+Curl网站健康状态检查脚本,抓出中国博客联盟失联站点

    Linux Shell+Curl website health status check script, caught the Chinese blog alliance lost contact site

    This article mainly introduces the Shell+Curl website health status check script, catch the Chinese blog alliance lost contact site, need friends can refer to
    2016-02-02
  • Linux修改主机名的命令详解

    Description of commands for changing host names in Linux

    Usually in the purchase of the server or some newly installed Linux system host name is a random string, but if we want to identify the name of each host, we can modify the host name, so this article gives you the Linux to modify the host name command, need a friend can refer to
    2024-01-01
  • Shell脚本中实现切换用户并执行命令操作

    Switch users and execute command operations in Shell script

    This article mainly introduces the Shell script to switch users and execute command operations, read the example code to understand the second, the original is so simple, the need of friends can refer to
    2014-12-12
  • shell编程中的字符串截取方法小结

    Summary of string interception methods in shell programming

    This article mainly introduces the summary of the string interception method in shell programming, this article explains the method of intercepting the first 8 bits of the character variable, according to the specified string interception method, according to the specified requirements of the segmentation method, etc., the need of friends can refer to the next
    2015-03-03
  • Linux rm命令详解 Linux删除文件目录的操作方法

    The Linux rm command describes how to delete a file directory in Linux

    This article mainly introduces the detailed understanding of Linux rm command, Linux delete file directory, this article gives you a very detailed introduction, for everyone's study or work has a certain reference value, need friends can refer to the next
    2023-01-01
  • Shell编程之循环语句示例详解

    Shell programming loop statement example detailed explanation

    In Shell script development, often encounter some specification problems, such as forgetting to use quotation marks or forget to add fi at the end of the if statement, this article mainly introduces the Shell programming of the loop statement, the need of friends can refer to the next
    2023-12-12
  • Shell中eval的用法示例

    Example usage of eval in Shell

    This article mainly introduces the use of eval in the Shell example, this article gives a number of examples to explain its use, need friends can refer to
    2015-07-07
  • Linux命令中的rpm安装命令

    rpm installation command in Linux command

    RPM stands for Redhat Package Manager, which was developed by Redhat to manage software packages under Linux. The following through this article to share Linux command rpm installation command, the need for friends to refer to it
    2017-06-06
  • 详解shell 遍历文件夹内所有文件并打印绝对路径

    The shell traverses all files in the folder and prints the absolute path

    This article mainly introduces the shell traversing all files in the folder and printing the absolute path, has a certain reference value, interested can understand.
    2017-01-01

Latest comments