I tried the commandline basex both manually and with your script, it works fine there no matter how often I try, not sure why...
Good to know! Once we have a script that fails on one of your systems,it will be easy to get your use case reproduced on other systems.