#!/bin/bash# 首先判断输入的参数里面是否含有字符串function mycount(){
i=1
while["$i" -le "$#"]do# 现在 通过${!i}去实现第一个参数,第二个参数,第n个参数的效果ifecho${!i}|grep -q '[^1-9]'thenecho"your input contain str,please input again."exit 1
filet i++ # 让i递增done
i=1
while["$i" -le "$#"]dolet sum=$sum+${!i}let i=$i+1
doneecho$sum}function menu(){cat<<EOF
`echo"*****************this is a counting program start***************************"``echo"1.对你输入的参数实行累加"``echo"2.不做任何操作,直接退出"``echo"*****************this is a counting program end*****************************"`
EOFread -p "请输入上述编号" num
case$numin
1)
mycount $@;;
2)echo"正常退出程序";;
*)echo"你输入的$num为非法输入,异常退出"
esac
}
menu $@
代码演示
[dayuanshuai@IDYS sh_day01]$ ./countv2.sh 12 13 14
*****************this is a counting program start***************************
1.对你输入的参数实行累加
2.不做任何操作,直接退出
*****************this is a counting program end*****************************
请输入上述编号1
39
[dayuanshuai@IDYS sh_day01]$ ./countv2.sh 12 13 14
*****************this is a counting program start***************************
1.对你输入的参数实行累加
2.不做任何操作,直接退出
*****************this is a counting program end*****************************
请输入上述编号2
正常退出程序
[dayuanshuai@IDYS sh_day01]$ ./countv2.sh 12 13 asa
*****************this is a counting program start***************************
1.对你输入的参数实行累加
2.不做任何操作,直接退出
*****************this is a counting program end*****************************
请输入上述编号1
your input contain str,please input again.