"Error: unknown shorthand flag: 'f' in -f5" when running bash script from Python

5/20/2020

I'm having an issue running a simple python script that reads a helm command from a .sh script and outputs it.

When I run the command directly in the terminal, it runs fine:

helm list | grep prod- | cut -f5

# OUTPUT: prod-L2.0.3.258

But when I run python test.py (see below for whole source code of test.py), I get an error as if the command I'm running is helm list -f5 and not helm list | grep prod- | cut -f5:

user@node1:$ python test.py

# OUTPUT:
# Opening file 'helm_chart_version.sh' for reading...
# Running command 'helm list | grep prod- | cut -f5'...
# Error: unknown shorthand flag: 'f' in -f5

The test.py script:

import subprocess

# Open file for reading
file = "helm_chart_version.sh"

print("Opening file '" + file + "' for reading...")
bashCommand = ""
with open (file) as fh: 
    next(fh) 
    bashCommand = next(fh)

print("Running command '" + bashCommand + "'...")

process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()

if error is None:
    print output
else:
    print error

Contents of helm_chart_version.sh:

cat helm_chart_version.sh

#  OUTPUT: 
## !/bin/bash
## helm list | grep prod- | cut -f5
-- Kobbi Gal
bash
kubernetes-helm
linux
python
shell

1 Answer

5/21/2020

Try to avoid running complex shell pipelines from higher-level languages. Given the command you show, you can run helm list as a subprocess, and then do the post-processing on it in Python.

process = subprocess.run(["helm", "list"], capture_output=True, text=True, check=True)
for line in process.stdout.splitlines():
  if 'prod-' not in line:
    continue
  words = line.split()
  print(words[4]) 

The actual Python script you show doesn't seem to be semantically different from just directly running the shell script. You can use the sh -x option or the shell set -x command to cause it to print out each line as it executes.

-- David Maze
Source: StackOverflow