ºìÁªLinuxÃÅ»§
Linux°ïÖú

shell³ÌÐòÉè¼Æ--º¯Êý

·¢²¼Ê±¼ä:2008-06-29 17:04:30À´Ô´:ºìÁª×÷Õß:neverthesame
¿ª·¢´óÐ͵Äshell³ÌÐòÉÙ²»Á˺¯Êý£¬ËüÄܹ»Ê¹ÓóÌÐòµÄ´úÂëµÄ¸´Óó̶ȸߣ¬¿ª·¢¼òµ¥»¯£¬Ä£¿é»¯£¬ÍŶӺÏ×÷ÐÔÇ¿µÈ¡£
º¯Êý¶¨Òå¸ñʽ£º
functionname ()
{
Óï¾ä×é
}
º¯ÊýʹÓãº
1¡¢µ÷Óú¯Êý֮ǰ£¬±ØÐëÏȶ¨Ò庯Êý¡£
2¡¢º¯ÊýµÄ²ÎÊýµÄ´«µÝ£ºfunctionname para1 para2 ¡­.
3¡¢ÔÚº¯ÊýÄÚ²¿²ÎÊýµÄ¶ÁÈ¡£º$* $@ $# $1 $2 $3 $4 $5 ¡­
²»°üÀ¨ $0 £¬ÔÚº¯ÊýÄÚ²¿¿ÉÒÔÒýÓà $0²ÎÊý¡£Òò´Ë£¬ÔÚº¯ÊýÄÚ²¿¾Í²»ÄÜÒýÓõ½³ÌÐòµÄ²ÎÊý¡£µ±º¯ÊýÖ´ÐÐÍêÖ®ºó£¬ÕâЩֵ»á±»»Ö¸´¡£
4¡¢º¯ÊýµÄ·µ»ØÖµ£º¿ÉÄÜʹÓÃreturnÃüÁî·µ»ØÊý×ÖÖµ£»Òª·µ»Ø×Ö·û´®Öµ£¬¿ÉÒÔ×Ö·û´®±£´æÔÚÒ»¸öÈ«¾ÖÐԵıäÁ¿ÖУ¬¸Ã±äÁ¿ÔÚº¯Êý½áÊøºóÄܱ»Íâ½çʹÓã»Èç¹ûûÓÐʹÓÃreturnÃüÁÔòº¯Êý·µ»ØÖµÊǺ¯ÊýÖÐ×îºóÖ´ÐеÄÒ»ÌõÃüÁîµÄÍ˳ö״̬Âë¡£
5¡¢±äÁ¿Ê¹Ó㺺¯ÊýÄÚ²¿ÉùÃ÷µÄ±äÁ¿Ä¬ÈÏΪȫ¾Ö±äÁ¿£¬Ê¹ÓÃlocal¹Ø¼ü×ÖÉùÃ÷µÄ±äÁ¿Îª¾Ö²¿±äÁ¿£¨Èç local var=¡°var¡±£©¡£Èç¹û¾Ö²¿±äÁ¿ÓëÈ«¾Ö±äÁ¿Í¬Ãû£¬ÔòÔÚº¯ÊýÄÚ²¿¾Ö²¿±äÁ¿¸²¸ÇÈ«¾Ö±äÁ¿¡£
6¡¢·µ»ØÖµµÄ»ñÈ¡£¬µ±Ö´ÐÐÍ꺯Êýºó£¬º¯ÊýµÄ·µ»ØÖµ±»´æ·ÅÔÚ$?ÖУ¬¿ÉÒÔͨ¹ýËüÀ´»ñÈ¡º¯ÊýµÄ·µ»ØÖµ¡£
#!/bin/sh
#This is a example for testing the return value of function
fun()
{
return 10
}
fun
echo $?
exit 0
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. lyq617617 ÓÚ 2008-07-09 10:30:39·¢±í:

    ¶àлÁ˸çÃÇ,ѧÁËÕâЩÌìlinux shell,Ò»Ö±ÔÚÀ§»ó,Ôõôµ÷Óôø²ÎµÄº¯Êý,ÖÕÓÚ½â¾öÁË,ѧϰÁË,Ò²ÕýºÃ°ÑÎҵijÌÐò´úÂëÌù³öÀ´,ÓÐÐèÒªµÄÅóÓÑ¿ÉÒÔ½áºÏÉÏÃæµÄºÍʵÀýѧϰһÏÂ:
    1.µÚÒ»¸ö½Å±¾ testadd(°üº¬¼Ó·¨º¯ÊýµÄ½Å±¾)
    #! /bin/bash
    addition(){
    declare -i sum
    sum=$1+$2
    return $sum
    }
    2.µÚ¶þ¸ö½Å±¾ add(Ôڴ˽ű¾Öе÷ÓÃÉÏÃæ½Å±¾Öеļӷ¨º¯Êýaddtion())
    #! /bin/bash
    . testadd
    addition 10 20
    echo $?

    Ö´Ðнá¹ûÈçÏÂ:
    # add
    30