Search code examples
pythonbashsubprocessargumentspopen

Run bash command with $1, $2 in Python


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


Solution

  • 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