2016-07-05 66 views
-1

當我想在屏幕上顯示我的日曆時,腳本向我顯示有條件的錯誤 - 如果其他 - 但我不知道爲什麼;我想這應該沒問題。我的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 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 
+2

你試過了嗎if [$ mostrar = 1];然後' - 用分號? – TessellatingHeckler

+0

是的,但我不知道爲什麼如果更改,如果[$ mostrar = 1]工作 –

+0

請給予更多有意義的主題,如「如果陳述不在bash中工作」 –

回答

4

假設(只是舉例的緣故),該mostrar0,那麼這行:

if[$mostrar = 1] then 

的意思是 「與參數=1]運行命令if[0,和then」。由於您沒有命名爲if[0的命令,因此會爆炸。

您可以通過在命令的各個組件周圍添加空格來解決大部分問題。此外,在then之間需要分號或換行符,應將$mostrar擴展換成雙引號,以防止文件名擴展和分詞(這兩者都會導致奇怪的結果)。所以:

if [ "$mostrar" = 1 ] ; then 
相關問題