In the process of writing shell scripts, we often customize some functions and execute the corresponding process according to the function's return value, so how do we get the return value of the function?
There are two ways to call a function in the shell first:
The first kind: value=' function_name [arg1 arg2 ...] ' or second type: Function_name [arg1 arg2 ...] Echo $?
What is the difference between the two?
For example:
[[Email protected]~]#CatTest.SH #!/bin/SHfunctionaaa () {if[" $"-eq"1"]; Thenreturn0 elif[" $"-eq"2"]; ThenExit1 elif[" $"-eq"3"]; Then Echo "3" Echo " to">&2#no space after >Echo " +" elif[" $"-eq"4"]; Thena wrong commendfi}Echo "begin"value1= ' AAA1`Echo$?Echo "value1= $value 1"Echo '---------------'value2= ' AAA2`Echo$?Echo "value2= $value 2"Echo '---------------'Value3= ' AAA3`Echo$?Echo "value3= $value 3"Echo '---------------'Value4= ' AAA4`Echo$?Echo "value4= $value 4"Echo "End"The output is as follows: [[email protected]~]#SHTest.SHbegin0value1=---------------1value2=--------------- to0Value3=3 +---------------test.SH: Line -: A:command not found127Value4=---------------End
" + "can return normally, so $?" Has a value of 0, and a wrong command returns an integer other than 0 because it is not performing properly.
Okay, so let's take a look at the following command:
[Email protected] ~]#/opt/lamp/mysql/bin/mysql-h192. 168.1. - ' Show variables like "version" ' warning:using a password on the command line interface can be insecure.version 5.6. -log
There's a bit of a nasty warning in the output, but when I assign the value of the command to the variable AA,
[Email protected] ~]# aa= '/opt/lamp/mysql/bin/mysql-h192. 168.1. - ' Show variables like "version" ' echo"$aa"version 5.6. -log
You will find that the value of the variable AA does not contain the warning message. Why, see the above analysis can know the reason why this is the case, although the interface shows a warning message, but this warning message is redirected to the standard error output, and will not be assigned to the variable AA, how do I know it? We can do this experiment:
[Email protected] ~]# aa= '/opt/lamp/mysql/bin/mysql-h192. 168.1. - ' Show variables like "version" ' 2>&1echo"$aa"warning:using A Password on the command line interface can is insecure.version 5.6. -log
I relocated the standard error output to the standard input, you see, the value of this variable AA contains a warning message. Of course we can also redirect the standard output to an empty device:
[Email protected] ~]# aa= '/opt/lamp/mysql/bin/mysql-h192. 168.1. - ' Show variables like "version" ' >/dev/null2>&1echo'$aa "
You see, the value of AA is empty. (even when directed to a file, the output is empty)
Summary: When invoking a function return value, the value of ECHO $ is the same regardless of method one or method two. and method one, namely value= ' function_name [arg1 arg2 ...] ',value depends on the standard output of the calling command, there is no standard output is empty, we generally return the value of the function returned to the last execution state , (generally not exit to return, because it is directly called when the entire shell is directly terminated, Of course, if you have a special purpose to do this, use echo to output the value of the variable.
Shell Call function return value in-depth analysis