原文:http://bash.cyberciti.biz/academic/script-to-find-out-odd-number-2/ 整数中,能被2整除的数是偶数,反之是奇数, 偶数=2乘以任意一个整数,奇数=2乘以任意一个整数+1.
算法:取出两个输入数字的中间的所有整数,逐一对这些整数除以2,能被整除的数淘汰掉,其余的留下来。
shell程序代码如下:
#!/bin/bash
# Shell program to read two numbers and display all the odd
# numbers berween those two number
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
echo -n "输入第一个数字 : "
read n1
echo -n "输入第二个数字 : "
read n2
if [ $n2 -gt $n1 ];
then
for(( i=$n1; i<=$n2; i++ ))
do
# see if it is odd or even number 判断奇数还是偶数
test=$(( $i % 2 ))
if [ $test -ne 0 ];
then
echo $i
fi
done
else
echo "数字 $n2 要求大于数字 $n1, 请重新输入数字..."
fi |