0

Hi I need to run following bash command in python:

function get_setting () {
   echo $(cat /proc/cmdline |
   awk -F"$1=" '{print $2}' | awk -F" " '{ print $1 }' )
}

I tried this:

process = subprocess.Popen(["cat", "/proc/cmdline | awk -F"$1=" '{print $2}' | awk -F" " '{ print $1 }", "BOOT_IMAGE"], stdout=subprocess.PIPE)

but it doesn't work.

I don't want to load bash script and run it by python. How do I do that? Any help would be greatly appreciated. thanks

sam2215
  • 31
  • 7
  • 1
    Make sure to escape your double quotes: `...-F\"$1=\"...` – C.Nivs Dec 10 '20 at 19:25
  • Don't abuse cat. Instead pass the file name to awk args. Also can you clarify "doesn't work"? What happens. What is your output? – OneLiner Dec 10 '20 at 19:28
  • 5
    This doesn't work in it's current form because the `|` pipelines are a shell feature and you are not using `shell=True`. IMO don't bother trying to subprocess this, you can trivially rewrite it in pure python since it's just basic string parsing – jordanm Dec 10 '20 at 19:31
  • 1
    +1 for @jordanm remark about doing it in python. Although it doesn't answer the OP's original question I would second his suggestion. – OneLiner Dec 10 '20 at 19:32

1 Answers1

1

If you want to ignore @jorganm suggestion to use pure python (And I would suggest going with pure Python), then this works on my machine:

>>> Popen("cat /proc/cmdline | awk -F\"$1=\" '{print $2}' | awk -F\" \" '{ print $1 }'", shell="True")
<subprocess.Popen object at 0x7f2cf1a12550>
>>> /vmlinuz-3.10.0-1160.2.2.el7.x86_64
OneLiner
  • 571
  • 2
  • 6