Linux 我的Bash代码中发生了什么?
当我想在屏幕上显示我的日历时,脚本向我显示了一个条件错误——如果不是这样的话——但我不知道为什么;我想应该没问题Linux 我的Bash代码中发生了什么?,linux,bash,Linux,Bash,当我想在屏幕上显示我的日历时,脚本向我显示了一个条件错误——如果不是这样的话——但我不知道为什么;我想应该没问题 # !/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 se
# !/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
假设(仅为了示例)mostrar0,则此行:
if[$mostrar = 1] then
表示“如果[0
带有参数=
,1]
和然后,则运行命令”。由于没有名为if[0
的命令,因此会出现问题
您可以通过在命令的各个组件周围添加空格来解决大部分问题。此外,您需要在之后的之前使用分号或换行符,并且应该将$mostrar
扩展用双引号括起来,以防止文件名扩展和分词(这两种情况都会导致奇怪的结果)。因此:
您是否尝试过如果[$mostrar=1];然后用分号-是的,但我不知道为什么如果更改if[$mostrar=1]有效请给出更有意义的主题,如“if语句在bash中不起作用”请注意,第一行的shebang需要在#
和!
之间没有空格的情况下启动#
。它通常会工作,直到您开始使用sh
无法识别的Bash功能,这将用于运行用#!/bin/Bash
编写的代码(因为空格阻止了原本应该是shebang的内容成为shebang)。您需要非常小心地快速使用空格-空格在shell脚本中非常重要(无论它们在哪里)。试试看,它会自动检测到类似这样的问题
if [ "$mostrar" = 1 ] ; then