0

In a bash script:

    jenkins_folder=`cut -d "|" -f1 -s input.csv`
    jenkins_url='https://url.com:8181/jenkins/view/'
    echo "jenkins_folder : ${jenkins_folder}"
    for job in `java -jar jenkins-cli.jar -s ${jenkins_url}${jenkins_folder} list-jobs ${jenkins_folder} `
do
    echo "Job name:: ${job} ****"
    java -jar jenkins-cli.jar -s ${jenkins_url}${jenkins_folder} get-job ${job} > job.xml
done

is giving following output

jenkins_folder : Platform-X.X.X-SPO-MyPD-Integration-Dummy
 ****ame:: NH-AccountManagementAudit-Consumer-MyPD-Integration-DUMMY-Reporting
'; perhaps you meant 'NH-AccountManagementAudit-Consumer-MyPD-Integration-DUMMY-Reporting'?

And when I substitute values of all variables and run following command; it works fine

java -jar jenkins-cli.jar -s https://url.com:8181/jenkins/view/Platform-X.X.X-SPO-MyPD-Integration-Dummy get-job NH-AccountManagementAudit-Consumer-MyPD-Integration-DUMMY-Reporting > job.xml

I have wasted too much time on this. I even tried using eval function but no luck. Please help.

Thanks to @Kusalananda when I am trying to echo job name its printing output in wierd fashion.. I feel this is the root cause.. but not sure why that is happening

If I try to shorten the length of variable job (using substring); then it prints fine. Hence, if value of job is long; it is creating problem

9
  • 1
    Please edit your question and show us how the variables get their values and what value each variable has.
    – terdon
    Apr 24, 2023 at 11:03
  • 1
    I'm assuming you read the values of your variables, or at least of job, from a DOS text file?
    – Kusalananda
    Apr 24, 2023 at 11:11
  • @Kusalananda i am getting value of job as output from another command; updated the code Apr 24, 2023 at 11:26
  • What happens if you add more text after the output of ${job} in that echo call?
    – Kusalananda
    Apr 24, 2023 at 11:29
  • 1
    @Kusalananda it gives wierd output. when i adds asteriks at lasts; it echos those in front Apr 24, 2023 at 11:33

1 Answer 1

3

The first issue is obvious: your url is https://url.com:8181/jenkins/view, so since it doesn't end with a slash, the value of ${jenkins_url}${jenkins_folder} is https://url.com:8181/jenkins/viewPlatform-X.X.X-SPO-MyPD-Integration-Dummy. So fix the url:

jenkins_url='https://url.com:8181/jenkins/view/'

Or, add the slash when you combine variables:

${jenkins_url}/${jenkins_folder}

Next, remember to always quote your variables:

java -jar jenkins-cli.jar -s "${jenkins_url}${jenkins_folder}" get-job "$job" > job.xml

Your other issue is that it sounds like you have a file or command output with Windows line endings. That might be input.csv or anything else, but you will need to remove them. First run

dos2unix input.csv

Or, if you don't have dos2unix, run:

sed -i 's/\r//' input.csv

Then try your script again. If it still doesn't work, update your question with more details. If the \r is from your jenkins-cli command, try this:

jenkins_folder=`cut -d "|" -f1 -s input.csv`
jenkins_url='https://url.com:8181/jenkins/view/'
echo "jenkins_folder : ${jenkins_folder}"
java -jar jenkins-cli.jar \ 
 -s "${jenkins_url}/${jenkins_folder}" \
  list-jobs "$jenkins_folder" |
   sed 's/\r$//' | while read job
do
    echo "Job name:: ${job} ****"
    java -jar jenkins-cli.jar -s ${jenkins_url}${jenkins_folder} get-job ${job} > job.xml
done
12
  • I missed slash in URL while masking the actual URL. Editing the question. I tried using double quotes also; but still same output. I feel the problem we need to fix is that why during echo JobName it is putting asterikes in beginning ! Apr 24, 2023 at 11:46
  • and yes I did run your sed command; no change Apr 24, 2023 at 11:47
  • @DeepakSinghal that is the windows line endings. You haven't told us anything about your system, so we can't know which part is causing it, but you have Windows somewhere in there.
    – terdon
    Apr 24, 2023 at 11:47
  • file was originally coded on Mac and now running them on Linux. Apr 24, 2023 at 11:49
  • 1
    Their java command in the loop header outputs DOS text, so $job has a carriage-return at the end.
    – Kusalananda
    Apr 24, 2023 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .