Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 查找匹配的第27行时:语法错误:意外的文件结尾_Bash_Variables_For Loop - Fatal编程技术网

Bash 查找匹配的第27行时:语法错误:意外的文件结尾

Bash 查找匹配的第27行时:语法错误:意外的文件结尾,bash,variables,for-loop,Bash,Variables,For Loop,我试图使用for循环创建一个乘法表,但我不知道如何初始化变量,也不知道在for循环中读取的变量是否需要相同 Syntax: #!/bin/bash #multiplication table #$#=parameter given to the script #$i=variable in for loop #$1=variable represents a valid number not zero #$0=variable represents bash script name echo

我试图使用for循环创建一个乘法表,但我不知道如何初始化变量,也不知道在for循环中读取的变量是否需要相同

Syntax:
#!/bin/bash
#multiplication table
#$#=parameter given to the script
#$i=variable in for loop
#$1=variable represents a valid number not zero
#$0=variable represents bash script name

echo "Enter number you want to multiply"
read varnumber
echo "This number: $varnumber has this multiplication result:

if [ $varnumber -eq 0 ]
then
echo "Error - Number missing from command line argument"
echo "Syntax : $0 number"
echo "Use to print multiplication table for a given number"
exit 1
fi

n=$varnumber
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$varnumber * $i = `expr $i \* $varnumber`"

for循环应以done结尾,因此:

for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$varnumber * $i = `expr $i \* $varnumber`"
done  #line added
此外,这样做也没有害处:

n="$varnumber" 
请注意,在bash中不首选反勾号(``)。出于某种原因,请改用命令
$()
格式。因此:

echo "$varnumber * $i = $(expr $i \* $varnumber)" # Used $() syntax.
看看是什么

事实上,如果不使用外部命令
expr
就可以完成作业,效果会更好:

echo "$varnumber * $i = $((i * varnumber))" # Faster than the previous version

(感谢@benjamin-w的建议)

for循环应以done结尾,因此:

for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$varnumber * $i = `expr $i \* $varnumber`"
done  #line added
此外,这样做也没有害处:

n="$varnumber" 
请注意,在bash中不首选反勾号(``)。出于某种原因,请改用命令
$()
格式。因此:

echo "$varnumber * $i = $(expr $i \* $varnumber)" # Used $() syntax.
看看是什么

事实上,如果不使用外部命令
expr
就可以完成作业,效果会更好:

echo "$varnumber * $i = $((i * varnumber))" # Faster than the previous version

(感谢@benjamin-w的建议)

执行上述操作的简单bash脚本如下所示:

#!/bin/bash

echo "Enter the number for which the multiplication table is desired."
read varname

if [ $varname -eq 0 ];
then
    echo "Error!! The multiplication of anything with 0 results to 0."
    exit 130        # Exit with CTRL + C
fi

for ((i=1; i<=10; i++)); # You can change 10 into any other number
                         # according to your requirement.
                         # Or substitute it with a variable (say N)
                         # which can be prompted to the user.
do
    product=$(expr $i \* $varname)  # Or use: `expr $i \* $varname`
    echo "$varname times $i = $product"
done
如果在提示时输入5,将打印以下内容:

5 times 1  = 5
5 times 2  = 10
5 times 3  = 15
5 times 4  = 20
5 times 5  = 25
5 times 6  = 30
5 times 7  = 35
5 times 8  = 40
5 times 9  = 45
5 times 10 = 50

执行上述操作的更简单的bash脚本如下所示:

#!/bin/bash

echo "Enter the number for which the multiplication table is desired."
read varname

if [ $varname -eq 0 ];
then
    echo "Error!! The multiplication of anything with 0 results to 0."
    exit 130        # Exit with CTRL + C
fi

for ((i=1; i<=10; i++)); # You can change 10 into any other number
                         # according to your requirement.
                         # Or substitute it with a variable (say N)
                         # which can be prompted to the user.
do
    product=$(expr $i \* $varname)  # Or use: `expr $i \* $varname`
    echo "$varname times $i = $product"
done
如果在提示时输入5,将打印以下内容:

5 times 1  = 5
5 times 2  = 10
5 times 3  = 15
5 times 4  = 20
5 times 5  = 25
5 times 6  = 30
5 times 7  = 35
5 times 8  = 40
5 times 9  = 45
5 times 10 = 50

首先测试。祝你好运(您至少需要在for…do…循环的
上关闭
done
)。每个for循环都必须以“done”结束,这就像是循环的结束标记!Shellcheck.net检查给定代码的语法错误?首先测试。祝你好运(您至少需要在for…do…
循环的
上关闭
done
)。每个for循环都必须以“done”结束,这就像是循环的结束标记!Shellcheck.net检查给定代码的语法错误?奇怪的是,赋值的右侧是一个不需要在参数扩展周围加双引号的位置:结果上既不执行文件名扩展,也不执行分词。(尽管最简单的方法可能是制定一个总是将参数扩展放在双引号中的策略。)@ruakh:但也有例外情况。请参阅和下面的评论。除非您非常确定,否则最好用双引号括住变量。
expr
是一个外部命令,为什么不使用算术展开呢<代码>回显“$varnumber*$i=$((i*varnumber))”
?@BenjaminW。为什么不呢?添加了以下内容:-)好东西,但您链接到的页面列出了许多功能,这些功能肯定既没有过时也没有弃用-作者只是出于意见反对它们。类似地,backtick命令替换语法也没有被严格反对,尽管有充分的理由选择
$(…)
语法,如中所述;有关反勾号语法是否真的不受欢迎的讨论,请参阅上的注释。奇怪的是,赋值的右侧实际上不需要在参数扩展周围加双引号:结果上既不执行文件名扩展,也不执行单词拆分。(尽管最简单的方法可能是制定一个总是将参数扩展放在双引号中的策略。)@ruakh:但也有例外情况。请参阅和下面的评论。除非您非常确定,否则最好用双引号括住变量。
expr
是一个外部命令,为什么不使用算术展开呢<代码>回显“$varnumber*$i=$((i*varnumber))”?@BenjaminW。为什么不呢?添加了以下内容:-)好东西,但您链接到的页面列出了许多功能,这些功能肯定既没有过时也没有弃用-作者只是出于意见反对它们。类似地,backtick命令替换语法也没有被严格反对,尽管有充分的理由选择
$(…)
语法,如中所述;有关backtick语法是否真的不推荐使用的讨论,请参阅上的评论。感谢您的回复!我会用这种方式试试的谢谢你的回复!我想这样试试