0

Not a Bash developer here, so probably this is simple. I have a Bash script that port-forwards k8s pods (pf.sh):

POD=$(kubectl get pods | awk '{print $1}' | grep -e $1)
echo $POD
kubectl port-forward $POD 5433:5432

So you maybe call it with:

pf.sh some-main-part-of-a-pod-name

The problem is that when I echo $POD in the second line, the variable is exactly as I want it to use it in the third line. But when I try injecting it in the third line I get:

pods "some-main-part-of-a-pod-name-57f485d4f5-fdl4p\r" not found

Notice the carriage return at the end. Can I avoid this somehow?

More info: I'm using MobaXTerm bash flavor on a Windows laptop. And the input of the get pods command will look something like:

some-main-part-of-a-pod-name-57f485d4f5-fdl4p 2/2 Running 0 2d1h

Just to reiterate: there's no problem on the assignment of the POD variable. The problem seems to be on the third line, when I do the variable interpolation. It's in THAT line that I get the carriage return at the end. Not on the $POD (at least not according to the echo of the second line).

The echo $POD actually correctly returns:

some-main-part-of-a-pod-name-57f485d4f5-fdl4p

UPDATE!!! Figured it out! I simply can go by without the variable creation and interpolate the whole thing. Only one line:

kubectl port-forward $(kubectl get pods | awk '{print $1}' | grep -e $1) 5433:5432

nicokruk
  • 59
  • 6

1 Answers1

0

Try this (not tested):

POD=$(kubectl get pods | perl -sane 's/\r//g; print $F[0] if /$regex/' -- -regex=$1)
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223