Advanced Bash-scripting Guide (19th): maximum common divisor of two integers, bashscripting

Source: Internet
Author: User

Advanced Bash-scripting Guide (19th): maximum common divisor of two integers, bashscripting

The example selected in this article is from the book "Advanced Bash-scripting Gudie", translated by Yang chunmin Huang Yi

1 #! /Bin/bash 2 # calculate the maximum common divisor of two integers 3 4 E_BADARGS = 65 5 6 # if the number of parameters is not 2, exit 7 if [$ #-ne 2] 8 then 9 echo "Usage: 'basename $ 0' first-number second-number "10 exit $ E_BADARGS11 fi12 13 # If the parameter is not an integer or the parameter value is 0, exit 14 for I in $ @ 15 do16 if [$ I = ~ [0-9] +] # "= ~ "Followed by the regular expression, + in the regular expression, it indicates that the previous content matches at least once 17 then18 if [$ I-eq 0] 19 then20 echo" Usage: 'basename $ 0' parameter can't be zero "21 exit $ E_BADARGS22 fi23 else24 echo" Usage: 'basename $ 0' parameter must be integer "25 exit $ E_BADARGS26 fi27 done28 29 # design a gcd () function and calculate the maximum approx. 30 gcd () using the moving phase division (Euclidean Algorithm () 31 {32 remainder = 133 dividend = $134 divisor = $235 36 until [$ remainder-eq 0] 37 do38 let "remainder = $ dividend % $ divisor" 39 dividend = $ divisor40 divisor = $ remainder41 done42} 43 44 gcd $1 $245 46 echo "gcd of $1 and $2 is: $ devidend "47 48 exit 0

When adapting this script, I mainly consider the following:

1. Do you want to exclude non-Integer Parameters?

For the first time, I use echo $ I | sed '/s/^ [0-9] * $/''/G' & echo $? If the first command is correctly executed, $? 0 should be returned, but we have a better method, that is, "= ~ "Followed by regular expressions

2. If the parameter value is 0, do you want to exclude it?

When $ I is judged as an integer, another nested judgment [$ I-eq 0]

3. How to control the number of parameters?

[$ #-Eq 2] or [$ #-ne 2] can exclude null parameters or the number of parameters is not 2.

4. What is the processing of $1 <$2 in Euclidean algorithms?

First look at the situation of $1> $2

$1 = 65 $2 = 15

First loop: 5 = 65% 15

Dividend = 15

Divisor = 5

The second cycle 0 = 15% 5

Dividend = 5

Divisor = 0

Exit the loop, gcd = $ dividend = 5

Let's look at the situation of $1 <$2.

$1 = 15 $2 = 65

First cycle: 15 = 15% 65

Dividend = 65

Divisor = 15

Second cycle: 5 = 65% 15

Dividend = 15

Divisor = 5

Third Cycle: 0 = 15% 5

Dividend = 5

Divisor = 0

Exit the loop, gcd = $ dividend = 5

We can see that $1 <$2 has a loop more than $1> $2, and the result is the same.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.