linux shell script Syntax error: word unexpected (expecting “fi”)Bash script throws “syntax error near unexpected token `}'” when runSyntax error near unexpected token `('Shell script If syntax errorline 42: syntax error: unexpected end of file-bash: syntax error near unexpected token `&'“bash: syntax error near unexpected ')” in Linux ShellSyntax error: word unexpected (expecting “)”)running simple bash script fails with Syntax error: word unexpected (expecting “)”)Bash script: syntax error near unexpected token `('unexpected EOF and syntax error:

"It doesn't matter" or "it won't matter"?

Why is so much work done on numerical verification of the Riemann Hypothesis?

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

Can you use Vicious Mockery to win an argument or gain favours?

Can I cause damage to electrical appliances by unplugging them when they are turned on?

How to draw a matrix with arrows in limited space

A Trivial Diagnosis

How to get directions in deep space?

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

How to convince somebody that he is fit for something else, but not this job?

Shouldn’t conservatives embrace universal basic income?

Does Doodling or Improvising on the Piano Have Any Benefits?

Did the UK lift the requirement for registering SIM cards?

Will the Sticky MAC access policy prevent unauthorized hubs from connecting to a network?

When were female captains banned from Starfleet?

Review your own paper in Mathematics

15% tax on $7.5k earnings. Is that right?

Confused about Cramer-Rao lower bound and CLT

What is the difference between lands and mana?

What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?

Doesn't the system of the Supreme Court oppose justice?

What do you call a word that can be spelled forward or backward forming two different words

Why do Radio Buttons not fill the entire outer circle?



linux shell script Syntax error: word unexpected (expecting “fi”)


Bash script throws “syntax error near unexpected token `}'” when runSyntax error near unexpected token `('Shell script If syntax errorline 42: syntax error: unexpected end of file-bash: syntax error near unexpected token `&'“bash: syntax error near unexpected ')” in Linux ShellSyntax error: word unexpected (expecting “)”)running simple bash script fails with Syntax error: word unexpected (expecting “)”)Bash script: syntax error near unexpected token `('unexpected EOF and syntax error:













0















Below I have a shell script i wrote and when I try to execute it I get an error in my terminal describing the "expecting fi" keyword. I have written it down before the done keyword and have still not managed to overcome to error. Can anybody tell me as to how to solve this error and where in my approach I have made the mistake?



 VAL1=0
VAL2=0

while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
do read -p "Enter values for both numbers" VAL1 VAL2
if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
then break
else read -p "Enter value number two" VAL2
if [ "$VAL2" -eq 0 ]
then echo "Cannot divide by zero"
else echo $(( VAL1 / VAL2 ))
fi
done









share|improve this question



















  • 2





    Well you have two ifs and only one fi, so one fi is missing – replace the fi line with: fi;fi

    – dessert
    2 hours ago











  • @dessert wow, i overlooked something so simple. Thanks dessert!

    – Roosevelt Mendieta
    2 hours ago











  • note: you can get a near debug mode with #!/bin/bash -x on the first line of your script OR by starting the script with bash -x ./myscript.sh

    – cmak.fr
    2 hours ago












  • another ex-COBOL coder eh? The fullstop kills all levels of IF doesn't apply to many other langauges (if any) :)

    – guiverc
    1 hour ago















0















Below I have a shell script i wrote and when I try to execute it I get an error in my terminal describing the "expecting fi" keyword. I have written it down before the done keyword and have still not managed to overcome to error. Can anybody tell me as to how to solve this error and where in my approach I have made the mistake?



 VAL1=0
VAL2=0

while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
do read -p "Enter values for both numbers" VAL1 VAL2
if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
then break
else read -p "Enter value number two" VAL2
if [ "$VAL2" -eq 0 ]
then echo "Cannot divide by zero"
else echo $(( VAL1 / VAL2 ))
fi
done









share|improve this question



















  • 2





    Well you have two ifs and only one fi, so one fi is missing – replace the fi line with: fi;fi

    – dessert
    2 hours ago











  • @dessert wow, i overlooked something so simple. Thanks dessert!

    – Roosevelt Mendieta
    2 hours ago











  • note: you can get a near debug mode with #!/bin/bash -x on the first line of your script OR by starting the script with bash -x ./myscript.sh

    – cmak.fr
    2 hours ago












  • another ex-COBOL coder eh? The fullstop kills all levels of IF doesn't apply to many other langauges (if any) :)

    – guiverc
    1 hour ago













0












0








0








Below I have a shell script i wrote and when I try to execute it I get an error in my terminal describing the "expecting fi" keyword. I have written it down before the done keyword and have still not managed to overcome to error. Can anybody tell me as to how to solve this error and where in my approach I have made the mistake?



 VAL1=0
VAL2=0

while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
do read -p "Enter values for both numbers" VAL1 VAL2
if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
then break
else read -p "Enter value number two" VAL2
if [ "$VAL2" -eq 0 ]
then echo "Cannot divide by zero"
else echo $(( VAL1 / VAL2 ))
fi
done









share|improve this question
















Below I have a shell script i wrote and when I try to execute it I get an error in my terminal describing the "expecting fi" keyword. I have written it down before the done keyword and have still not managed to overcome to error. Can anybody tell me as to how to solve this error and where in my approach I have made the mistake?



 VAL1=0
VAL2=0

while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
do read -p "Enter values for both numbers" VAL1 VAL2
if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
then break
else read -p "Enter value number two" VAL2
if [ "$VAL2" -eq 0 ]
then echo "Cannot divide by zero"
else echo $(( VAL1 / VAL2 ))
fi
done






command-line bash scripts gnome-terminal syntax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Pilot6

53.5k15109198




53.5k15109198










asked 2 hours ago









Roosevelt MendietaRoosevelt Mendieta

2014




2014







  • 2





    Well you have two ifs and only one fi, so one fi is missing – replace the fi line with: fi;fi

    – dessert
    2 hours ago











  • @dessert wow, i overlooked something so simple. Thanks dessert!

    – Roosevelt Mendieta
    2 hours ago











  • note: you can get a near debug mode with #!/bin/bash -x on the first line of your script OR by starting the script with bash -x ./myscript.sh

    – cmak.fr
    2 hours ago












  • another ex-COBOL coder eh? The fullstop kills all levels of IF doesn't apply to many other langauges (if any) :)

    – guiverc
    1 hour ago












  • 2





    Well you have two ifs and only one fi, so one fi is missing – replace the fi line with: fi;fi

    – dessert
    2 hours ago











  • @dessert wow, i overlooked something so simple. Thanks dessert!

    – Roosevelt Mendieta
    2 hours ago











  • note: you can get a near debug mode with #!/bin/bash -x on the first line of your script OR by starting the script with bash -x ./myscript.sh

    – cmak.fr
    2 hours ago












  • another ex-COBOL coder eh? The fullstop kills all levels of IF doesn't apply to many other langauges (if any) :)

    – guiverc
    1 hour ago







2




2





Well you have two ifs and only one fi, so one fi is missing – replace the fi line with: fi;fi

– dessert
2 hours ago





Well you have two ifs and only one fi, so one fi is missing – replace the fi line with: fi;fi

– dessert
2 hours ago













@dessert wow, i overlooked something so simple. Thanks dessert!

– Roosevelt Mendieta
2 hours ago





@dessert wow, i overlooked something so simple. Thanks dessert!

– Roosevelt Mendieta
2 hours ago













note: you can get a near debug mode with #!/bin/bash -x on the first line of your script OR by starting the script with bash -x ./myscript.sh

– cmak.fr
2 hours ago






note: you can get a near debug mode with #!/bin/bash -x on the first line of your script OR by starting the script with bash -x ./myscript.sh

– cmak.fr
2 hours ago














another ex-COBOL coder eh? The fullstop kills all levels of IF doesn't apply to many other langauges (if any) :)

– guiverc
1 hour ago





another ex-COBOL coder eh? The fullstop kills all levels of IF doesn't apply to many other langauges (if any) :)

– guiverc
1 hour ago










2 Answers
2






active

oldest

votes


















1














“Expecting fi” means your script is missing this keyword. In your case you have two ifs, but only one fi, leaving the first unclosed. Correct it to:



 else echo $(( VAL1 / VAL2 ))
fi
fi
done


In order to debug shell scripts it’s a good idea to use shellcheck either online or via the command shellcheck (sudo apt install shellcheck):



$ shellcheck myscript

Line 4:
while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
^-- SC1009: The mentioned syntax error was in this while loop.

Line 6:
if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
^-- SC1046: Couldn't find 'fi' for this 'if'.
^-- SC1073: Couldn't parse this if expression. Fix to allow more checks.

Line 13:
done
^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.





share|improve this answer






























    1














    As dessert already pointed out, having two ifs but only one fi immediately tells you that there is a mismatch somewhere in your conditional statement.



    One thing I would suggest is that when coding, it helps to use your indentation to help figure out if you are missing any structure. Keeping your if-then-else-fi within the same column is one trick that can be used to keep track of these things. Same for the case-esac or the do-done for that matter.



    Using your sample code, if we indent in that manner, it's pretty clear with regards to something missing:



    VAL1=0 
    VAL2=0

    while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
    do
    read -p "Enter values for both numbers" VAL1
    if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
    then
    break
    else
    read -p "Enter value number two" VAL2
    if [ "$VAL2" -eq 0 ]
    then
    echo "Cannot divide by zero"
    else
    echo $(( VAL1 / VAL2 ))
    fi
    ?? <------ *Missing fi would likely have been detected here*
    done





    share|improve this answer






















      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1127641%2flinux-shell-script-syntax-error-word-unexpected-expecting-fi%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      “Expecting fi” means your script is missing this keyword. In your case you have two ifs, but only one fi, leaving the first unclosed. Correct it to:



       else echo $(( VAL1 / VAL2 ))
      fi
      fi
      done


      In order to debug shell scripts it’s a good idea to use shellcheck either online or via the command shellcheck (sudo apt install shellcheck):



      $ shellcheck myscript

      Line 4:
      while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
      ^-- SC1009: The mentioned syntax error was in this while loop.

      Line 6:
      if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
      ^-- SC1046: Couldn't find 'fi' for this 'if'.
      ^-- SC1073: Couldn't parse this if expression. Fix to allow more checks.

      Line 13:
      done
      ^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
      ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.





      share|improve this answer



























        1














        “Expecting fi” means your script is missing this keyword. In your case you have two ifs, but only one fi, leaving the first unclosed. Correct it to:



         else echo $(( VAL1 / VAL2 ))
        fi
        fi
        done


        In order to debug shell scripts it’s a good idea to use shellcheck either online or via the command shellcheck (sudo apt install shellcheck):



        $ shellcheck myscript

        Line 4:
        while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
        ^-- SC1009: The mentioned syntax error was in this while loop.

        Line 6:
        if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
        ^-- SC1046: Couldn't find 'fi' for this 'if'.
        ^-- SC1073: Couldn't parse this if expression. Fix to allow more checks.

        Line 13:
        done
        ^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
        ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.





        share|improve this answer

























          1












          1








          1







          “Expecting fi” means your script is missing this keyword. In your case you have two ifs, but only one fi, leaving the first unclosed. Correct it to:



           else echo $(( VAL1 / VAL2 ))
          fi
          fi
          done


          In order to debug shell scripts it’s a good idea to use shellcheck either online or via the command shellcheck (sudo apt install shellcheck):



          $ shellcheck myscript

          Line 4:
          while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
          ^-- SC1009: The mentioned syntax error was in this while loop.

          Line 6:
          if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
          ^-- SC1046: Couldn't find 'fi' for this 'if'.
          ^-- SC1073: Couldn't parse this if expression. Fix to allow more checks.

          Line 13:
          done
          ^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
          ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.





          share|improve this answer













          “Expecting fi” means your script is missing this keyword. In your case you have two ifs, but only one fi, leaving the first unclosed. Correct it to:



           else echo $(( VAL1 / VAL2 ))
          fi
          fi
          done


          In order to debug shell scripts it’s a good idea to use shellcheck either online or via the command shellcheck (sudo apt install shellcheck):



          $ shellcheck myscript

          Line 4:
          while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
          ^-- SC1009: The mentioned syntax error was in this while loop.

          Line 6:
          if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
          ^-- SC1046: Couldn't find 'fi' for this 'if'.
          ^-- SC1073: Couldn't parse this if expression. Fix to allow more checks.

          Line 13:
          done
          ^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
          ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          dessertdessert

          24.7k672105




          24.7k672105























              1














              As dessert already pointed out, having two ifs but only one fi immediately tells you that there is a mismatch somewhere in your conditional statement.



              One thing I would suggest is that when coding, it helps to use your indentation to help figure out if you are missing any structure. Keeping your if-then-else-fi within the same column is one trick that can be used to keep track of these things. Same for the case-esac or the do-done for that matter.



              Using your sample code, if we indent in that manner, it's pretty clear with regards to something missing:



              VAL1=0 
              VAL2=0

              while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
              do
              read -p "Enter values for both numbers" VAL1
              if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
              then
              break
              else
              read -p "Enter value number two" VAL2
              if [ "$VAL2" -eq 0 ]
              then
              echo "Cannot divide by zero"
              else
              echo $(( VAL1 / VAL2 ))
              fi
              ?? <------ *Missing fi would likely have been detected here*
              done





              share|improve this answer



























                1














                As dessert already pointed out, having two ifs but only one fi immediately tells you that there is a mismatch somewhere in your conditional statement.



                One thing I would suggest is that when coding, it helps to use your indentation to help figure out if you are missing any structure. Keeping your if-then-else-fi within the same column is one trick that can be used to keep track of these things. Same for the case-esac or the do-done for that matter.



                Using your sample code, if we indent in that manner, it's pretty clear with regards to something missing:



                VAL1=0 
                VAL2=0

                while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
                do
                read -p "Enter values for both numbers" VAL1
                if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
                then
                break
                else
                read -p "Enter value number two" VAL2
                if [ "$VAL2" -eq 0 ]
                then
                echo "Cannot divide by zero"
                else
                echo $(( VAL1 / VAL2 ))
                fi
                ?? <------ *Missing fi would likely have been detected here*
                done





                share|improve this answer

























                  1












                  1








                  1







                  As dessert already pointed out, having two ifs but only one fi immediately tells you that there is a mismatch somewhere in your conditional statement.



                  One thing I would suggest is that when coding, it helps to use your indentation to help figure out if you are missing any structure. Keeping your if-then-else-fi within the same column is one trick that can be used to keep track of these things. Same for the case-esac or the do-done for that matter.



                  Using your sample code, if we indent in that manner, it's pretty clear with regards to something missing:



                  VAL1=0 
                  VAL2=0

                  while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
                  do
                  read -p "Enter values for both numbers" VAL1
                  if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
                  then
                  break
                  else
                  read -p "Enter value number two" VAL2
                  if [ "$VAL2" -eq 0 ]
                  then
                  echo "Cannot divide by zero"
                  else
                  echo $(( VAL1 / VAL2 ))
                  fi
                  ?? <------ *Missing fi would likely have been detected here*
                  done





                  share|improve this answer













                  As dessert already pointed out, having two ifs but only one fi immediately tells you that there is a mismatch somewhere in your conditional statement.



                  One thing I would suggest is that when coding, it helps to use your indentation to help figure out if you are missing any structure. Keeping your if-then-else-fi within the same column is one trick that can be used to keep track of these things. Same for the case-esac or the do-done for that matter.



                  Using your sample code, if we indent in that manner, it's pretty clear with regards to something missing:



                  VAL1=0 
                  VAL2=0

                  while [ "$VAL1" -eq 0 ] && [ "$VAL2" -eq 0 ]
                  do
                  read -p "Enter values for both numbers" VAL1
                  if [ "$VAL1" -eq 99 ] || [ "$VAL1" -eq 99 ]
                  then
                  break
                  else
                  read -p "Enter value number two" VAL2
                  if [ "$VAL2" -eq 0 ]
                  then
                  echo "Cannot divide by zero"
                  else
                  echo $(( VAL1 / VAL2 ))
                  fi
                  ?? <------ *Missing fi would likely have been detected here*
                  done






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  S. NixonS. Nixon

                  233




                  233



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1127641%2flinux-shell-script-syntax-error-word-unexpected-expecting-fi%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Are there any comparative studies done between Ashtavakra Gita and Buddhim?How is it wrong to believe that a self exists, or that it doesn't?Can you criticise or improve Ven. Bodhi's description of MahayanaWas the doctrine of 'Anatta', accepted as doctrine by modern Buddhism, actually taught by the Buddha?Relationship between Buddhism, Hinduism and Yoga?Comparison of Nirvana, Tao and Brahman/AtmaIs there a distinction between “ego identity” and “craving/hating”?Are there many differences between Taoism and Buddhism?Loss of “faith” in buddhismSimilarity between creation in Abrahamic religions and beginning of life in Earth mentioned Agganna Sutta?Are there studies about the difference between meditating in the morning versus in the evening?Can one follow Hinduism and Buddhism at the same time?Are there any prohibitions on participating in other religion's practices?Psychology of 'flow'

                      fallocate: fallocate failed: Text file busy in Ubuntu 17.04? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)defragmenting and increasing performance of old lubuntu system with swap partitionIssue with increasing the root partition from the swapthis /usr/bin/dpkg returned error || ubuntu-16.04, 64bitDefault 17.04 swap file locationHow to Resize Ubuntu 17.04 Zesty Swap file size?Ubuntu freezes from online formsMy Laptop is not starting after upgrade ubuntu 16.04 (Kernel 4.8.0-38 to 04.10.0-36)hcp: ERROR: FALLOCATE FAILED!Not sure my swap is being usedWine 3.0 asking for more virtual free swap

                      Where else does the Shulchan Aruch quote an authority by name?Parashat Metzora+HagadolPesach/PassoverShulchan Aruch UTF-8Anonymous glosses in the Shulchan AruchWhy is the Shulchan Aruch definitive?Siman 32, Kitzur Shulchan Aruch: UntranslatedLitvaks/Yeshivish and Shulchan AruchBuying a Shulchan AruchEnglish version of SHULCHAN ARUCHIs there any place where Shulchan Aruch rules with the Rosh against the Rif and Rambam?Are there practices where Sepharadim do not hold by Shulchan Aruch?5th part of the shulchan aruch