[Solved] The syntax error for sh function

Discussion in 'Linux' started by quywz, Jul 7, 2018.

  1. quywz

    quywz MDL Member

    Jun 30, 2017
    118
    7
    10
    #1 quywz, Jul 7, 2018
    Last edited: Jul 9, 2018
    Whatever using "gedit" command or "cat" command to edit sh function,the result is always syntax error.
    os:ubuntu 18.04 lts
    Code:
    #!/bin/sh
    function fun1()
    {
     echo --------
    }
    fun1
    
    ./xx.sh:2:syntax error: "(" unexpected

    Code:
    #!/bin/sh
    fun1()
    {
     echo --------
    }
    fun1
    
    ./xx.sh:4:syntax error: "}" unexpected
    syntax.png

    ================================================================================

    I known what is problem.
    it is /bin/sh -> dash,not /bin/sh -> bash
     
  2. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    4,071
    4,651
    150
    Does it need a return value?

    maybe
    Code:
    #!/bin/sh
    void fun1()
    {
    }
    
    Just a guess from C and C++.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Tito

    Tito Super Mod / Adviser
    Staff Member

    Nov 30, 2009
    18,681
    18,589
    340
  4. Tito

    Tito Super Mod / Adviser
    Staff Member

    Nov 30, 2009
    18,681
    18,589
    340
  5. Hadron-Curious

    Hadron-Curious MDL Guru

    Jul 4, 2014
    3,730
    603
    120
    From the little knowledge I have about syntaxes regarding executing such script it might be the way you are running it that is having the issue. As long as you have the Shebang on the first line you are not supposed to encounter that error message. Try to execute the script using ./ followed by the filename and extension.
     
  6. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    4,071
    4,651
    150
    @quywz: I just tried your code in the bash editor that Tito provided.

    It works fine. :g:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. quywz

    quywz MDL Member

    Jun 30, 2017
    118
    7
    10
    syntax.png
     
  8. quywz

    quywz MDL Member

    Jun 30, 2017
    118
    7
    10
    It is most peculiar.
    syntax.png
     
  9. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    4,071
    4,651
    150
    It's possible that the cat command might be putting something extra in that's upsetting the scripter.

    Try dumping the file to the console and see what's in the file.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. quywz

    quywz MDL Member

    Jun 30, 2017
    118
    7
    10
    I known what is problem.
    it is /bin/sh -> dash,not /bin/sh -> bash
     
  11. quywz

    quywz MDL Member

    Jun 30, 2017
    118
    7
    10
  12. Hadron-Curious

    Hadron-Curious MDL Guru

    Jul 4, 2014
    3,730
    603
    120
    Why is the space between your shebang and the slash there in the script?
     
  13. quywz

    quywz MDL Member

    Jun 30, 2017
    118
    7
    10
    en........
    may be ,more press the space.