Jenkins EnvInject plugin - Environment variable value containing multiple lines - jenkins

I followed the suggestions from this post on how to use the EnvInject plugin to create and set Jenkins environment variables. I'm using the "Inject environment variables" in post build step and set "Properties File Path"
The windows batch script create a environment variable OPS and writes it to a property file : results.txt which contains multiple lines , like :
OPS= This is line one,
This is two
This is three
Challenge: OPS picks up only the first line from results.txt and skips the rest of the lines.
How do I set OPS have all the lines as its value ?
cd C:\To\Test\Class\Path
java utilities.LogExtractor>ops.txt
#echo off
setlocal EnableDelayedExpansion
set LF=^
rem *** Two empty lines are required for the linefeed
FOR /F "delims=" %%a in (ops.txt) do (
set var=!var!!LF!%%a
)
set var=!var!!LF!
echo OPS=!var! > %JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\results.txt
and I set the "Properties File Path" to %JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\results.txt

From the source code, I'd say it uses java.util.Properties to load the file, calling the load method. The documentation says you can escape a line break with a backslash, so using
OPS= This is line one,\
This is two\
This is three
should be sufficient. (Be careful, whitespace at the beginning of the line is ommitted.)

Related

For loop in batch file dies calling Windows conversion tool

ParseRat is an old program that I have had a long time. I need to transpose text files in c:\wherefilesare.
I run the below and it just dies:
echo on
setlocal EnableDelayedExpansion
cd c:\wherefilesare
SET progdir=C:\program files (x86)\ParseRat
for%%x in (*.txt) do ("%%progdir%\parserat.exe" "%%x.txt" "%%progdir%\test.prz" "%%x.csv"
As #Squashman says:
"You (have) an extra percent symbol for all your (environment) variables. You are also not using the FOR meta-variable correctly. Essentially %%x expands to the actual file name with the extension. So it will see file1.txt.txt and file1.txt.csv. You need to use the command modifiers. %%~nx to get just the file name without the extension."
Also, you are missing a closing parenthesis...
echo on
setlocal EnableDelayedExpansion
cd c:\wherefilesare
SET progdir=C:\program files (x86)\ParseRat
for %%x in (*.txt) do (
"%progdir%\parserat.exe" "%%~nx.txt" "%progdir%\test.prz" "%%~nx.csv"
)

how to escape empty lines from end of command result

i use the following command to create a variable from my CPU Name:
#echo off
Rem create variable from cpu name
for /f "useback tokens=* skip=1" %%g in (`wmic cpu get name ^|findstr /i "."`) do (
set CPU_NAME=%%g
echo %CPU_NAME%
)
but the result is nothing, because there is some empty lines at end of "wmic cpu get name" command result and remove created variable
how can i solve it?
thanks a lot
Please search SO for delayed expansion.
call echo %%CPU_NAME%%
should show you the required data. This is one of several well-documented solutions.
There are some empty lines at the end of wmic cpu get name
Use findstr as follows to strip blank lines from the wmic output. You also need to use delayed expansion
Corrected batch file (test.cmd):
#echo off
setlocal enabledelayedexpansion
Rem create variable from cpu name
for /f "useback tokens=* skip=1" %%g in (`wmic cpu get name ^| findstr /r /v "^$"`) do (
set CPU_NAME=%%g
echo !CPU_NAME!
)
endlocal
Example usage:
> test
Intel(R) Core(TM) i5-2410M CPU # 2.30GHz
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
findstr - Search for strings in files.
wmic - Windows Management Instrumentation Command.
There is no need to echo the result within the for loop in your case because you are only setting a single name. Just echo it later.
#Echo Off
For /F "Skip=1 Delims=" %%A In ('WMIC CPU Get Name'
) Do For /F "Delims=" %%B In ("%%A") Do Set "CPU_NAME=%%B"
Echo=%CPU_NAME%
The second For loop is intended to remove the unwanted 'empty lines' you reported.

Getting error for Data stage compare command line tool

I am using a utility provided in Data Stage 9.1 diffapicmdline.exe to compare two jobs from different environment. I am using following batch script code to read the job names from text file in loop:
#echo off
SET var=
for /f "delims=" %%i in (grm_deploy.txt) do (C:\IBM\InformationServer91\Clients\Classic\diffapicmdline.exe /lhscd "/d=cs1cd:9080 /h=cs1i04 /u=user/p=password cs1cdhbi04/IST_GRM %%i" /rhscd "/d=cs1cd:9080 /h=cs1i04 /u=test /p=Pass cs1cdhbi04/TEST_NIMISH %%i" /t job /ot html /ol E:\compare_output.html)
echo this is the end
However I am getting following error:
D:\dataStage Components\Scripts>read_file.bat
Validating syntax of /lhscd.
Unknown flag specified 'jbLoadStgARxAR'
this is the end
Can anyone let me know what is going wrong over here?

How to get relative path from current folder?

This is what i currently get with FOR loop:
FOR /R "ProgramFolder" %%P in (*) do (
echo %%P
)
C:\Folder\Folder2\ProgramFolder\Managed\bolt.dll
C:\Folder\Folder2\ProgramFolder\Resources\fmodstudio.dll
C:\Folder\Folder2\ProgramFolder\Plugins\dll\DynamicWaterNativeWrapper.dll
How to get pathes like this?
Managed\bolt.dll
Resources\fmodstudio.dll
Plugins\dll\DynamicWaterNativeWrapper.dll
edited on dbenham comments
#echo off
setlocal enableextensions disabledelayedexpansion
pushd c:\somewhere\ProgramFolder
for /f "tokens=1,* delims=\" %%a in (
'xcopy . "%temp%" /l /s'
) do if not "%%b"=="" echo(%%b
popd
The basic idea behind this code is to use the xcopy command not to copy, but to retrieve a list (/l) of the files that should be processed with relative paths. For it to work, it is necessary to first change the current active directory to the required one (pushd) and use a relative reference to the current folder (.)
The output of xcopy command with this configuration will be in the form
.\folder\folder\file.ext
To remove the prefixing dot and backslash the for /f is configured to use the slashes as delimiters and to retrieve the first token (the dot) before the first backslash and the rest of the line as the second token, that is, the relative paths
As the xcopy command output includes an aditional line with the total number of files, and this line will result in an aditional blank line in the output of the script, an aditional if is included to discard this line.

KSH Resolving Environmental Variables for an Entire File

I have a list of file names with environment variables in them. I'd like to read the file line by line and then set a variable to the read in line however have the envirnment variable translated to the appropriate environment variable. Here is my script so far:
#!/bin/ksh
. /test/currentEnv.sh
while read line
do
echo $line
done < $1
if my source file is:
foo1$ENVVAR1.csv
foo2$ENVVAR2.csv
foo3$ENVVAR3.csv
and my Environment variables in currentEnv.sh are
$ENVVAR1=hello; export ENVVAR1
$ENVVAR2=world; export ENVVAR2
$ENVVAR3=test; export ENVVAR3
I'd like the results of the script to be
foo1hello.csv
foo2world.csv
foo3test.csv
currently it just dumps out the original file:
foo1$ENVVAR1.csv
foo2$ENVVAR2.csv
foo3$ENVVAR3.csv
Edit
I was able to get the majority of my files resolved using:
#!/bin/ksh
. /test/currentEnv.sh
while read line
do
eval echo $line
done < $1
however some of my variables are in the middle of string like:
foo3$ENVVAR3_bar.csv
this seems to look for an env variable $ENVVAR3_bar and doesn't find it I need this to output:
foo3test_bar.csv
You declare a variable without the dollar sign:
$var=value # no
var=value # yes
Since underscore is a valid character for a variable name, ksh is trying to expand the variable named ENVVAR3_bar: you need to use braces to separate the variable name from the surrounding text:
foo3$ENVVAR3_bar.csv # no
foo3${ENVVAR3}_bar.csv # yes

Resources