专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

shell程序:读取两个输入数字并显示中间的奇数

百度收藏 QQ搜藏

原文: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
上一篇:shell程序循环显示1到10的数字 下一篇:用shell脚本显示文件下载进度功能

power by soyo123 2007-2008