将多行环境变量拆分为多行

Splitting a multi-line environment variable into lines

我遇到以下问题: 我在 Jenkins 服务器上执行 windows 批处理文件,必须将多行环境变量(通过 Jenkins 参数设置)拆分为单行。每行都是另一个程序的参数列表的一部分:

Jenkins 文本框参数:

-foo 224 -bar "Some parameter with spaces"
-foo 225 -bar "another param"

应该会导致 Jenkins 内部的以下调用:

myprog.exe -baz 0 -meow -foo 224 -bar "Some parameter with spaces"
myprog.exe -baz 0 -meow -foo 225 -bar "another param"

我尝试用 for /F 拆分它,但没有成功。搜索没有找到任何有用的东西,我尝试的任何东西都给我语法错误或者只是打印了第一行。

这是我尝试过的事情之一:

for /f "tokens=* delims= " %%f in ("%varname%") do

给我语法错误,因为该变量已经包含引号。

echo %varname%

只输出变量的第一行。

有什么想法吗?

作为反射,我建议您采用以下解决方案:

如果安装了 python(如果没有...由您来安装它..它很有用 ;-))

在你的批处理行中: Python MyScriptToExecuteEachLinesFromVar.py 变量名 "myprog.exe -baz 0 -meow"

附脚本内容:

import sys,os  # libraries used : system and operating system

if len(sys.argv)>0:                   # if arguments provided (at least the variable name)
  varname = sys.argv[1]               # the variable name is the second argument (0=script)
  prefix = " ".join(sys.argv[2:])     # the list of all others are the prefix.

  lines = os.environ[varname].split('\n')  # get the var and split lines 

  for f in lines : 
    print "execute:", prefix, f
    os.system(prefix+" "+f)                # execute each line with the given prefix.

希望对您有所帮助。

您可以 echo 将变量输出到临时 txt 文件,然后使用 for /f 循环逐行处理该 txt 文件。您还应该使用延迟扩展来防止换行符被不恰当地评估。

@echo off
setlocal enableDelayedExpansion

:: // Test environment.  Create a variable containing line breaks.
set BR=^


:: // note: The two empty lines above are required.
set "str=The quick brown!BR!fox jumps over!BR!the lazy dog."

:: // Test environment ready.
:: // Output to temporary txt file.
>"%temp%\tmp.txt" echo(!str!

:: // Process the txt file line by line.
set "line=0"
for /f "usebackq delims=" %%I in ("%temp%\tmp.txt") do (
    set /a line += 1
    echo Line !line!: %%I
    rem // myprog.exe -baz 0 -meow %%I
)

:: // Delete temporary txt file.
del "%temp%\tmp.txt"

那应该输出

Line 1: The quick brown

Line 2: fox jumps over

Line 3: the lazy dog.


或者如果您不想使用临时文件,您可以调用另一个运行时来为 for /f 循环提供环境变量。这是一个 JScript 混合示例。

@if (@CodeSection == @Batch) @then

@echo off
setlocal enableDelayedExpansion

:: // Test environment.  Create a variable containing line breaks.
set BR=^


:: // note: The two empty lines above are required.
set "str=The quick brown!BR!fox jumps over!BR!the lazy dog."

:: // Test environment ready.
:: // Invoke JScript to read the environment variable and return it line-by-line
set "line=0"
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0"') do (
    set /a line += 1
    echo Line !line!: %%I
)

:: // end main runtime
goto :EOF

@end // JScript portion simply echoes %str% from the context of the current process
WSH.Echo(WSH.CreateObject('WScript.Shell').Environment('PROCESS')('str'));

输出同上

FOR /F 命令将插入变量中的 LF 作为行分隔符(这是 FOR /F 命令的自然行为),因此不需要任何额外的操作来使用 FOR /F 处理此类变量:

@echo off
setlocal EnableDelayedExpansion

rem Create a variable containing line breaks.
set LF=^
%empty line 1/2%
%empty line 2/2%
set "str=The quick brown!LF!fox jumps over!LF!the lazy dog."

set line=0
for /F "delims=" %%a in ("!str!") do (
   set /A line+=1
   echo Line !line!: %%a
)

这里的关键是用!exclamationMarks!扩展变量;否则 LF 将削减 %value%。此外,如果您想要 完整行 由 LF 分隔,请使用 "delims="。输出示例:

Line 1: The quick brown
Line 2: fox jumps over
Line 3: the lazy dog.