首页 > 解决方案 > 为什么我在 cmd 中的 1 行命令不起作用?

问题描述

bin如果文件夹不存在,我有这个命令可以自动创建一个文件夹:

if not exist bin mkdir bin

我有另一个命令来编译 C 文件并将其放入bin文件夹中:

gcc hello_world.c -o bin/hello_world.exe

当我像这样将命令连接在一起时:

if not exist bin mkdir bin && gcc hello_world.c -o bin/hello_world.exe

如果该bin文件夹不存在,它可以正常工作。但是当 bin 文件夹已经存在时,第二个命令不会运行。

为什么以及如何发生这种情况?

编辑:我已经尝试过只使用 1&但它仍然是相同的结果。

标签: shellgcccmdterminaldirectory

解决方案


C:\WINDOWS\system32>(if not exist bin mkdir bin) & echo %errorlevel%
1

C:\WINDOWS\system32>(if not exist bin mkdir bin) & echo %errorlevel%
0

&&表示仅当为 0 时才运行下一条命令。%errorlevel%更改为&.

https://winsourcecode.blogspot.com/2019/12/command-prompt-cheat-sheet.html


推荐阅读