当我想在屏幕上显示日历时,脚本向我显示条件错误 - if else - 但我不知道为什么;我想应该没问题。
# !/bin/bash
rm --f calen
mostrar = 0
echo "agrega un mes"
read mes
echo "agrega un año"
read year
echo "Agregar [1] para mostrar las dos primeras semanas, o [2] para mostrar las ultimas dos semanas"
read mostrar
if[$mostrar = 1] then
cal -m $mes $year >> calen
head -n 4 calen
else
cal -m $mes $year >> calen
head -n 2 calen
tail -n 3 calen
fi
最佳答案
假设(仅作为示例)mostrar
为 0
,则这一行:
if[$mostrar = 1] then
表示“使用参数 =
、1]
和 then
运行命令 if[0
”。由于您没有名为 if[0
的命令,因此会发生错误。
您可以通过在命令的各个组件周围添加空格来解决大部分问题。此外,您需要在 then
之前使用分号或换行符,并且应该将 $mostrar
扩展用双引号引起来,以防止文件名扩展和分词(这两者都可能导致奇怪的结果)。所以:
if [ "$mostrar" = 1 ] ; then
关于linux - 我的 Bash 代码发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38194293/