Multi-line command init_container z2jh

Hello,
I have the below cmd to be executed in the init_container:

cmd = f"""
                 if [ ! -d "/home/jovyan/{selected_course}" ]; then
                   mkdir -p "/home/jovyan/{selected_course}"
                 fi
                 if [ ! -d "/mnt/exchange/{selected_course}"/outbound" ]; then
                     mkdir -p "/mnt/exchange/{selected_course}/outbound"
                 fi
                 if [ ! -d "/mnt/exchange/{selected_course}/inbound" ]; then
                     mkdir -p "/mnt/exchange/{selected_course}/inbound"
                 fi
                 if [ ! -d "/mnt/exchange/{selected_course}/feedback_public" ]; then
                     mkdir -p "/mnt/exchange/{selected_course}/feedback_public"
                 fi
                 if [ ! -f "/home/jovyan/{selected_course}/nbgrader_config.py" ]; then
                   echo "c = get_config()" > "/home/jovyan/{selected_course}/nbgrader_config.py"
                   echo "c.CourseDirectory.root = '/home/jovyan/{selected_course}'" >> "/home/jovyan/{selected_course}/nbgrader_config.py"
                   echo "c.CourseDirectory.course_id = '{selected_course}'" >>  "/home/jovyan/{selected_course}/nbgrader_config.py"
                 fi
            """
            spawner.init_containers = [{
              "name":"initialization",
              "image": "busybox",
              "command": ["sh","-c", cmd],
              some_volumes

But here is what I see from the kubectl get po jupyter-someuser -o yaml:

initContainers:
  - command:
    - sh
    - -c
    - "\n         if [ ! -d \"/home/jovyan/pyf1\" ]; then\n           mkdir -p \"/home/jovyan/pyf1\"\n
      \        fi\n         if [ ! -d \"/mnt/exchange/pyf1\"/outbound\" ]; then\n
      \            mkdir -p \"/mnt/exchange/pyf1/outbound\"\n         fi\n         if
      [ ! -d \"/mnt/exchange/pyf1/inbound\" ]; then\n             mkdir -p \"/mnt/exchange/pyf1/inbound\"\n
      \        fi\n         if [ ! -d \"/mnt/exchange/pyf1/feedback_public\" ]; then\n
      \            mkdir -p \"/mnt/exchange/pyf1/feedback_public\"\n         fi\n
      \        if [ ! -f \"/home/jovyan/pyf1/nbgrader_config.py\" ]; then\n           echo
      \"c = get_config()\" > \"/home/jovyan/pyf1/nbgrader_config.py\"\n           echo
      \"c.CourseDirectory.root = '/home/jovyan/pyf1'\" >> \"/home/jovyan/pyf1/nbgrader_config.py\"\n
      \          echo \"c.CourseDirectory.course_id = 'pyf1'\" >>  \"/home/jovyan/pyf1/nbgrader_config.py\"\n
      \        fi\n    "

how can I write multi-line cmd in kubernetes cmd within python?
best

I’m not sure I understand the problem. You’ve shown that a multiline command is indeed created with the text provided. In the yaml, newlines are escaped with \n.

Yes but it supposed to be like this:

command:
  - sh
  - -c
  - |
   multi-line command here

update: this solved the issue:

cmd = [
              f"if [ ! -d /home/jovyan/{selected_course} ]; then mkdir -p /home/jovyan/{selected_course};  echo 'c = get_config()' > /home/jovyan/{selected_course}/nbgrader_config.py; echo 'c.CourseDirectory.root = \"/home/jovyan/{selected_course}\"' >> /home/jovyan/{selected_course}/nbgrader_config.py; echo 'c.CourseDirectory.course_id = \"{selected_course}\"' >>  /home/jovyan/{selected_course}/nbgrader_config.py; fi",
              f"if [ ! -d /mnt/exchange/{selected_course}/outbound ]; then mkdir -p /mnt/exchange/{selected_course}/outbound; fi",
              f"if [ ! -d /mnt/exchange/{selected_course}/inbound ]; then mkdir -p /mnt/exchange/{selected_course}/inbound; fi",
              f"if [ ! -d /mnt/exchange/{selected_course}/feedback_public ]; then mkdir -p /mnt/exchange/{selected_course}/feedback_public; fi",
            ]
            spawner.init_containers = [{
              "name":"initialization",
              "image": "busybox",
              "command": ["sh","-c", *cmd],
              "volumeMounts": [
              {
                "name": f"volume-{username}{servername}",
                "mountPath": "/home/jovyan"
              },
              {
                "name": "nbgrader-exchange" ,
                "mountPath": "/mnt/exchange"
              }
              ]
            }]

Yes but it supposed to be like this:

those are the same. yaml has multiple equivalent ways to serialize multiline strings. For example:

command:
  - a
  - |
    line1
    line2
  - "line1\nline2\n"

both the second and third item in the list have exactly the same value.