Shell function processing

Source: Internet
Author: User

Shell function calls are divided into two types:

The first way, a bit like the C language Call function style, directly to the function of the execution of the result copy to the variable! However, the assignment process is not the same as the C language function assignment!

In C, the function call is to return the function's return value to the called function, such as:

Fun () {     return (1+2);} Main () {    = fun ();    ...............}

This actually copies the return value of the function, which is the value of return, to the variable a! However, it is important to note that the first way to invoke a function in the shell is to pass the standard output to the main program's variable, not the return value ! Please see the following program test.sh:

#!/bin/sh check_user () {n= ' CAT/ETC/PASSWD | Cut-d":"-F1| Grep"^$1$"-rn | Cut-d":"-F1' echo $n #这里是使用echo语句 and output the result to standard output, so you can use the variable to receive }userinfo () {userinfo) in the main program= ' head-$1/etc/passwd | Tail-1| Cut-d":"-F3,4' echo $userinfo} while true Doread username m=' Check_user $username ' #使用变量接收函数check_user传递的值if[-N"$m"] then userinfo $m exitElseEcho"$username is not exit!"fi  done

Test code:

[Email protected] ~]$./ is not exit! Qiu.li 40006:1004

The second way to call a function is to use $? To receive the return value status of the previous program, which is the value returned by return. In the following procedure, if judgment, return 0 or 1, in this case, we can use $? receive the value of return, then store it, then proceed to the next judgment!

Check_user () {n= ' CAT/ETC/PASSWD | Cut-d":"-F1| Grep-n"^$1$"| Cut-d":"-F1`    if[-Z"$n"] Thenreturn 0    Else        return 1Fi}show_userinfo () {userinfo= ' head-$n/etc/passwd | Tail-1| Cut-d":"-F1,3,4' echo $userinfo}echo"input Username:"Read Usernamecheck_user $usernamenum=$?if[$num-eq0]then Echo"The user ' $username ' is not exist."ExitElseShow_userinfo $nfi

The results of the above two programs are the same, but be aware of the different ways in which the two functions are called and what they return to the keynote program. Understand this to know exactly how to receive the return value!

Shell function processing

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.