【shell第五天】shell中使用函数

函数语法

function 函数名() {
	指令
	return 
}
简化写法1: 
function 函数名 { 
	指令
	return 
}
简化写法2: 
函数名() { 
	指令
	return 
}

函数调用

在Shell中,函数调用的基本语法如下:
function_name param1 param2 …
在上面的语法中,function_name表示函数名称,其后面跟的param1、param2…表示函数的参数。
Shell的函数参数的语法比较特殊。实际上,Shell将脚本参数和函数参数做了统一地处理。也就是说,
Shell采用了相同的方法来处理脚本的参数和函数参数。
(1)调用函数:直接执行函数名即可。
函数名
(2)带参数的函数执行方法:
函数名 参数
与Shell脚本一样,用户可以在Shell函数中使用位置变量来获取参数值。例如,$0表示脚本名称,$#来获取函数的参数个数,$1表示第1个参数,$2表示第2个参数等,以此类推。另外,用户还可以通过系统变量$@和$*获取所有参数的值。

[root@localhost test6]# vim func.sh 
#!/bin/bash func() 
{ 
	echo "the function has $# parameters" 
	echo "all parameters are $*" 
	echo "all parameters are $@" 
	echo "the script's name is $0" 
	echo "the first parameter is $1" 
}
func hello world 
[root@localhost test6]# bash func.sh 
the function has 2 parameters 
all parameters are hello world 
all parameters are hello world 
the script's name is func.sh 
the first parameter is hello

函数返回值

首先,用户可以使用return语句来返回某个数值,这与绝大部分的程序设计语言是相同的。但是,在
Shell中,return语句只能返回某个0~255之间的整数值。
在函数中,用户将需要返回的数据写入到标准输出(stdout),通常这个操作是使用echo语句来完成
的。然后在调用程序中将函数的执行结果赋给一个变量。这种做法实际上就是命令替换的一个变种。

[root@localhost test6]# vim length.sh 
#!/bin/bash 
length() 
{ 
	str=$1 
	result=0 
	if [ "$1" != "" ];then 
		result=${#str} 
	fi
	echo "$result" 
}
len=$(length "abcd12") 
echo "the string's length is $len" 

[root@localhost test6]# bash length.sh 
the string's length is 6

简单实战

1.写一个脚本,判定给定的IP列表中的主机哪些在线

#!/bin/bash
online() {
    if ping -c 2 -W 1 $1 &>/dev/null
    then
        return 0
    else
        return 1
    fi
}
for i in $@
do
    if online $i
    then
        echo "$i is up"
    else
        echo "$i is down"
    fi
done

执行结果:
[mmrrj@localhost 5day]$ bash ping_func.sh 192.168.10.{128..130}
192.168.10.128 is up
192.168.10.129 is down
192.168.10.130 is down

2.函数能够接受一个参数,参数为用户名;判断一个用户是否存在如果存在,就返回此用户的shell和UID;并返回正常状态值;如果不存在,就说此用户不存在;并返回错误状态值;

#!/bin/bash
user_id()
{
    if id $1 &>/dev/null
    then
        userinfo=`grep ^$1 /etc/passwd`
        echo  -n "shell is "${userinfo##*:}"\\n"
        echo "UID is "`echo $userinfo|cut -d ":" -f3 `
        return 0
    else
        echo "user not found"
        return 1
    fi
}
echo -e `user_id $1`

执行结果:
[mmrrj@localhost 5day]$ bash user_func.sh mmrrj
shell is /bin/bash
UID is 1000
[mmrrj@localhost 5day]$ bash user_func.sh root
shell is /sbin/nologin
UID is 0
[mmrrj@localhost 5day]$ bash user_func.sh haha
user not found

3.函数库文件:在一个脚本中调用另一个脚本中的函数

#!/bin/bash
func_a() {
    echo "this is function a"
    echo "your whole parameter is '$@'"
}

执行结果:
[mmrrj@localhost ~]$ cd shell/5day/
[mmrrj@localhost 5day]$ func_a
bash: func_a: command not found...
[mmrrj@localhost 5day]$ . func_lib.sh 
[mmrrj@localhost 5day]$ func_a 123 456 abc
this is function a
your whole parameter is '123 456 abc'

4.利用递归求n的阶乘

#!/bin/bash 
Factorial() {
    local n=$1
    if [ "$n" -eq 0 ] 
    then
        echo 1
    else
    let "m=n-1" 
    result=`Factorial "$m"` 
    echo "$result * $n"|bc
    fi 
}
result=`Factorial $1`
echo "Factorial of $1 is $result"

执行结果:
[mmrrj@localhost 5day]$ bash fact.sh 10
Factorial of 10 is 3628800
[mmrrj@localhost 5day]$ bash fact.sh 120
Factorial of 120 is 66895029134491270575881180540903725867527463331380298102956713523016\
33557244962989366874165271984981308157637893214090552534408589408121\
859898481114389650005964960521256960000000000000000000000000000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值