In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to customize the function of shell in linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Define shell function (define function)
Syntax:
[function] funname [()]
{
Action
[return int;]
}
Description:
1. You can define it with function fun () or directly fun () without any parameters.
2. If the parameter is returned, you can display the plus: return return. If you do not add it, the result will be run with the last command as the return value. Return followed by the value n (0-255)
Instance (testfun1.sh):
#! / bin/sh fSum 32; function fSum () {echo $1 fSum 2; return $(($1 recording 2));} fSum 5 7; total=$ (fSum 32); echo $total,$?;sh testfun1.shtestfun1.sh: line 3: fSum: command not found5,73215
From the above example, we can draw several conclusions:
1. The function must be declared before the function is called. The shell script is run line by line. It will not be precompiled like other languages. At one time, you must declare a function before using it.
2. Total=$ (fSum 3 2); through this calling method, we know clearly that inside the single parentheses in shell, it can be: command statement. Therefore, we can think of a function in shell as defining a new command, which is a command, so the input parameters are directly separated by spaces. Once, the parameter method in the command can be obtained by: $0. Get $n. $0 represents the function itself.
3. The function returns a value that can only be returned through $? The system variable gets, directly through =, gets a null value. In fact, according to the above understanding, we know that a function is a command, and it is necessary to use $? to get the return value of the command in shell. Get.
Second, the function scope, the variable scope of action
First, let's look at an example (testfun2.sh):
#! / bin/shecho $(uname); declare num=1000;uname () {echo "test!"; ((num++)); return 100;} testvar () {local num=10; (num++)); echo $num;} uname;echo $? echo $num;testvar;echo $num;sh testful 1001001111001
Let's analyze the above example and come to the following conclusion:
1, the definition function can be the same as the system command, indicating that the shell search command, first of all in the current shell file defined in the place to find, find direct execution.
2. Need to get the function value: through $? Get
3. If you need to send out other type function values, you can define the variable (this is the global variable) before the function call. It can be modified directly inside the function, and then the modified value can be read out when the function is executed.
4. If you need to define your own variable, you can define it in the function: local variable = value, then the variable is an internal variable, and its modification will not affect the value of the same variable outside the function.
These are all the contents of the article "how to customize functions in shell in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.