A summary of 6 characteristics of shell custom functions _linux shell

Source: Internet
Author: User
Tags eval

The recent systematic study of the shell function, the overall sense of the root of the other language function is similar, but it also has its own characteristics

One, the call function must be defined after the function, or it will be the error

Copy Code code as follows:

Fun

Fun () {
echo "AAAAAA"
}

Fun

The returned results are as follows:

Copy Code code as follows:

[Root@krlcgcms01 mytest]# SH fun.sh
Fun.sh:line 1:fun:command not found//First call fun is an error, fun does not define
AAAAAA//Second call FUM display result

If it's PHP or JavaScript, there's no such problem.

Second, passing parameters to the shell function is very characteristic

Copy Code code as follows:

Fun2 () {
echo $//$0 itself file name
If [$]//parameter Why are all starting from $ $ is the filename itself
Then
echo "PARAM1 is" $
Else
echo "param1 is null"
Fi
if [$]
Then
echo "Param2 is" $
Fi
}

FUN2 Triple A BBB//FUN2 pass 2 parameters
FUN2 CCC//fun2 pass 1 parameters
FUN2//No pass parameters

The returned results are as follows:

Copy Code code as follows:

[Root@krlcgcms01 mytest]# SH fun2.sh
pa.sh//First call FUN2, show $ value
Param1 is AAA
Param2 is BBB
pa.sh//Second call FUN2, display $ value
PARAM1 is CCC
pa.sh//Third call FUN2, display $ value
PARAM1 is null

Such a way of communication, very special bar.

Third, citing examples

Copy Code code as follows:

Reference ()
{
pa=\$ "$"//$1 shows Param.
echo $PA//display is $param
x= ' eval ' expr $pa '//x as test
echo $1= $x//param=test
Local aaa= $x//define Locals
bbb= $x//Default global variable
Eval "$1= ' Test been modified '"
Return 1//maximum returned integer 257
}

param= "Test"
Echo $param "is test"

Reference param
Return_value=$? The return value is stored in the $?
echo "return value is" $return _value
Echo $param "is not test"

Return_value=$?
echo "return value is" $return _value

echo "AAA =" $AAA//local variable cannot be invoked outside
echo "BBB =" $bbb

The returned results are as follows:

Copy Code code as follows:

[Zhangy@localhost zhangy]$ SH re.sh
Test is Test
$param
Param=test
return value is 1
Test been modified is not test
return value is 0
AAA =
BBB = Test

From the above example, I summarize the following points:

1, the reference is also very special, function max (& $num _a, $num _b) {} If $num_a changes in the method body, the variable that is invoked will also change. The shell quotes the same idea, but the situation is different.
The return value of the 2,shell function can only be cosmetic, and between 0-257.
3, the variables defined in the method, if not made a special statement, is generally a local variable, and the shell is exactly the opposite, local variables to be specifically declared.
4, there can be no action between calling the method and getting the return value, or you cannot get the value of the returns.

Combined with the first two examples of the characteristics shown, there are almost 6 features, there must be some other features, to be added.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.