拆分多行环境变量为行 [英] Splitting a multi-line environment variable into lines
问题描述
我有以下问题:
我一个詹金斯服务器上执行一个视窗批处理文件,并具有多行环境变量(集经由詹金斯参数)分成单线条。每一行是另一个程序的参数列表的一部分:
I have the following problem: I execute a windows batch file on a Jenkins server and have to split a multi-line environment variable (set vía a Jenkins parameter) into single lines. Each line is part of a parameter list for another program:
詹金斯文本参数:
-foo 224 -bar "Some parameter with spaces"
-foo 225 -bar "another param"
应该导致内部詹金斯以下呼叫:
Should lead to the following calls inside 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
,但没有任何成功。搜索没有露面任何有用的,什么是我试图给我的语法错误或刚刚打印的第一行。
I tried to split it with for /F
but did not have any success. Searching did not turn up anything useful, anything I tried gave me syntax errors or just printed the first line.
这是中,我尝试过的事情:
This is among the things that I tried:
for /f "tokens=* delims= " %%f in ("%varname%") do
给我的语法错误,因为该变量已经包含引号。
Gives me syntax errors, because that variable already contains quotes.
echo %varname%
是否仅输出变量的第一行
Does only output the first line of the variable.
任何想法?
推荐答案
在FOR / F命令采用LF的插入变量行分隔符(这是FOR / F命令的自然行为),所以没有什么额外的要求为了处理与FOR / F这样的变量:
The FOR /F command takes the LF's inserted in the variable as lines separators (this is the natural behavior of FOR /F command), so nothing additional is required in order to process such variable with 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将削减%值%。另外,如果你想的完整的生产线的由LF分隔,用delims =。输出例如:
The key here is expanding the variable with !exclamationMarks!; otherwise the LF will cut the %value%. Also, if you want complete lines separated by LF, use "delims=". Output example:
Line 1: The quick brown
Line 2: fox jumps over
Line 3: the lazy dog.
这篇关于拆分多行环境变量为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!