批处理脚本在工作了一个多月后停止工作



直到昨天,下面的脚本还运行得很好,现在它无法工作了。尝试读取文件并进行字符串替换的循环中出现了什么问题?

@echo off
:loop
SETLOCAL ENABLEDELAYEDEXPANSION EnableExtensions
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`kubectl get ns`) DO (

echo %%F|find "che" >nul
if errorlevel 1 (
set "jijijiji=ghghghg"
) else (
FOR /F "tokens=1* delims= " %%G IN ("%%F") DO (
echo %%G
set "INTEXTFILE=secret.yaml"
set "SEARCHTEXT=billy"
del secret_%%G.yaml
del this.yaml
for /f "delims=" %%A in ('type "%INTEXTFILE%"') do (
set "string=%%A"
set "modified=!string:%SEARCHTEXT%=%%G!"
echo !modified!>>"secret_%%G.yaml"
)
copy "secret_%%G.yaml" "this.yaml"
kubectl apply -f this.yaml
del this.yaml
)
)
)
ENDLOCAL
timeout /t 10
goto loop

正如Compo所说,由于存在错误,该脚本无法运行
当它起作用时,那就随机吧
只有在脚本启动之前定义了INTEXTFILESEARCHTEXT,它才能工作。

但解决方法很简单。将两个SET语句移动到第一个FOR循环之前

@echo off
:loop
SETLOCAL ENABLEDELAYEDEXPANSION EnableExtensions
SET count=1
set "INTEXTFILE=secret.yaml"
set "SEARCHTEXT=billy"
FOR /F "tokens=* USEBACKQ" %%F IN (`kubectl get ns`) DO (
...

谢谢@compo@jeb。它帮助了我。我在脚本中使用了powershell:

@echo off
:loop
SETLOCAL ENABLEDELAYEDEXPANSION EnableExtensions
SET count=1
set "INTEXTFILE=secret.yaml"
set "SEARCHTEXT=billy"
FOR /F "tokens=* USEBACKQ" %%F IN (`kubectl get ns`) DO (

echo %%F|find "che" >nul
if errorlevel 1 (
set "dummy=set"
) else (
FOR /F "tokens=1* delims= " %%G IN ("%%F") DO (
echo %%G
powershell -Command "(gc secret.yaml) -replace '!SEARCHTEXT!', '%%G' | Out-File -encoding ASCII secret_%%G.yaml"
copy "secret_%%G.yaml" "this.yaml"
kubectl apply -f this.yaml
del this.yaml
)
)
)
ENDLOCAL
timeout /t 100
goto loop

相关内容

最新更新