[Email protected] ~]#CatJiechen.SH #!/bin/bash# Design a shell program to calculate the factorial of N, required: #1. Receive parameters from the command line n;#2to determine the legality of n immediately after the commencement of the program, that is, whether there are parameters, if any, if there is a positive integer, if illegal, please give the wrong hint. #3. The result of the final calculation
Num=$1Expr$num +1&>/dev/NULL[ $? -ne0] &&Echo "Please input a number."&& exit2[ $# -ne1] &&Echo 'usage:$0 Number'&& exit1[$num-le0] &&Echo "Please input a number bigger than 0"&& exit3s=1 forIinch`seq 1$num ' Dos=$ (($s *$i)) DoneEcho$s [[email protected]~]# bash Jiechen.SH 5 -[[Email protected]~]# bash Jiechen.SH 36[[Email protected]~]# bash Jiechen.SH 22[[Email protected]~]# bash Jiechen.SH 11[[Email protected]~]#
Shell script that calculates the factorial of n by a shell program.