【发布时间】:2021-03-09 15:58:14
【问题描述】:
我使用了 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
【问题讨论】:
-
请仅使用
2作为您的论点重新编辑您的Q。然后请包括您对该输入的预期输出。祝你好运。 -
看起来你在循环之前设置了
prod,但从不更新它;因此,它永远不会从“2”改变值。 -
当您执行
a=1; b=2; prod=$((a*b))时,它将使用a和b的当前值,并等效于prod=2。如果您更改a或b并希望prod成为新产品,您应该再次更改prod=$((a*b))。
标签: bash shell scripting git-bash