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

前端 未结 4 716
孤城傲影
孤城傲影 2021-01-27 23:37

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

#!/bin/bash 
if [ \"$1\" = \"x\" ] 
then
  echo Pushing web on sharada to origin.
else if [ \"$1\" = \"y         


        
相关标签:
4条回答
  • 2021-01-27 23:44

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

    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
    
    0 讨论(0)
  • 2021-01-27 23:51

    you are missing closing "fi" at the end. the else if construct really is not an elif continuation, but instead the new if lives within the else clause of the previous if.

    so, properly formatted you should have:

    #!/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"
    
    0 讨论(0)
  • 2021-01-27 23:52

    The problem described in the questioned has already been solved. I just wanted to share my experience here because the error message I got was the same, even if the reason and the solution was slightly different.

    #!/bin/bash
    
    echo "a"
    
    if [ -f x.txt ]; then \
        echo "b" \
    fi;
    
    echo "c"
    

    This script produced the same error message and the solution was to add the missing semicolon to the end of the echo "b" line:

    #!/bin/bash
    
    echo "a"
    
    if [ -f x.txt ]; then \
        echo "b" ; \
    fi;
    
    echo "c"
    

    The direct reason was the same as it was in the original question. The if didn't have its closing pair fi. The reason, however, was that the fi line blended into the previous line, due to the missing ; semicolon.

    0 讨论(0)
  • 2021-01-28 00:06

    You have two ifs, therefore you need two fis.

    0 讨论(0)
提交回复
热议问题