奇怪的“语法错误:文件意外结束”在 bash 脚本中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13825504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:00:03  来源:igfitidea点击:

Weired 'syntax error: unexpected end of file' in bash script

bash

提问by Dilawar

I can't figure out what is wrong with following script.

我无法弄清楚以下脚本有什么问题。

#!/bin/bash 
if [ "" = "x" ] 
then
  echo Pushing web on sharada to origin.
else if [ "" = "y" ] 
then 
  echo Pulling web on sharada from origin.
else 
  echo "Usage : arg x to push or y to pull."
fi

I am on linux (Ubuntu) in xterm.

我在xterm.

回答by uml?ute

you are missing closing "fi" at the end. the else ifconstruct really is not an elifcontinuation, but instead the new iflives within the elseclause of the previous if.

你最后错过了关闭“fi”。该else if构造实际上不是elif延续,而是前一个ifelse子句中的新生命if

so, properly formatted you should have:

所以,正确格式化你应该有:

#!/bin/bash 
if [ "" = "x" ] 
then
  echo Pushing web on sharada to origin.
else 
  if [ "" = "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"

回答by dogbane

It should be elif, not else if, as shown below:

应该是elif,不是else if,如下图:

if [ "" = "x" ] 
then
  echo Pushing web on sharada to origin.
elif [ "" = "y" ] 
then 
  echo Pulling web on sharada from origin.
else 
  echo "Usage : arg x to push or y to pull."
fi

回答by Ignacio Vazquez-Abrams

You have two ifs, therefore you need two fis.

您有两个ifs,因此您需要两个fis。