bash 脚本,用于查找两个连续数字的乘积,直到最大数字

bash script to find the product of two numbers in succession up to a max number

我已经使用了 shellcheck.net,并且脚本运行了,但输出与我预期的不同,这里是脚本和输出,我再次尝试让脚本显示 1 的结果*2 2*3 3*4 等...直到范围的给定最大数量并显示计数,如 1. 2. 3. 等...任何输入都会有很大帮助。

#!/bin/bash
#A script to find the products of two nonnegative numbers in succession to each other up to a maximum number
a=1
b=2
prod=$((a*b))
count=1
echo "Input The max number to find product of numbers in succession with each other up to."
read -r maxnum
echo "Ok all the products of numbers in succesion to each other from 1 to $maxnum are as follows."
for (( b=2; b<=maxnum; ++b ))
do
        for (( a=1; a<b; ++a ))
        do
                for (( count=1; count<=maxnum; ++count ))
                do
                        echo "$count. $prod"
                done
        done
done
echo "You have now reached the end of your range of products with integers in succession to each other"

输出

Input The max number to find product of numbers in succesion with each other up to.
5
Ok all the products of numbers in succesion to each other from 1 to 5 are as follows.
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2 
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
You have now reached the end of your range of products with integers in succesion to each other

如果我没理解错的话,你会喜欢这样的:

#!/bin/bash
#A script to find the products of two nonnegative numbers in succession to each other up to a maximum number
echo "Input The max number to find product of numbers in succession with each other up to."
read -r maxnum
echo "Ok all the products of numbers in succesion to each other from 1 to $maxnum are as follows."
for (( count=1; count<maxnum; ++count ))
do
    a=$count
    b=$((count+1))
    prod=$((a*b))
    echo "$count. $prod"
done
echo "You have now reached the end of your range of products with integers in succession to each other"

没必要用那么多for循环。只有一次迭代计数,您可以定义连续的数字 a=count 和 b=count+1 直到达到 maxnum。这也是您正确索引迭代的唯一方法,因为在您的代码中,您正在为两个更多不必要的循环迭代 a 和 b 重复计数。

另外你应该建立一个条件,你的读取最大值不能小于2,否则你没有两个连续的数字相乘。

将这个(和上一个)问题的各种评论汇集在一起​​,并进行一些清理:

$ cat abc.bash
#!/bin/bash

echo "Input The max number to find product of numbers in succession with each other up to."

read -r maxnum

echo "Ok all the products of numbers in succesion to each other from 1 to $maxnum are as follows."

for (( a=1; a<maxnum; a++ ))
do
    printf "%s.%s\n" "${a}" $((a*(a+1)))
done

echo "You have now reached the end of your range of products with integers in succession to each other"

几个样本运行:

$ abc.bash
Input The max number to find product of numbers in succession with each other up to.
3
Ok all the products of numbers in succesion to each other from 1 to 3 are as follows.
1.2
2.6
You have now reached the end of your range of products with integers in succession to each other

$ abc.bash
Input The max number to find product of numbers in succession with each other up to.
5
Ok all the products of numbers in succesion to each other from 1 to 5 are as follows.
1.2
2.6
3.12
4.20
You have now reached the end of your range of products with integers in succession to each other

注意:可能需要返回并根据 OP 所需的输出调整代码(仍将添加到问题中)。