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

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

    I wanted to comment, but can only add an answer at the moment.

    The techniques given are excellent and I use them also.

    It is hard to retain a file which has two kinds of line breaks contained within it, that being /n for the bash part and /r/n for the windows part. Most editors try and enforce a common line break scheme by guessing what kind of file you are editing. Also most methods of transferring the file across the internet (particularly as a text or script file) will launder the line breaks, so you could start with one kind of line break and end up with the other. If you made assumptions about line breaks and then gave your script to someone else to use they might find it doesn't work for them.

    The other problem is network mounted file systems (or CDs) that are shared between different system types (particularly where you can't control the software available to the user).

    One should therefore use the DOS line break of /r/n and also protect the bash script from the DOS /r by putting a comment at the end of each line (#). You also cannot use line continuations in bash because the /r will cause them to break.

    In this way whoever uses the script, and in whatever environment, it will then work.

    I use this method in conjunction with making portable Makefiles!

提交回复
热议问题