【问题标题】:bash script to find the product of two numbers in succession to each other up to a max numberbash脚本,以查找两个数字的乘积,最多可达最大数字
【发布时间】:2021-03-09 13:28:23
【问题描述】:

嘿,伙计们,我正在尝试编写一个脚本来连续查找两个数字的乘积,例如 1*2 2*3 3*4 等...直到最大数字,但我在第 14 行不断收到此错误

./test.sh: line 14: ((: 2=1: attempted assignment to non-variable (error token is "=1")

并且不确定我做错了什么非常新的 bash 任何帮助将不胜感激。

#!/bin/bash
#A script to find the products of two nonnegative numbers in succesion 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 succesion with each other up to."
read maxnum
echo "Ok all the products of numbers in succesion to each other from 1 to" $maxnum  "are as follows."
for (( $b=1; $b<=$maxnum; $bb++ ))
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"

【问题讨论】:

  • 你能发布一个你的输入和预期输出的例子
  • 初始设置后“prod”脚本将在哪里修改?
  • 请先将您的脚本粘贴到shellcheck.net,然后尝试实施那里提出的建议。
  • 所以基本上我正在尝试编写一个脚本,允许用户输入一个数字范围的最大数字,比如 10,然后程序将两个数字的集合连续相乘,直到像这样的给定范围 请输入范围 10 的最大数字 好的 从 1 到“$maxnum 的所有数字的连续乘积如下。1. 2 2. 6 3. 12 4. 20 5. 30 6 . 42 7. 56 8. 72 9. 90
  • 请更新问题以包含所需的输出

标签: bash shell scripting git-bash


【解决方案1】:
for (($b=1;...
#     ~
#     ^

不要在作业中使用美元符号。

顺便说一句,您可以省略算术表达式中的所有美元符号。

for ((b=1; b<=maxnum; b++ ))

您确定bb 不应该只是b

【讨论】:

  • 是的,它应该只是因为改变了它,并且程序在使用 shellcheck.net 后现在运行但它没有运行,因为它应该输出不完全正确我将更新帖子以反映这一点
  • 不,请提出一个新问题。更改它会使现有的 cmets 和答案无用。
  • 抱歉我不知道
猜你喜欢
  • 2021-03-09
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多