我无法弄清楚以下脚本有什么问题。
#!/bin/bash
if [ "$1" = "x" ]
then
echo Pushing web on sharada to origin.
else if [ "$1" = "y" ]
then
echo Pulling web on sharada from origin.
else
echo "Usage : arg x to push or y to pull."
fi
我在xterm
.
我无法弄清楚以下脚本有什么问题。
#!/bin/bash
if [ "$1" = "x" ]
then
echo Pushing web on sharada to origin.
else if [ "$1" = "y" ]
then
echo Pulling web on sharada from origin.
else
echo "Usage : arg x to push or y to pull."
fi
我在xterm
.
你最后错过了关闭“fi”。else if
构造实际上不是elif
延续,而是新的生活if
在前一个的else
子句中if
。
所以,正确格式化你应该有:
#!/bin/bash
if [ "$1" = "x" ]
then
echo Pushing web on sharada to origin.
else
if [ "$1" = "y" ]
then
echo Pulling web on sharada from origin.
else
echo "Usage : arg x to push or y to pull."
fi
fi # <-- this closes the first "if"
它应该是elif
,而不是else if
,如下所示:
if [ "$1" = "x" ]
then
echo Pushing web on sharada to origin.
elif [ "$1" = "y" ]
then
echo Pulling web on sharada from origin.
else
echo "Usage : arg x to push or y to pull."
fi
你有两个if
s,因此你需要两个fi
s。
问题中描述的问题已经解决。我只是想在这里分享我的经验,因为我得到的错误消息是相同的,即使原因和解决方案略有不同。
#!/bin/bash
echo "a"
if [ -f x.txt ]; then \
echo "b" \
fi;
echo "c"
该脚本产生了相同的错误消息,解决方案是将缺少的分号添加到echo "b"
行尾:
#!/bin/bash
echo "a"
if [ -f x.txt ]; then \
echo "b" ; \
fi;
echo "c"
直接原因与原问题相同。if
没有关闭fi
对子。然而,原因是由于缺少分号,该fi
行混入了前一行。;