我有以下问题:我在Jenkins服务器上执行一个windowsbatch file,并且必须将一个多行环境variables(将一个Jenkins参数设置为vias)分成单行。 每行都是另一个程序的参数列表的一部分:
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
分割它,但是没有成功。 search没有什么有用的东西,我试过的任何东西给了我语法错误或只是打印第一行。
这是我尝试的事情之一:
for /f "tokens=* delims= " %%f in ("%varname%") do
给我的语法错误,因为该variables已经包含引号。
echo %varname%
只输出variables的第一行。
有任何想法吗?
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将减少%值%。 另外,如果你想要用LF分隔完整的行 ,使用“delims =”。 输出示例:
Line 1: The quick brown Line 2: fox jumps over Line 3: the lazy dog.
那么作为反射,我建议你以下解决方案:
如果安装了python(如果没有安装它,这是有用的;-))
在批处理行中:Python MyScriptToExecuteEachLinesFromVar.py varname“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"
这应该输出
第一行:快速的棕色
2号线:狐狸跳过
第3行:懒狗。
或者,如果您宁愿避免临时文件,则可以调用另一个运行时将环境变量提供给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'));
输出与上面相同。