Analysis of the number of parameters passed into the script by $# in the main function of shell script
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!
- Analysis of the number of parameters passed into the script by $# in the main function of shell script
- PowerShell function example of function argument passing to another function
- bash shell command line options and modifies incoming parameter processing
- Shell special variables (Shell $#, $*, $@, $? , $$)
- Shell $0, $#, $*, $@, $? , $$and the use of command line arguments
- The definition of shell variables $#,$@,$0,$1,$2 in linux
Related article
-
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 understand2022-10-10 -
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 to2024-01-01 -
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 to2014-12-12 -
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 next2015-03-03 -
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 next2023-01-01 -
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 next2023-12-12 -
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 to2015-07-07 -
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 it2017-06-06 -
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