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

后端 未结 11 1903
盖世英雄少女心
盖世英雄少女心 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:49

    I needed this for some of my Python package install scripts. Most things between sh and bat file are same but few things like error handling are different. One way to do this is as follows:

    common.inc
    ----------
    common statement1
    common statement2
    

    Then you call this from bash script:

    linux.sh
    --------
    # do linux specific stuff
    ...
    # call common code
    source common.inc
    

    Windows batch file looks like this:

    windows.bat
    -----------
    REM do windows specific things
    ...
    # call common code
    call common.inc
    

提交回复
热议问题