سؤال

I have a bash script that needs to be converted to a python program.

I run a command and capture its output in bash array and iterate through it.

diskArr=(`lsblk | grep 'disk' | awk -v col1=1 '{print $col1}'`)

The command gives me a list of all HDD's in the system and stores it in the array "diskArr".

I have tried using os.system and subprocess.Popen and haven't been successful.

>>> import shlex, subprocess
>>> command_line = raw_input()
lsblk | grep 'disk' | awk -v col1=1 '{print $col1}'
>>> args = shlex.split(command_line)
>>> print args
['lsblk', '|', 'grep', 'disk', '|', 'awk', '-v', 'col1=1', '{print $col1}']
>>>
>>>
>>> subprocess.Popen(args)
<subprocess.Popen object at 0x7f8e083ce590>
>>> lsblk: invalid option -- 'v'

Usage:
 lsblk [options] [<device> ...]
هل كانت مفيدة؟

المحلول

So far, you aren't actually converting the program to python, you're just trying to use python as a wrapper to the shell. But you can do the grepping and awking in python too:

import subprocess
import re

lsblk = subprocess.Popen(['lsblk'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in lsblk.stdout:
    if 'disk' in line:
        parts = re.split(r'\s+', line.strip())
        name, majmin, rm, size, ro, devtype = parts[:6]
        if len(parts) > 6:
            mountpoint = parts[6]
        else:
            mountpoint = None
        print(majmin)
returncode = lsblk.wait()
if returncode:
    print("things got bad. real bad.")

That's just one example. If you want a list of lines that refer to disks, you could build a list that holds lines that have 'disk' in them:

lsblk = subprocess.Popen(['lsblk'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
blockdevs = [line.strip() for line in lsblk.stdout if 'disk' in line]
returncode = lsblk.wait()
if returncode:
    print("things got bad. real bad.")
print(blockdevs)

نصائح أخرى

You may have a look at replacing the shell pipeline in the official documentation, it has a good example of what you are trying to do.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top