Single script to run in both Windows batch and Linux Bash?

后端 未结 11 1912
盖世英雄少女心
盖世英雄少女心 2020-11-28 19:04

Is it possible to write a single script file which executes in both Windows (treated as .bat) and Linux (via Bash)?

I know the basic syntax of both, but didn\'t figu

11条回答
  •  心在旅途
    2020-11-28 19:34

    you can try this:

    #|| goto :batch_part
     echo $PATH
    #exiting the bash part
    exit
    :batch_part
     echo %PATH%
    

    Probably you'll need to use /r/n as a new line instead of a unix style.If I remember correct the unix new line is not interpreted as a new line by .bat scripts.Another way is to create an #.exe file in the path that does do nothing in similar manner as my answer here: Is it possible to embed and execute VBScript within a batch file without using a temporary file?

    EDIT

    The binki's answer is almost perfect but still can be improved:

    :<

    It uses again the : trick and the multi line comment.Looks like cmd.exe (at least on windows10) works without problems with the unix style EOLs so be sure that your script is converted into linux format. (same approach has been seen used before here and here ) . Though using shebang still will produce redundant output...

提交回复
热议问题