Windows下Git Bash调用CMD运行ESP-IDF脚本及编译命令失败求助
问题:Git Bash中无法正确启动ESP-IDF的CMD环境并执行构建命令
问题背景
需要在Git Bash中模拟以下快捷方式的行为,并追加构建命令:
- 目标类型:应用程序
- 目标位置:system32
- 目标:
C:\windows\system32\cmd.exe /k ""C:\Users\thebi\.espressif\idf_cmd_init.bat"" esp-idf-f4096c5fb8232f5ffa980cf1b514d882 - 已指定启动位置
正常双击该快捷方式时,CMD窗口会在指定启动位置打开,运行idf_cmd_init.bat完成对应ESP-IDF版本的环境变量配置,最终可执行idf.py build。现在需要将「打开CMD窗口→执行idf.py build→关闭窗口」的流程打包成Shell脚本,但多次尝试均失败。
尝试的代码
# Define the path to the shortcut target command esp_idf_v5_1_init='C:\Users\thebi\.espressif\idf_cmd_init.bat esp-idf-f4096c5fb8232f5ffa980cf1b514d882' # Run the shortcut target command using cmd.exe cmd.exe /k "$esp_idf_v5_1_init" && ( # Run additional commands in the resulting cmd window cmd.exe /k "cd %ESP32_REPO_PROD% && esp-idf.py build" )
实际现象:CMD窗口打开后无任何操作。已尝试改用powershell、替换/c//k参数、将CMD命令写入单独文件等方法,均未解决问题。
更新后的测试结果
按建议调整后运行脚本,输出如下:
$ ./test.sh Microsoft Windows [Version 10.0.19045.4291] (c) Microsoft Corporation. All rights reserved. D:\...\temp>call C:\Users\thebi\.espressif\idf_cmd_init.bat esp-idf-f4096c5fb8232f5ffa980cf1b514d882 Setting PYTHONNOUSERSITE, was not set Using Python in C:\Users\thebi\.espressif\python_env\idf5.1_py3.11_env\Scripts\ Python 3.11.2 Using Git in C:\Users\thebi\.espressif\tools\idf-git\2.39.2\cmd\ git version 2.39.2.windows.1 This .bat file is for Windows CMD.EXE shell only.
对比正常打开ESP-IDF CMD窗口的输出,后者会完成环境变量设置、工具路径添加、依赖检查等步骤,最终提示可执行idf.py build。查看idf_cmd_init.bat发现,脚本已执行到最后一行:
:: Check if this is a recent enough copy of ESP-IDF. :: If so, use export.bat provided there. :: Note: no "call", will not return into this batch file. if exist "%IDF_PATH%\export.bat" %IDF_PATH%\export.bat
问题根源在于export.bat输出「仅适用于Windows CMD.EXE shell」的提示,导致环境初始化流程中断,求解决方法。
内容的提问来源于stack exchange,提问作者monkey
相关产品推荐
相关产品推荐

