linux中脚本退出函数,Linux命令shell脚本之09(函数)1.使⽤函数
[oracle@XAG143 myshell]$ cat test_fun1.sh
#!/bin/bash
# using a function in a script
function func1
{
echo "This is an example of a function"
}
count=1
while [ $count -le 3 ]
do
func1
count=$[ $count + 1 ]
done
echo "This is the end of the loop"
func1
echo "Now this is the end of the script"
[oracle@XAG143 myshell]$ ./test_fun1.sh
This is an example of a function
This is an example of a function
This is an example of a function
This is the end of the loop
This is an example of a function
Now this is the end of the script
2.函数返回值(默认退出状态码)
默认情况下,函数的退出状态码是函数中最后⼀条命令返回的退出状态码。
在函数执⾏结束后,可以⽤标准变量$?来确定函数的退出状态码
$ cat test4.sh
#!/bin/bash
# testing the exit status of a function
func1() {
echo "trying to display a non-existent file"
ls -l badfile
}
echo "testing the function: "
func1
echo "The exit status is: $?"
$ ./test4.sh
testing the function:
trying to display a non-existent file
ls: badfile: No such file or directory
The exit status is: 1
#函数的退出状态码是1,这是因为函数中的最后⼀条命令没有成功运⾏。但你⽆法知道函数中其他命令中是否成功运⾏使⽤函数的默认退出状态码是很危险的
3.函数返回值(使⽤ return 命令)
$ cat test5.sh
#!/bin/bash
# using the return command in a function
function dbl {
read -p "Enter a value: " value
echo "doubling the value"
return $[ $value * 2 ]
}
dbl
echo "The new value is $?"
$ ./test5.sh
Enter a value: 2
doubling the value
The new value is 2
$ ./test5.sh
Enter a value: 200
doubling the value
The new value is 1
记住,函数⼀结束就取返回值;
记住,退出状态码必须是0~255。
要返回较⼤的整数值或者字符串值的话,你就不能⽤这种返回值的⽅法了
4.函数返回值(使⽤函数输出)
会⽤echo语句来显⽰计算的结果
$ cat test5b.sh
#!/bin/bash
# using the echo to return a value
function dbl {
read -p "Enter a value: " value
echo $[ $value * 2 ]
}
result=$(dbl)
echo "The new value is $result"
$ ./test5b.sh
Enter a value: 200
The new value is 400
你会注意到dbl函数实际上输出了两条消息。read命令输出了⼀条简短的消息来向⽤户询问输⼊值。
bash shell脚本⾮常聪明,并不将其作为STDOUT输出的⼀部分,并且忽略掉它。
如果你⽤echo语句⽣成这条消息来向⽤户查询,那么它会与输出值⼀起被读进shell变量中。
5.向函数传递参数
函数名会在$0
变量中定义,函数命令⾏上的任何参数都会通过$1、$2等定义。也可以⽤特殊变量$#来判断传给函数的参数数⽬[oracle@XAG143 myshell]$ cat test_fun6.sh
#!/bin/bash
# passing parameters to a function
function addem
{
if [ $# -eq 0 ] || [ $# -gt 2 ]; then
echo -1
elif [ $# -eq 1 ]; then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
echo -n "Adding 10 and 15: "
value=$(addem 10 15)
echo -n "Let's try adding just one number: "
value=$(addem 10)
echo $value
echo -n "Now trying adding no numbers: "
value=$(addem)
echo $value
echo -n "Finally, try adding three numbers: "
value=$(addem 10 15 20)
echo $value
[oracle@XAG143 myshell]$ ./test_fun6.sh
Adding 10 and 15: 25
Let's try adding just one number: 20
Now trying adding no numbers: -1
Finally, try adding three numbers: -1
6.获取脚本在命令⾏中的参数值
[oracle@XAG143 myshell]$ cat test_fun7.sh
#!/bin/bash
# trying to access script parameters inside a function function func7
{
echo $[ $1 * $2 ]
}
if [ $# -eq 2 ]; then
value=$(func7 $1 $2)
echo "The result is $value"
else
echo "Usage: badtest1 a b"
fi
[oracle@XAG143 myshell]$ ./test_fun7.sh 4 5
The result is 20
8.函数中局部变量
只要在变量声明的前⾯加上local关键字就可以了
[oracle@XAG143 myshell]$ cat test_fun8.sh
# trying to access script parameters inside a function function func7
{
local temp=$[ $1 * $2 ]
echo $[ $temp * 2 ]
}
if [ $# -eq 2 ]; then
value=$(func7 $1 $2)
echo "The result is $value"
else
echo "Usage: badtest1 a b"
fi
[oracle@XAG143 myshell]$ ./test_fun8.sh 4 5
The result is 40
9.向函数传数组参数
[oracle@XAG143 myshell]$ cat test_fun10.sh
#!/bin/bash
# array variable to function test
function testit
{
local newarray
#newarray=$1
# or
newarray=($(echo "$@"))
shell脚本返回执行结果
echo "The new array value is: ${newarray[*]}"
for var in ${newarray[@]}; do
echo "打印的内容@:" $var
done
for var2 in ${newarray[*]}; do
echo "打印的内容*:" $var2
done
}
myarray=(1 2 3)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。