与 Bash 的 `true` 命令等效的 Windows 命令外壳是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22046780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:43:09  来源:igfitidea点击:

What's the Windows command shell equivalent of Bash's `true` command?

gitbashshellcmd

提问by Andy Stewart

I have written a Vim plugin which shells out to run external commands. Two of the commands I run are diffand grepwhich can each exit with a non-zero exit code during "normal" operation.

我写了一个 Vim 插件,它可以运行外部命令。我运行的两个命令是diffgrep在“正常”操作期间,每个命令都可以以非零退出代码退出。

(diffexits with exit code 1 when it finds differences and grepexits with exit code 1 when it doesn't find matches.)

diff当发现差异时以退出代码 1grep退出,当未找到匹配时以退出代码 1 退出。)

For the purposes of my Vimscript I need to return an exit code of 0 from these commands. So far I'm constructing the commands like this:

为了我的 Vimscript,我需要从这些命令返回一个退出代码 0。到目前为止,我正在构建这样的命令:

diff a b || true

And:

和:

grep foo bar || true

This works for me on OS X and it apparently works for some Windows users. However, when I run Windows 7 on OS X via VirtualBox, using the Bash installed by the Git installer, I get the error message:

这在 OS X 上对我有用,并且显然对某些 Windows 用户有用。但是,当我通过 VirtualBox 在 OS X 上运行 Windows 7 时,使用 Git 安装程序安装的 Bash,我收到错误消息:

'true' is not recognized as an internal or external command, operable program or batch file.

'true' 不是内部或外部命令,也不是可运行的程序或批处理文件。

What is the correct successful-no-op command to use on Windows?

在 Windows 上使用的正确的 success-no-op 命令是什么?

采纳答案by chepner

trueis roughly equivalent to (exit 0)(the parentheses create a subshell that exits with status 0, instead of exiting your current shell.

true大致相当于(exit 0)(括号创建了一个以状态 0 退出的子 shell,而不是退出当前的 shell。

回答by user368683

VER>NUL

works for me.

对我来说有效。

For example,

例如,

MKDIR . || VER>NUL

issues an error message, but it sets %ERRORLEVEL%to 0.

发出错误消息,但它设置%ERRORLEVEL%为 0。

回答by VonC

The context is a Windows cmdshell (used by the git-cmd.batscript):

上下文是一个 Windows cmdshell(由git-cmd.bat脚本使用):

Following "Exiting batch with EXIT /B Xwhere X>=1acts as if command completed successfully when using &&or ||operators between batch calls", you could define in your path a true.batfile with:

遵循“在批处理调用之间使用操作符时,使用where退出批处理就像命令成功完成一样EXIT /B XX>=1&&||”,您可以在路径中定义一个true.bat文件:

@%COMSPEC% /C exit 1 >nul

回答by Peter Zagubisalo

cd .

also sets %ERRORLEVEL%to 0 but runs a bit faster and writes a bit shorter than ver>nul. Example:

也设置%ERRORLEVEL%为 0 但运行速度比ver>nul. 例子:

mkdir . 2>nul || cd .