0

I'm trying to set IFS in a Bash shell within a Jenkins pipeline script. See line 34 below. The problem is I can't get the multi-level quoting correct. If I'm just typing at a bash terminal, the line would be IFS=$'\n'. But, no matter how many backslashes I use in different combinations, I can't make it work. At this point, I'm just guessing.

The $filename variable at the end of line 36 is a list of file names that have internal spaces separated by linefeed characters (\n). Hence, I want the IFS variable to be only \n.

enter image description here

Here is the same snippet in text form.

steps {
    sh """
        #!/bin/bash
        export PATH="$PATH:/root/.local/bin"
        IFS=\$\\'\\n\\'
        pip install -r requirements.txt --user
        python EC_Workload_Analysis/combine.py --webdav --zipfile ${BUILD_NUMBER} --user $SCRIPT_CREDS_USR --password $SCRIPT_CREDS_PSW $filenames
       """
      }
Marc
  • 13
  • 4
  • 1
    I would strongly recommend putting that code in a script. Trying to maintain non-trivial code written within another language is always a massive pain. – l0b0 Feb 26 '21 at 03:21
  • `IFS=$'\n'` works on my redhat but not on ubuntu. – Soprano86 Feb 28 '23 at 20:26

3 Answers3

0

can you try with

export IFS=$(echo -e "\n ")

because you can't insert the character you need, but you can create and store it, to test this you can do

echo "$IFS $IFS $IFS"
Samsul Islam
  • 2,581
  • 2
  • 17
  • 23
Lino_ares
  • 16
  • 2
0

Thanks to l0b0 for the comment! I put lines 32 to 36 in their own script in my git repo, run_me.sh, removed the extraneous quotes and backslashes, and then put bash < run_me.sh in place of lines 32 to 36.

run_me.sh looks like this:

#!/bin/bash
export PATH="$PATH:/root/.local/bin"
export IFS=$'\n'
pip install -r requirements.txt --user
python EC_Workload_Analysis/combine.py --webdav --zipfile $BUILD_NUMBER --user $SCRIPT_CREDS_USR --password $SCRIPT_CREDS_PSW $filenames
Marc
  • 13
  • 4
0

You can write like below code snippet and try.

steps {
    sh """
        #!/bin/bash
        export PATH="$PATH:/root/.local/bin"
        IFS=\$'\\n'
        pip install -r requirements.txt --user
        python EC_Workload_Analysis/combine.py --webdav --zipfile ${BUILD_NUMBER} --user $SCRIPT_CREDS_USR --password $SCRIPT_CREDS_PSW $filenames
       """
      }

Explanation: In double qoutes, $ is treated as a special character and so it needs to be escaped using \. \ is also a special character so we will escape that too by replacing it with double slashes \\.

I have tried with dummy data so please confirm if it works with your requirement or not.

mayurssoni
  • 66
  • 5