0

In the below code I am reading a .txt file and want to write it's content to the variable userId. But when I do echo "${userId}" I get 0, how come? The file itself is located in a SVN branch and I am using jenkins pipeline.

def userId = bat script: 'for /f "delims=" %%a in (user_id.txt) do echo %%a', returnStdout: true

edit: the txt file contains one line of an user id (for example cronaldo)

gants
  • 173
  • 1
  • 1
  • 13

2 Answers2

0

I have no knowledge about batch-scripting, but I would bet, it's the newline after the user_id in the text-file.

If you do the batch for loop on the command-line, you see the user-id on one line and afterwards an empty line I think...

You should try to suppress the empty lines in the output...

susi
  • 493
  • 4
  • 13
0

I think susi is right and you probably get another line than the one you want... You could try this solution and add an exit after the first echo of your loop, so you are sure to just output the first value.

Example batch :

@echo off

for /f %%a in (user_id.txt) do (
  echo %%a
  exit /b
)

Convert this to an inline version and put that in your Jenkins pipeline and you should be good to go.

Community
  • 1
  • 1
Pom12
  • 7,622
  • 5
  • 50
  • 69
  • This removes the other line, but it still does not work since I am getting 0 as output. The question is if it even is possible to assign a value from batch to groovy variable? – gants Sep 01 '16 at 13:11
  • depending on what "bat" returns... I didn't found some documentation about the command – susi Sep 01 '16 at 14:36
  • Could you please edit your post and add a sample output of your `user_id.txt` file please ? Also, [Bat documentation](https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#bat-windows-batch-script) seems to indicate this `returnStatus` parameter so I'd say you can use it... but I've never tested it... – Pom12 Sep 01 '16 at 14:42