linux shell之函数返回值

返回值0表示无错误,1表示有错误

vi function7.sh
#!/bin/bash

#使用return语句的函数
show_week()
{
echo -n "What you input is: "
echo "$1"
#根据输入的参数值来显示不同的操作
case $1 in
0)
echo "Today is Sunday. "
return 0;;
1)
echo "Today is Monday. "
return 0;;
2)
echo "Today is Tuesday. "
return 0;;
3)
echo "Today is Wednesday. "
return 0;;
4)
echo "Today is Thursday. "
return 0;;
5)
echo "Today is Friday. "
return 0;;
6)
echo "Today is Saturday. "
return 0;;
*)
return 1;;
esac
}

#主程序部分根据返回值不同输出不同的结果
if show_week "$1" #在if中调用函数
then
echo "What you input is right! " #提示参数输入正确
else
echo "What you input is wrong! " #提示参数输入错误
fi

exit 0

./function7.sh 1
What you input is: 1
Today is Monday.
What you input is right!
./function7.sh 10
What you input is: 10
What you input is wrong!

posted @ 2021-04-09 17:18  zhudaheng123  阅读(586)  评论(0)    收藏  举报