linux-shell5
一,shell中函数定义function 函数名()
{
shell命令
}
如:
function hello()
{
echo "how are you."
}
二,shell中传递参数
#!/bin/bash
function hello()
{
echo "$1 :how are you!"
return 1
}
echo -e "begin to call function hello\n"
hello zangsan
echo "function back"
三,函数调用
使用“.【空格】函数文件名”格式引入函数,如
#!/bin/bash
. function2.sh
echo -e "begin to call function hello\n"
hello
echo "function back"
四,函数返回状态
$?
shell函数无返回值,只有返回状态
五,shell中shift的应用
#!/bin/bash
usage()
{
echo "usage:'bshname $0' filenames"
}
totalline=0
if [ $# -lt 2 ]
then
usage
fi
while [ $# -ne 0 ]
do
line=`cat $1|wc -l`
echo "$1 : $line"
totalline=$[ $totalline+$line ]
shift
done
echo "---end---"
echo "total:${totalline}"
页:
[1]