I'm using kubernetes, and I want to get podname from the app name. Here is the command I use.
POD=$(kubectl get pods -l app=influxdb-local -o custom-columns=:metadata.name -n influx)
Then I would like to use the result in:
kubectl exec -it -n influx $POD -- influx -username user -password "user" -execute "CREATE DATABASE db"
but the first line is empty, and the second line contains pod name, so the second command doesn't work.
How should I remove the first white line ?
Add --no-headers
to skip the header line
kubectl get pods -l app=influxdb-local -o custom-columns=:metadata.name -n influx --no-headers
Using the flag -o custom-columns=<header-name>:<field>
will let you customize the output.
Example with resource name, under header NAME
kubectl get pods -o custom-columns=NAME:metadata.name
output
NAME
myapp-5b77df6c48-dbvnh
myapp-64d5985fdb-mcgcn
httpd-9497b648f-9vtbl
Empty header name: you used the flag as -o custom-columns=:metadata.name
- the first line is the header line, but with an empty header.
the proper solution to omit the header line is by using the flag --no-headers
kubectl get pods -o custom-columns=NAME:metadata.name --no-headers
Example output
myapp-5b77df6c48-dbvnh
myapp-64d5985fdb-mcgcn
httpd-9497b648f-9vtbl