0

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"'
7
  • 1
    This has nothing to do with Kubernetes and its commands or argument. Its is the way bash script is written, Eg: while true; do date; sleep 1; done . Notice there is no semicolon after do. Commented Apr 14, 2022 at 16:00
  • Also, IMO it's best to supply any multi-line script via config-maps not directly through the pod's command. Commented Apr 14, 2022 at 16:02
  • @P.... my reference to kube was because of pod where I am writing the script. So what I understood from your explanation is the syntax that I am using is because of the type of shell I am using like /bash Vs /sh ? Commented Apr 14, 2022 at 17:55
  • yes, the syntax of shell script is defined by type of shell used. Commented Apr 14, 2022 at 17:58
  • @P.... Following is the bash syntax I have now but it still error out args: - /bin/bash - -c - > for i in 1 2 3 4 5 do echo "Welcome $i times" done /bin/bash: -c: line 2: syntax error near unexpected token echo' /bin/bash: -c: line 2: echo "Welcome $i times"' Commented Apr 14, 2022 at 19:26

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.