In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to call the Shell function in Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to call the Shell function in Linux.
The essence of the Shell function is a piece of script code that can be reused, which is written in advance, placed in a specified location, and can be called directly when it is used.
The definition format of 1.Shell function
You can use function fun_name () with the function keyword to define it, or you can give the function name fun_name () definition directly. Can not take any parameters, the function name must be unique, so that the parameters can be passed, using 2, … @ to get the parameters. It is recommended to use the function keyword to define the function, which is easy for code to read. The definition format is as follows:
Function_name () {list of commands [return value]}
# or
Function function_name () {list of commands [return value]}
Function returns a value that can be explicitly incremented by the return statement; if not, the result of the last command run is taken as the return value.
The return value of the Shell function can only be an integer, which is generally used to indicate whether the function is successful or not. 0 indicates success and other values indicate failure. If you return other data, such as a string, you will often get an error message: "numeric argument required".
If you must let the function return a string, you can first define a variable to receive the result of the function's calculation, and the script accesses this variable when needed to get the function's return value.
#! / bin/bashfuncWorld () {echo "world"} res= `funcWorld`echo "hello $res"
Running result:
How to call hello world2.Shell function
To call a function in a script, simply give the function name. For example, to call the function funcWorld () in the section "definition format of 1.Shell functions", you only need to give the function name funcWorld, which does not need to be followed by parentheses (). The Shell function is called much like the Shell command.
The acquisition of the return value of 3.Shell function
Use get. ? You can get either the exit status of the previous command or the return value of the function. What's more, the call to the Shell function is very similar to the execution of the Shell command.
#! / bin/bashadd () {varAddRes= `expr $1 + $2`return $varAddRes} add 1 2 # call the function, passing the parameter echo $?
Running result: 3
Define local variables in 4.Shell function
The variable defined in the Shell function is global by default, and its scope starts from "where the variable definition is executed when the function is called" to the end of the Shell or where it is shown to be deleted. The variable defined by the function can be displayed as local using the local keyword, its scope is limited to the function, and the local variable with the same name masks the global variable. Note, however, that the argument to the function is local.
#! / bin/bashfunction test () {local localVar=dablelv # Local variable} testecho $localVar # output to an empty 5.Shell function, do you need a predeclaration?
The Shell function must be defined before use, there is no declaration! A common way is to put the function definition at the beginning of the script.
Deletion of 6.Shell function
Just like deleting a variable, the delete function can also use the unset command, but with the-f option, as follows:
Unset-f function_name7. Use functions on the command line
If you want to use a function on the command line, you need to execute the definition of the function on the command line. The drawback of defining the Shell function directly on the command line is that once you exit Shell, the function definition will become invalid. You can define the function in the. bash_profile file under the Home directory, or the / etc/profile file that is valid for all users, so that every time you log in, the profile file will be called, and you can call it immediately by typing the function name at the command prompt.
Note: to take effect immediately after modifying the file, you need to execute source / etc/profile or source ~ /. Bash_profile, otherwise it will only take effect the next time you log in again.
At this point, I believe you have a deeper understanding of "how to call the Shell function in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.