与 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
What's the Windows command shell equivalent of Bash's `true` command?
提问by Andy Stewart
I have written a Vim plugin which shells out to run external commands. Two of the commands I run are diff
and grep
which can each exit with a non-zero exit code during "normal" operation.
我写了一个 Vim 插件,它可以运行外部命令。我运行的两个命令是diff
和grep
在“正常”操作期间,每个命令都可以以非零退出代码退出。
(diff
exits with exit code 1 when it finds differences and grep
exits 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
true
is 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 cmd
shell (used by the git-cmd.bat
script):
上下文是一个 Windows cmd
shell(由git-cmd.bat
脚本使用):
Following "Exiting batch with EXIT /B X
where X>=1
acts as if command completed successfully when using &&
or ||
operators between batch calls", you could define in your path a true.bat
file with:
遵循“在批处理调用之间使用或操作符时,使用where退出批处理就像命令成功完成一样EXIT /B X
X>=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 .