[[email protected] ~]# cat jiechen.sh #!/bin/bash #设计一个shell程序计算n的阶乘,要求: #1.从命令行接收参数n; #2.在程序开始后立即判断n的合法性,即是否有参数,若有是否为正整数,若非法请给错误提示。 #3.最后出计算的结果 num=$1 expr $num + 1 &>/dev/null [ $? -ne 0 ] && echo "please input a number." && exit 2 [ $# -ne 1 ] && echo ‘Usage:$0 number‘ && exit 1 [ $num -le 0 ] && echo "please input a number bigger than 0" && exit 3 s=1 for i in `seq 1 $num` do s=$(($s*$i)) done echo $s [[email protected] ~]# bash jiechen.sh 5 120 [[email protected] ~]# bash jiechen.sh 3 6 [[email protected] ~]# bash jiechen.sh 2 2 [[email protected] ~]# bash jiechen.sh 1 1 [[email protected] ~]#
时间: 2024-10-11 19:11:31