In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to use the shell function, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
The function allows us to divide a complex function into several modules, which makes the program structure clearer and the code reuse higher. Like other programming languages, Shell supports functions. The Shell function must be defined before it is used. The definition format of the Shell function is as follows:
[function] fun () {list of commands [return value]}
Description:
It can be defined with function fun () or directly fun () without any parameters, and the function keyword and () must have one of them.
Return value, you can display plus: return return, if not, the result will be run with the last command as the return value. Return is followed by the value n (0-255).
Parameters, () there are no parameters, it is not like the C language, there can be parameters in (). Then you may be depressed, function calls will always need some parameters, so how to pass these parameters in? In fact, the parameter passing method is: fname; (no need to pass parameters) or fname agr1 arg2 (two parameters need to be passed)
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. There are three ways to return a value from the shell function:
Return directly through $? To receive the return value
Define the global variable, receive it through the global variable, and use the function to return the value.
Echo, returned through standard output.
#! / bin/bashfunction sum () {if [$#! = 2] then echo "arg num! = 2" return 0 fi return $(($1 + $2))} sum 56 # call function only need to give the function name, no need to add parentheses. The return value of the function is passed through $? after the function is called, echo "5" 6 "11". To get. Sum 100200 echo "100,200 echo" is followed by the value n (0-255). The array a = (1 2 3 4) sum () {local sum=0; for v in $@ do sum=$ [sum+v] # sum=$ (($sum+$v)); done echo $function is passed to the function. #-> 10} sum ${a [@]} # just like deleting a variable, you can also use the unset command to delete the function, but add the .f option, # $unset-f function_name# if you want to call the function directly from the terminal, you can define the function in the .profile file under the home directory, so that every time you log in, enter the function name at the command prompt and you can call it immediately.
$0: is the name of the script itself
$#: the number of parameters passed to the script
$@: is a list of all parameters passed to the script, that is, extended to "$1", "$2", "$3", and so on.
$*: displays all parameters passed to the script as a single string. Unlike position variables, there can be more than 9 parameters, that is, extended to "$1c$2c$3", where c is the first character of IFS.
$: the ID number of the current process under which the script is running
$?: displays the exit status of the last command. 0 means there is no error, and others indicate that there is an error.
After reading the above, do you have any further understanding of how to use the shell function? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.