【shell 脚本】求最大公约数

====== gcd.sh======
 #!/bin/bash
 # gcd.sh: 最大公约数
 #         使用Euclid的算法
 
 #  两个整数的"最大公约数" (gcd),
 #+ 就是两个整数所能够同时整除的最大的数.
 
 #  Euclid算法采用连续除法.
 #  在每一次循环中,
 #+ 被除数 <---  除数
 #+ 除数 <---  余数
 #+ 直到 余数 = 0.
 #+ 在最后一次循环中, gcd = 被除数.
 #
# ------------------------------------------------------
 # 参数检查
 ARGS=2
 E_BADARGS=65
 
 if [ $# -ne "$ARGS" ]
 then
   echo "Usage: `basename $0` first-number second-number"
   exit $E_BADARGS
 fi
 # ------------------------------------------------------
 
 
function gcd ()
 {
 
   dividend=$1                    #  随意赋值.
   divisor=$2                     #+ 在这里, 哪个值给的大都没关系.
                                  #  为什么没关系?
 
   remainder=1                    #  如果在循环中使用了未初始化的变量,
                                  #+ 那么在第一次循环中,
                                  #+ 它将会产生一个错误消息.
 
   until [ "$remainder" -eq 0 ]
   do
     let "remainder = $dividend % $divisor"
     dividend=$divisor            # 现在使用两个最小的数来重复.
     divisor=$remainder
   done                           # Euclid的算法
 
 }                                # Last $dividend is the gcd.
 
 gcd $1 $2
 echo; echo "GCD of $1 and $2 = $dividend"; echo
 
 
 # Exercise :
 # --------
 #  检查传递进来的命令行参数来确保它们都是整数.
 #+ 如果不是整数, 那就给出一个适当的错误消息并退出脚本
=======测试=======
root@client.example.com ~/yang # ./gcd.sh
Usage: gcd.sh first-number second-number
root@client.example.com ~/yang # ./gcd.sh  55 3

GCD of 55 and 3 = 1

时间: 2024-08-04 04:45:54

【shell 脚本】求最大公约数的相关文章

用shell脚本求100以内所有正整数之和

#!/bin/bashdeclare -i sum=0for ((i=1;i<=100;i++))dolet sum+=$idoneecho "Sum:$sum"

linux shell脚本问题,急求

问题描述 linux shell脚本问题,急求 #!/bin/bash SERNAME=$1 kill_process() { PID=$(ps -ef | grep $SERNAME | grep -v 'grep' | awk '{print $2}') echo $PID kill -9 $PID sleep 2 } kill_process 这上边的脚本命名为a.sh,我在执行时是 a.sh server1,但会出错,不是报错,是杀错进程,一般显示: 1234 1234 killed 如

shell脚本编程问题,急求

问题描述 shell脚本编程问题,急求 有个问题,写了个shell脚本,执行是会有两个进程出现, jboss5 7852 7678 0 18:01 pts/2 00:00:00 /bin/bash ./logzipok.sh jboss5 7893 7852 0 18:01 pts/2 00:00:00 /bin/bash ./logzipok.sh 这是为是什么,之前没有这问题,在脚本中填入了下面函数,才有这问题,有人知道为什么吗? removevar() { mkdir -p /opt/ap

8个实用的Shell脚本分享

  这篇文章主要介绍了8个实用的Shell脚本分享,本文给出了判断输入为数字.字符或其他.求平均数.自减输出.在文件中添加前缀.批量测试文件是否存在等实用脚本,需要的朋友可以参考下 几个Shell脚本的例子,觉得还不错. [例子:001]判断输入为数字,字符或其他 代码如下: #!/bin/bash read -p "Enter a number or string here:" input case $input in [0-9]) echo -e "Good job, Y

运维-shell脚本一个运行小疑惑

问题描述 shell脚本一个运行小疑惑 一个小的shell#!/bin/bashfor F in 1 2 3 4 5 do echo $Fdone为什么运行起来是: command not found1: command not found2: command not found3: command not found4: command not found5: command not found这里是有什么错误么?求告知 解决方案 没问题的, 解决方案二: 如何确保 Shell 脚本只有一个实

注释-shell脚本修改文件部分内容

问题描述 shell脚本修改文件部分内容 问题描述:想修改文件指定内容 但是行数过多 用sed感觉过于繁琐 有没有像echo一样直接追加到指定位置然后注释原有内容之类的方法?感觉自己钻牛角尖了 没有想明白 求指教随便截个代码片段做例子 # for all of them.LOCAL_PATH = '/tmp'SECRET_KEY='e9c3b51c80a59a17ba8e'# We recommend you use memcached for development; otherwise af

shell脚本 脚本编程-expetc脚本报错问题spawn id exp6 not open

问题描述 expetc脚本报错问题spawn id exp6 not open 先贴代码 主要我用一个shell脚本实现增加linux用户的功能 1 #!/usr/bin/expect -f 2 #!/bin/sh 3 set password 1 4 set name yiyi 5 set num """" 6 spawn adduser $name 7 set timeout 30; 8 expect { 9 password: {send $passwordn

关于mysql shell脚本的问题

问题描述 关于mysql shell脚本的问题 第四行我想打印出结果,正确写法是什么,求大神帮忙!!! 解决方案 #!/bin/sh value=`mysql -uroot -proot -e "use powersystemview; select count(*) from alarminfo;"` echo $value exit 0 解决方案二: shell脚本内与mysql交互shell脚本中使用MySQLMysql压力测试shell脚本

shell脚本运行java的jar包,并且系统是没有安装过jre的,不过有jre没安装过的压缩包

问题描述 shell脚本运行java的jar包,并且系统是没有安装过jre的,不过有jre没安装过的压缩包 图中三条线分别是 jre ,java程序的jar包,和shell脚本 当前linux系统没有安装jre和jdk,我要怎么写shell脚本,才能运行jar包啊.是不是运行jar包之前还要在shell脚本中用命令把jre安装了啊.求大神.在线等......最好把脚本写好了.....谢谢,悬赏大大滴 解决方案 **要知道,java程序是运行在句jvm中的,如果你没有jre就是说明连java的运行

shell脚本中常见的一些特殊符号和作用详解

在编写Shell脚本时,我们需要会用到各种各样的特殊符号,通过这些特殊符号可以使我们编写的代码更加简洁和高效,这里给大家汇总下: 1.{} 大括号: 用法一:通配符扩展eg: ls my_{finger,toe}s这条命令相当于如下命令的组合:ls my_fingers my_toeseg: mkdir {userA,userB,userC}-{home,bin,data}我们将得到 userA-home, userA-bin, userA-data, userB-home, userB-bin