Skip to content

"}" can only be used to close a block #993

Closed

Description

@leetrout

Given this code:

function help_msg {
	echo "Wrong arguments" >&2
	echo "Usage:"
	echo "  $0 -s source -d destination"
	echo "Options:"
	echo "  -s  Source vault"
	echo "  -d  Destination vault"
}

I am getting this error:

file.sh:10:1: "}" can only be used to close a block

I am unsure how to fix this

Activity

  1. mvdan commented on Apr 22, 2023

    @mvdan
    Owner

    What did you run exactly?. It's likely that you're trying to parse in POSIX mode when this is bash syntax.

  2. leetrout commented on Apr 22, 2023

    @leetrout
    Author

    I just ran shfmt <file>

    Will respond with additional details on Monday when I am back at work but I did figure out to change it to not use the function keyword yesterday and it worked.

    I am inheriting some shell scripts I didn't write - thank you for the hint to look at the mode.

  3. rgowtham commented on Jun 22, 2023

    @rgowtham

    I am seeing a similar behavior with the below function as well,

    function prettyTime {
        local _dt=$1
        local _dm
        local _dh
        local _ds
        _dh=$((_dt / 3600))
        _dm=$(((_dt / 60) % 60))
        _ds=$((_dt % 60))
        printf '%d:%02d:%02d' ${_dh} ${_dm} ${_ds}
    }
    ~ shfmt run.sh
    run.sh:41:1: "}" can only be used to close a block

    I am using shfmt 3.7.0, not sure why shfmt considers this a bug

  4. mvdan commented on Jun 23, 2023

    @mvdan
    Owner

    @rgowtham as I tried to explain in my previous comment, this is not a bug in shfmt - the parser will work if you properly parse the script as Bash. You can do that explicitly via the -ln=bash flag, or by naming the script foo.bash, or by adding a shebang like #!/usr/bin/bash. Instead of any of those three options, your script probably just has a name like foo.sh, so the parser assumes POSIX shell, where function is not a valid way to declare functions.

  5. mvdan commented on Jul 13, 2023

    @mvdan
    Owner

    The parser should now give a better error message.

  6. added a commit that references this issue on Jul 13, 2023
    25abe06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址