daniel-sc / bash-shell-to-bat-converter

Converter for bash shell scripts to windows batch files.
https://daniel-sc.github.io/bash-shell-to-bat-converter
MIT License
229 stars 45 forks source link

`local` and `global` variables #63

Open shadichy opened 1 year ago

daniel-sc commented 1 year ago

@shadichy could you provide some example input together with the expected output?

shadichy commented 1 year ago

@daniel-sc

like

someFunc() {

  aGlobalVar=1

  local aLocalVar=1

}

should returns something like

:someFunc

set  aGlobalVar=1

setlocal
set aLocalVar=1
endlocal

exit /b 0
daniel-sc commented 1 year ago

@shadichy I see some problem with this approach (wrapping individual 'local' assignments with 'setlocal'/'endloacal'): When the variable is used in subsequent lines the value is gone - e.g. the following script

ECHO OFF

setlocal
set aLocalVar="hello"
echo "inside first setlocal: %aLocalVar%"
endlocal

echo "outside setlocal: %aLocalVar%"

setlocal
echo "inside second setlocal: %aLocalVar%"
endlocal

has the output

"inside first setlocal: "hello""
"outside setlocal: "
"inside second setlocal: "

And since 'local' and 'global' variables could occur in any order this is a problem - or did I miss something?

shadichy commented 1 year ago

@daniel-sc that's how 'local' variables work :) I think that when using local variables, any command that uses those vars should be wrapped inside setlocal and endlocal, and if any local var is returned globally, use something like this e.g:

endlocal & (
  set "GlobalVar1=%LocalVar1%"
  set "GlobalVar2=%LocalVar2%"
  set "GlobalVar3=%LocalVar3%"
)
daniel-sc commented 1 year ago

Yeah I understand. The point is, that a mere syntactically approach (as currently used) does not suffice to solve this. One would need to analyze all variables and their usages..

shadichy commented 1 year ago

seems hard Seems Microsoft has done really great in confusing their customers :)