技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

shell的函数

环境:ubuntu10.10 + bash

函数定义以及传参

#! /bin/sh

test()
{
    echo $1 #打印第一个参数
}

a='hello,world'
test $a #参数必须是定义好的变量

传递两个参数

#! /bin/sh

test()
{
    echo $1 #打印第一个参数
    echo $2
    return 127 #只能返回整数
}

test 'hello,world!' 'lishujun' #传递两个参数
echo $? #取得函数返回值,调用完毕立即取值

取得返回值

#! /bin/sh

test()
{
    echo $1 #打印第一个参数
    return 127 #只能返回整数
}

a='hello,world'
test $a #必须这么传值
echo $? #取得函数返回值,调用完毕立即取值

 

posted on 2013-04-01 17:13  codestyle  阅读(200)  评论(0)    收藏  举报