Closed leetrout closed 1 year ago
What did you run exactly?. It's likely that you're trying to parse in POSIX mode when this is bash syntax.
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.
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
@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.
The parser should now give a better error message.
Given this code:
I am getting this error:
I am unsure how to fix this