【问题标题】:Why do I have a syntax error in this bash code?为什么我在这个 bash 代码中有语法错误?
【发布时间】:2021-03-18 10:07:29
【问题描述】:
a=0
b=0
numterms=0
echo -e -n "\nEnter a number for variable a: "
read a
echo ""
echo -e -n "\nEnter a number for variable b: "
read b
echo ""
echo -e -n "\nEnter a number for how many terms printed: "
read numterms
echo ""
n=1
numtermss=$(($numterms+1))
while [ $n -lt $numtermss ]
do
        sum=$(($a * $n + $b))
        n=$(($n + 1))
        echo $sum
        if [ $numterms -lt 0 ]
        then
                echo -e -n "Number needs to be postive."
                echo ""
        else
                echo -e -n "\nEnter another number for variable a: "
                read aa
                echo ""
                echo -e -n "n\Enther another number for variable b: "
                read bb
                echo ""
                echo -e -n "n\Enter another number for amount of terms: "
                read terms
                echo ""
                        aa=0
                        bb=0
                        terms=0
                        for i in "$@"
                        do
                        number=(head -1 "$i")
                        bb=$((bb+1))
                        aa=$((aa + number))
                        termss=$(($terms+1))
                        echo " The sum is $aa"
fi
done

我试图让 bash 代码创建一个线性序列,其中用户输入变量 a、b 以及应该输出多少项。 U=a(n)+b 然后为同一线性序列选择其他变量并输出计数之和。

【问题讨论】:

  • 使用shellcheck.net。此外,您可以尝试删除(/注释掉)代码部分以找出问题的来源。
  • 您似乎缺少内部循环的“完成”。 number=(head 缺少 $

标签: bash git-bash


【解决方案1】:

您在开头缺少一个 shebang,在结尾的 for 循环中缺少一个分号,以及一个 done to 和 for 循环。我还添加了一个正确的退出命令。

#!/bin/bash

a=0
b=0
numterms=0
echo -e -n "\nEnter a number for variable a: "
read a
echo ""
echo -e -n "\nEnter a number for variable b: "
read b
echo ""
echo -e -n "\nEnter a number for how many terms printed: "
read numterms
echo ""
n=1
numtermss=$(($numterms+1))
while [ $n -lt $numtermss ]
do
        sum=$(($a * $n + $b))
        n=$(($n + 1))
        echo $sum
        if [ $numterms -lt 0 ]
        then
                echo -e -n "Number needs to be postive."
                echo ""
        else
                echo -e -n "\nEnter another number for variable a: "
                read aa
                echo ""
                echo -e -n "n\Enther another number for variable b: "
                read bb
                echo ""
                echo -e -n "n\Enter another number for amount of terms: "
                read terms
                echo ""
                        aa=0
                        bb=0
                        terms=0
                        for i in "$@";
                        do
                        number=(head -1 "$i")
                        bb=$((bb+1))
                        aa=$((aa + number))
                        termss=$(($terms+1))
                        echo " The sum is $aa"
                done
fi
done
exit 0

【讨论】:

    猜你喜欢
    • 2011-03-03
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2015-08-04
    相关资源
    最近更新 更多