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

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

    The following works for me without any errors or error messages with Bash 4 and Windows 10, unlike the answers above. I name the file "whatever.cmd", do chmod +x to make it executable in linux, and make it have unix line endings (dos2unix) to keep bash quiet.

    :; if [ -z 0 ]; then
      @echo off
      goto :WINDOWS
    fi
    
    if [ -z "$2" ]; then
      echo "usage: $0  "
      exit 1
    fi
    
    # bash stuff
    exit
    
    :WINDOWS
    if [%2]==[] (
      SETLOCAL enabledelayedexpansion
      set usage="usage: %0  "
      @echo !usage:"=!
      exit /b 1
    )
    
    :: windows stuff
    

提交回复
热议问题