首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在shell脚本中执行的批处理文件不等待用户输入

在shell脚本中执行的批处理文件不等待用户输入
EN

Stack Overflow用户
提问于 2021-03-10 21:13:36
回答 2查看 368关注 0票数 0

我无意中发现了git-钩子,并试图创建一个在Windows上运行的钩子。因此,我决定使用批处理文件这样做,因为这似乎很容易。

首先,我将预提交示例重命名为预提交,并在那里调用bat步骤:

代码语言:javascript
复制
#!/bin/sh
$(pwd)/git-hooks/hooks/unit_test.bat &&
$(pwd)/git-hooks/hooks/integration_test.bat

unit_test.bat只显示一些消息并运行单元测试任务,但是integration_test.bat提示用户是否想运行这些测试,因为它们通常比较慢。

问题是提示符(使用'choice‘或'set /p')没有得到用户输入

input

  • 'choice‘
  • 'set /p’不等待用户
  • 冻结,也不允许任何用户输入

我尝试添加start来调用.bat文件,但是它会在另一个cmd上打开它们,这样就不可能停止提交。

引用的文件

  • unit_test.bat

@echo off echo ^>睾丸单元ários调用gradlew testReport分部( echo (& echo Testes unitários falharam!( Acesse o relatório de & exit 1)

  • integration_test.bat

@echo off echo(echo ^>睾丸整数%SystemRoot%\System32\choice.exe /C sn /M "Esses testes geralmente s o mais lentos. Quer rodar os Testes integrados“)如果ERRORLEVEL 2 goto nao调用gradlew /M(回声(回波睾丸)Acesse o relatório de testes para conferir(& exit 1) :nao echo(echo N o se esque a de confirmar que os testes integrados antes de fazer o 'git push!)出口/B

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-11 21:29:46

冻结错误是因为git-钩子禁用了交互性,正如@AnthonySotille所指出的.

通过将提示符提取到另一个.bat文件并使用start /wait调用它们,我绕过了这种情况。这将打开另一个cmd,运行提示符,并使用成功代码退出,将Y和失败代码表示为N。这似乎不是一个好的实践,但就目前而言,它完成了工作。

这个问题可能被认为是How do I prompt the user from within a commit-msg hook?的重复。

票数 0
EN

Stack Overflow用户

发布于 2021-03-10 23:20:38

我认为gradlew实际上是另一个批处理文件,gradlew.bat,所以我假设,如果您希望它在完成后返回到脚本,那么应该使用Call命令。另外,您应该注意到,&在一行上连接两个单独的命令,而&&只在前一个命令成功时才运行一个命令。在您的示例中,Echo命令不能失败,因此只需要&即可。此外,如果错误代码是1或更高,这意味着,在您发布的代码中,代码将始终是If ErrorLevel 1。您应该使用的是If Not ErrorLevel 2If %ErrorLevel% Equ 1If "%ErrorLevel%" == "1"

示例:(请插入gradlew.bat的完整路径,而不是依赖容易损坏或受影响的%Path%变量,如果路径和可执行名称包含空格或有问题的字符,请加倍引用路径和可执行名称)。我已经删除了低级示例中不必要的级联,因为它在脚本中并不是必需的。

unit_test.bat

代码语言:javascript
复制
@Echo Off
Echo ^> Testes unitários
Call gradlew.bat testReport || (Echo(& Echo Testes unitários falharam! Acesse o relatório de testes para conferir.& Exit 1)

integration_test.bat

代码语言:javascript
复制
@Echo Off
Echo(
Echo ^> Testes integrados
%SystemRoot%\System32\choice.exe /C sn /M "Esses testes geralmente são mais lentos. Quer rodar os testes integrados"
If Not ErrorLevel 2 GoTo sim
Echo(
Echo Não se esqueça de confirmar que os testes integrados passam antes de fazer o 'git push'!
Exit /B

:sim
Call gradlew.bat integrationTests || (
    Echo(
    Echo Testes integrados falharam! Acesse o relatório de testes para conferir.
)
Exit 1

或者:

代码语言:javascript
复制
@Echo Off
Echo(
Echo ^> Testes integrados
%SystemRoot%\System32\choice.exe /C sn /M "Esses testes geralmente são mais lentos. Quer rodar os testes integrados"
If ErrorLevel 2 GoTo nao
Call gradlew.bat integrationTests || (
    Echo(
    Echo Testes integrados falharam! Acesse o relatório de testes para conferir.
)
Exit 1

:nao
Echo(
Echo Não se esqueça de confirmar que os testes integrados passam antes de fazer o 'git push'!
Exit /B

我还建议,根据您正在使用的一些字符,确保使用适当的代码页(可能是1252)运行脚本。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66572943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档