0

I can't figure out why my code doesn't return any output. I'm running python 3.8

bash_command = ['compgen', '-c']
process = subprocess.run(bash_command,
                         check=True,
                         text=True,
                         shell=True,
                         executable='/bin/bash',
                         capture_output=True
                         )

software_list = process.stdout.split('\n')
print(software_list)

Print gives me empty list: ['']

EDIT:

  1. compgen is bash command which lists all available commands, both built-ins and installed programs available in PATH
  2. I have compgen installed on my machine
Janek
  • 3
  • 1
  • Please add `print(process)` immediately before `software_list = process.stdout.split('\n')` and write what it is output. – Daweo Feb 10 '21 at 11:33
  • What does `print(repr(process))` show? – juanpa.arrivillaga Feb 10 '21 at 11:33
  • both print(process) and print(repr(process)) returned: CompletedProcess(args=['compgen', '-c'], returncode=0, stdout='', stderr='') – Janek Feb 10 '21 at 11:48
  • Does this answer your question? [Subprocess library won't execute compgen](https://stackoverflow.com/questions/23550650/subprocess-library-wont-execute-compgen) – MisterMiyagi Feb 10 '21 at 11:53

1 Answers1

0

When you run a program specifying shell=True, the command argument can be a single string instead of a list of strings. This is one instance where it needs to be a single string:

import subprocess


bash_command = 'compgen -c' # single string
process = subprocess.run(bash_command,
                         check=True,
                         text=True,
                         shell=True,
                         executable='/bin/bash',
                         capture_output=True
                         )

software_list = process.stdout.split('\n')
print(software_list)
Booboo
  • 38,656
  • 3
  • 37
  • 60