一个脚本在两个批次的Windows和Linux的Bash运行? [英] Single script to run in both Windows batch and Linux Bash?

查看:117
本文介绍了一个脚本在两个批次的Windows和Linux的Bash运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能写一个在执行了Windows(如.BAT处理)和Linux(通过击)?一个脚本文件

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

我知道两者的基本语法,但没有搞清楚。它可能可以利用一些猛砸晦涩的语法或某些Windows批处理器故障。

I know the basic syntax of both, but didn't figure out. It could probably exploit some Bash's obscure syntax or some Windows batch processor glitch.

要执行的命令可能只是一行来执行其他脚本。

The command to execute may be just a single line to execute other script.

的动机是有只是一个单一的应用程序的启动命令为Windows和Linux。

The motivation is to have just a single application boot command for both Windows and Linux.

更新:需要进行系统的原生的shell脚本是需要选择正确的跨preTER版本,符合一定知名度的环境变量等安装其他环境中,如CygWin的不是preferable - 我想保持这个概念。下载和放大器;运行

Update: The need for system's "native" shell script is that it needs to pick the right interpreter version, conform to certain well-known environment variables etc. Installing additional environments like CygWin is not preferable - I'd like to keep the concept "download & run".

唯一的其他语言来考虑Windows是Windows脚本宿主 - WSH,默认情况下是preSET自98

The only other language to consider for Windows is Windows Scripting Host - WSH, which is preset by default since 98.

推荐答案

我所做的是使用CMD的标签语法注释标记。标签字符,冒号(),相当于真正在大多数POSIXish炮弹。如果您立即不能在使用其它字符按照标签字符 GOTO ,那么您的评论 CMD 脚本应该不会影响你的 CMD code。

What I have done is use cmd’s label syntax as comment marker. The label character, a colon (:), is equivalent to true in most POSIXish shells. If you immediately follow the label character by another character which can’t be used in a GOTO, then commenting your cmd script should not affect your cmd code.

该黑客是把code行的字符序列之后。如果你正在写大多是单行脚本,或如可能的话,可以写 SH 的一个行 CMD的,下面可能会被罚款。不要忘记,任何使用 $ 的一定是你的下一个冒号前,因为 :?重置 $ 0

The hack is to put lines of code after the character sequence ":;". If you’re writing mostly one-liner scripts or, as may be the case, can write one line of sh for many lines of cmd, the following might be fine. Don’t forget that any use of $? must be before your next colon : because : resets $? to 0.

:; echo "Hi, I’m ${SHELL}."; exit $?
@ECHO OFF
ECHO I'm %COMSPEC%

守着 $ 的一个很做作?例如:

:; false; ret=$?
:; [ ${ret} = 0 ] || { echo "Program failed with code ${ret}." >&2; exit 1; }
:; exit
ECHO CMD code.

有关跳过 CMD code另一个想法是使用<一个href=\"http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_07_04\">heredocs让 SH 对待 CMD code作为一个未使用的字符串和 CMD 间$ p $点吧。在这种情况下,我们确保我们的定界符的分隔符都引用(停止 SH 从运行时做任何形式对其内容间pretation的<$ C CMD 跳过它像任何:$ C> SH )和启动开始与其他行

Another idea for skipping over cmd code is to use heredocs so that sh treats the cmd code as an unused string and cmd interprets it. In this case, we make sure that our heredoc’s delimiter is both quoted (to stop sh from doing any sort of interpretation on its contents when running with sh) and starts with : so that cmd skips over it like any other line starting with :.

:; echo "I am ${SHELL}"
:<<"::CMDLITERAL"
ECHO I am %COMSPEC%
::CMDLITERAL
:; echo "And ${SHELL} is back!"
:; exit
ECHO And back to %COMSPEC%

根据您的需要或编码风格,交错 CMD SH code可能会或可能不会让感。使用here文档是执行这样的隔行扫描的一种方法。这可能,但是,可以与 GOTO 技术扩展:

Depending on your needs or coding style, interlacing cmd and sh code may or may not make sense. Using heredocs is one method to perform such interlacing. This could, however, be extended with the GOTO technique:

:<<"::CMDLITERAL"
@ECHO OFF
GOTO :CMDSCRIPT
::CMDLITERAL

echo "I can write free-form ${SHELL} now!"
if :; then
  echo "This makes conditional constructs so much easier because"
  echo "they can now span multiple lines."
fi
exit $?

:CMDSCRIPT
ECHO Welcome to %COMSPEC%

;:#或

环球评论,当然,可以用字符序列完成。空间或分号是必要的,因为 SH 认为是一个命令名称的一部分,如果它不是第一个字符的标识符。例如,您可能需要使用 GOTO 方法来分割你的code之前写在你的文件的第一行的普遍意见。然后,你可以通知你的,为什么你的脚本是这么写的奇怪的读者:

Universal comments, of course, can be done with the character sequence : # or :;#. The space or semicolon are necessary because sh considers # to be part of a command name if it is not the first character of an identifier. For example, you might want to write universal comments in the first lines of your file before using the GOTO method to split your code. Then you can inform your reader of why your script is written so oddly:

: # This is a special script which intermixes both sh
: # and cmd code. It is written this way because it is
: # used in system() shell-outs directly in otherwise
: # portable code. See http://stackoverflow.com/questions/17510688
: # for details.
:; echo "This is ${SHELL}"; exit
@ECHO OFF
ECHO This is %COMSPEC%

因此​​,一些思想和方法来完成 SH CMD 无严重副作用兼容脚本尽可能我知道,(而无需 CMD 输出'#'不被识别为一个内部或外部命令,可操作的程序或批处理文件。)。

Thus, some ideas and ways to accomplish sh and cmd-compatible scripts without serious side effects as far as I know (and without having cmd output '#' is not recognized as an internal or external command, operable program or batch file.).

这篇关于一个脚本在两个批次的Windows和Linux的Bash运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆