标签:bash
写一个脚本:
输入三个参数 0,1,2,3之一
输入3;计算100以内所有能被3整除的正整数的和;
输入2;计算100以内所有所有偶数的和;
输入1;计算100以内所有所有奇数的和;
输入0;计算100以内所有所有整数的和;
代码
#!/bin/bash # declare -i SUM=0 if [ $1 == 3 ];then for I in {1..100};do if [ $[$I%3] -eq 0 ];then SUM=$[$SUM+$I] fi done echo $SUM elif [ $1 == 2 ];then for I in {1..100};do if [ $[$I%2] -eq 0 ];then SUM=$[$SUM+$I] fi done echo $SUM elif [ $1 == 1 ];then for I in {1..100};do if [ $[$I%2-1] -eq 0 ];then SUM=$[$SUM+$I] fi done echo $SUM elif [ $1 == 0 ];then for I in {1..100};do SUM=$[$SUM+$I] done echo $SUM else echo "Usage: ./for1003.sh ARG [0,1,2,3]" fi
执行结果
[root@localhost ~]# ./for1003.sh 0 5050 [root@localhost ~]# ./for1003.sh 1 2500 [root@localhost ~]# ./for1003.sh 2 2550 [root@localhost ~]# ./for1003.sh 3 1683 [root@localhost ~]# ./for1003.sh 4 Usage: ./for1003.sh ARG [0,1,2,3]
标签:bash
原文地址:http://sharewaytogo.blog.51cto.com/2816479/1846627