每当满足if条件时,我们就需要传递ctrl + c来停止当前项目并在shell脚本中启动下一个项目? [英] when ever a if condition satisfied then we need to pass ctrl+c to stop current item and start the next one in shell script?

查看:84
本文介绍了每当满足if条件时,我们就需要传递ctrl + c来停止当前项目并在shell脚本中启动下一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我要面对的真实情况

Real scenario which I am facing is below,

  1. 我有一个bat文件,用于根据最近修改的时间监视最近更新的文件,它将告诉用户文件是否在最近1分钟内未更新.

  1. I am having a bat file to monitor the last updated file based on last modified time and it will tell the user if the file not getting updated with in last 1 min.

2.但是我的情况是,我们正在使用最后修改的bat文件来监视日志文件更新.如果日志文件在最近1分钟内未更新,则脚本必须通过脚本本身发送Ctrl + C来停止当前测试.

2.But my scenario is ,we are using the last modified bat file to monitor the log file update. If the log file not updated in the last 1 min then the script has to send Ctrl + C thru script itself to stop the current test.

当前,如果在1分钟内未更新文件,则手动执行Ctrl + C,然后系统会询问我们是否需要退出?我们会说不,那么它将继续下一个测试套件.

Currently when the file not updated with in 1 min then manually we will do Ctrl+C and then the system will ask us like need to exit ? we will say no then it will continue the next test suite.

当前,我正在使用脚本来监视基于上次修改的日期和时间的日志文件更新.但是我需要自动化我在3中提供的步骤.

Currently I am having script to monitor the log file update based on last modified date and time. But i need to automate the steps which i provided in 3.

要检查最后修改的文件,请参见以下内容:

To check last modified file pls refer below:

复制以下内容,并将其另存为.bat文件,并将其命名为checkfolder.

copy the below and save it as .bat file with name as checkfolder.

@echo off
:: Wmic removes regional differences
:: XP Pro can have some filename errors due to the short filename bug

setlocal
:: set the number of minutes in the following line
set minutes=1

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "stamp=%YYYY% %MM% %DD% %HH% %Min%"
call :DateToMinutes %stamp% NowMins

set "name="
for /f "delims=" %%a in ('dir * /a-d /b /od ^|find /v /i "%~nx0"') do set "name=%%a"
if defined name call :CheckMins "%name%"
if %MinsOld% gtr %minutes% echo Folder hasn't been modified in %minutes% minutes & pause

goto :EOF

:CheckMins
set "filestamp="
set "filemins="
set "MinsOld="
set "YY=" & set "YYYY=" & set "MM=" & set "DD="
set "HH=" & set "Min=" & set "Sec=" & set "dt="
set "file=%~sf1"
:: can use CreationDate instead of lastmodified
WMIC DATAFILE WHERE name="%file:\=\\%" get lastmodified | find "." >test.txt
for /f %%a in (rh5.log) do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "filestamp=%YYYY% %MM% %DD% %HH% %Min%"
del test.txt 2>nul
if not defined yyyy goto :EOF

call :DateToMinutes %filestamp% FileMins
set /a MinsOld=%NowMins%-%FileMins%
goto :EOF

:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%&goto :EOF

复制以下内容并将其另存为filechecker .bat

copy the below and save it as filechecker .bat

@echo off
:loop
call "checkfolder.bat"
timeout /t 60 /nobreak
goto :loop

然后创建名为test.txt的txt文件.将这三个文件夹都放在同一个文件夹中,然后单击checkfolder.bat进行验证.

Then create txt file with the name test.txt. have all the three in same folder and click on checkfolder.bat to verify.

我的问题是

在这里,我得到的响应是未修改的文件,之后我需要通过脚本传递ctrl + c,并且我必须将值传递为'n'并按Enter.

Here i got the response as file not modified after that i need to pass ctrl+c thru script and the i have to pass the value as 'n' and hit enter.

如果(文件未更新) ctrl + c,然后提供n作为输入以启动下一个套件

if( file not updated) ctrl+c and then provide n as input to start the next suite

请帮助我.

推荐答案

请参见

此外,您可以使用批处理和vb混合将密钥发送到所需的任何窗口...在此示例中,密钥被发送到当前的cmd窗口.您可能需要调整时间以避免在Ctrl+Cs

Also, you may use a batch and vb hybrid to send keys to whatever window you want... In this sample, keys are sent to current cmd window. You may need to adjust timings to avoid something being echoed between Ctrl+C and s

@echo off

for /L %%a in (1,1,100) do (
  echo(Test waiting for Ctrl+C, loop %%a
  ping 1.1.1.1 -w 500 -n 1 > NUL
  if %%a equ 6 call :sendCTRLC_WAIT
)

echo(This is never reached.
exit/B


rem wait for a second until ctrl-c is sent to this cmd session
:sendCTRLC_WAIT
SetLocal
set "_file1_=%TEMP%\after.vbs"
set "_file2_=%TEMP%\ctrlc.vbs"
>"%_file1_%" (
  echo(wScript.Sleep 1300
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "s"
  echo(wScript.Sleep 50
  echo(oWS.SendKeys "{ENTER}"
)
>"%_file2_%" (
  echo(wScript.Sleep 1000  
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "^{c}"
)
start /B cmd /C ""%_file1_%"" & rem del /F /Q "%_file1_%" 2>NUL"
start /B cmd /C ""%_file2_%"" & rem del /F /Q "%_file2_%" 2>NUL"
exit/B 0


:sendCTRLC_TESTED
SetLocal
set "_file1_=%TEMP%\after.vbs"
set "_file2_=%TEMP%\ctrlc.vbs"
>"%_file1_%" (
  echo(wScript.Sleep 300
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "s"
  echo(wScript.Sleep 50
  echo(oWS.SendKeys "{ENTER}"
)
>"%_file2_%" (
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "^{c}"
)
start /B cmd /C ""%_file1_%"" & rem del /F /Q "%_file1_%" 2>NUL"
start /B cmd /C ""%_file2_%"" & rem del /F /Q "%_file2_%" 2>NUL"
exit/B 0

这篇关于每当满足if条件时,我们就需要传递ctrl + c来停止当前项目并在shell脚本中启动下一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆