'Kubernetes POD Command and argument

I am learning kubernetes and have the following question related to command and argument syntax for POD.

Are there any specific syntax that we need to follow to write a shell script kind of code in the arguments of a POD? For example

In the following code, how will I know that the while true need to end with a semicolon ; why there is no semi colon after do but after If etc

   while true;
  do
  echo $i;
  if [ $i -eq 5 ];
  then
    echo "Exiting out";
    break;
  fi;
      i=$((i+1));
      sleep "1";
  done

We don't write shell script in the similar way from semicolon prespective so why do we have to do this in POD.

I tried the command in /bin/bash format as well

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: bash
  name: bash
spec:
  containers:
  - image: nginx
    name: nginx
    args:
    - /bin/bash
    - -c
    - >
       for i in 1 2 3 4 5
       do
         echo "Welcome $i times"
       done
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Error with new code

/bin/bash: -c: line 2: syntax error near unexpected token `echo'
/bin/bash: -c: line 2: `  echo "Welcome $i times"'


Solution 1:[1]

Are there any specific syntax that we need to follow to write a shell script kind of code in the arguments of a POD?

No, shell syntax is the same across.

...how will I know that the while true need to end with a semicolon

Used | for your text block to be treated like an ordinary shell script:

...
args:
- /bin/bash
- -c
- |
  for i in 1 2 3 4 5
  do
    echo "Welcome $i times"
  done

When you use > your text block is merge into a single line where newline is replaced with white space. Your command become invalid in such case. If you want your command to be a single line, then write them with ; like you would in ordinary terminal. This is shell scripting standard and is not K8s specific.

If you must use >, you need to either add empty line or indented the next line correctly:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: bash
  name: bash
spec:
  containers:
  - image: nginx
    name: nginx
    args:
    - /bin/bash
    - -c
    - >
      for i in 1 2 3 4 5
        do
          echo "Welcome $i times"
        done
  restartPolicy: Never

kubectl logs bash to see the 5 echos and kubectl delete pod bash to clean-up.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gohm'c