1

I'm creating a build job within the Jenkins CI/CD software. The build job will clone my android project from Github and generates the APK. I'm using Ubuntu 20.04 LTS server OS.

As part of the build step, I'm executing the following command via Jenkins:

rm -r app/src/main/java/in/myproj/utils/Constants.kt 

But receiving the following error:

/tmp/jenkins10737829520192897047.sh: 1: Syntax error: "in" unexpected 

It looks like the bash is treating the folder name in as a Constant. I have been trying to find a solution since morning today but failed miserably. Any help is greatly appreciated.

Edit:

I have debugged the issue further and found the actual cause. It is not the rm command that is failing. I have another build step where I'm appending some code snippets to a file. The following is the code snippet that is actually failing:

cat << EOT >> app/src/main/java/in/myproj/utils/Constants.kt "package `in`.myproj.utils class Constants { companion object { const val BASE_URL = "https://api.example.in/api/" const val BASE_URL_IMAGE_ADDRESS = "https://dashboard.example.in:5555/" const val BASE_URL_PROFILE_IMAGE_ADDRESS = "https://api.example.in/" const val ABOUT_US_URL = "https://example.in/aboutus.html" const val PRIVACY_POLICY_URL = "https://example.in/privacy-policy.html" const val TERMS_AND_CONDITIONS_URL = "https://example.in/terms-condition.html" const val REQUEST_TIMEOUT_DURATION = 10 const val STATUS_SUCCESS = 1 
9
  • 2
    Please edit your question and add some context. Is that the entire script? How do you run the script? What operating system are you using? Commented Sep 9, 2021 at 12:29
  • @terdon Thanks. I have updated my question. Commented Sep 9, 2021 at 12:41
  • Thanks. But i) is that the entire script and ii) how do you run it exactly? The error doesn't make sense. My first guess is that there is some non-printing character there. Can you also add the output of cat -v your_script.sh? Commented Sep 9, 2021 at 12:44
  • @terdon rm -r app/src/main/java/in/myproj/utils/Constants.kt is the entire script. When I execute it via terminal directly, it works fine. It fails only when I execute it via Jenkins. Commented Sep 9, 2021 at 13:24
  • Can you also add the output of cat -v /tmp/jenkins10737829520192897047.sh? And how does Jenkins execute it? Does it help if you add a shebang line (#!/bin/sh) to the top of your script? Commented Sep 9, 2021 at 14:05

1 Answer 1

0

The problem is in how you are using the EOT. When using an unquoted word as a heredoc delimiter, the contents of the heredoc will be expanded by the shell before being written. As explained in the bash manual:

If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \newline is ignored, and ‘\’ must be used to quote the characters ‘\’, ‘$’, and ‘`’.

As a result, the `in` in your heredoc is being read by the shell as a command substitution, so it tries to run the command in and return its output.

In practical terms, this means you need to quote the EOT:

cat << 'EOT' >> app/src/main/java/in/myproj/utils/Constants.kt 

Note that this means that any variables you may have in your here doc will also not be expanded, but since I don't see any in your question, I am assuming that isn't a problem.

1
  • Apologies for the delayed response and thank you very much for guiding me. Yes, I did not wanted to expand any of the variables and just wanted to populated the file as-is. I have implemented your suggestion and it produced the expected result. Thanks a lot!! Commented Sep 11, 2021 at 5:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.