[英]Why do I keep getting syntax error in my bash script? Does it have something related to the “:”?
[英]Why do I have a syntax error in this bash code?
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 以及應該有多少項是 output。 U=a(n)+b 然后為相同的線性序列選擇其他變量和 output 的計數之和。
您在開頭缺少一個 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.