shell函数的使用比较简单,要点包括定义、参数来源、返回值;
在shell函数里,结合read命令,可以实现在执行脚本的过程中让用户选择参数,增强脚本的交互性;
脚本参数在脚本名称后面输入,函数参数在函数名后面输入;
printf和echo有点区别,printf可以格式化字符串,并且默认不换行;
例如:
#!/bin/sh
compare()
{
while [ -z $a ] && [ -z $b ]
do
printf %s $1
read a b
if [ $a -lt $b ]
then
return $b
elif [ $a -gt $b ]
then
return $a
else
return 0
fi
done
}
compare "请输入两个数,空格分隔:"
data=$?
echo $data