简体   繁体   English

我如何从Python列表中获取特定字符,后跟命令行参数变量

[英]How can i get a particular character from a Python list followed by command line argument variable

mount_list = ['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']

mount_list is a list li[1] = '/dev/vg10/lv10:cp:99'. mount_list是列表li[1] = '/dev/vg10/lv10:cp:99'.

How can I get the number followed by vg10 or lv10 only when it contains a volume ( cp , sp are volumes)? 仅当vg10lv10包含一个卷( cpsp是卷)时,如何才能得到该数字? It's a bit confusing, so I'll explain completely. 这有点令人困惑,所以我将完全解释。

#!/usr/bin/python

import subprocess
import sys

def volumeCheck(volume_name):
    """This function will check volume name is mounted or not.
    """
cmd  = ['cat', '/etc/auto_mount_list']
cmd1 = ['cat', '/etc/mdadm.conf']
cmd2 = ['cat', '/proc/mdstat']

vname = sys.argv[1]

p = subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1, err = p.communicate()
pattern = p1

p2 = subprocess.Popen(cmd,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p3, err = p2.communicate()
mount_list = p3.split()
#if mount_list.find(vname) != -1 here i need the number of vg or lv only when it conatin sys.argv[1]


p4 = subprocess.Popen(cmd1,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p5, err = p4.communicate()
mdadm_list = p5.split()

p6 = subprocess.Popen(cmd2,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p7, err = p6.communicate()


new_vol = '/VolumeData/' + sys.argv[1] 

#if pattern.find(new_vol) != -1 : this also give the proper output but below code is more efficiant.

if new_vol in p1:
    print 'volume mounted'
else:
    print 'volume not mounted'


if __name__ == '__main__':
   volumeCheck(sys.argv[1])

Here is the output of my Python program: 这是我的Python程序的输出:

root@sr-query:/# python volume_check.py cp
['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']
volume mounted
root@sr-query:/# df -h
Filesystem            Size  Used Avail Use% Mounted on
rootfs                938M  473M  418M  54% /
/dev/md0              938M  473M  418M  54% /
none                  250M  4.9M  245M   2% /dev
/dev/md2              9.7M  1.2M  8.0M  13% /usr/config
/dev/md7              961M   18M  895M   2% /downloads
tmpfs                 250M  8.2M  242M   4% /var/volatile
tmpfs                 250M     0  250M   0% /dev/shm
tmpfs                 250M     0  250M   0% /media/ram
/dev/mapper/vg10-lv10
                     1016M   65M  901M   7% /VolumeData/cp
root@sr-query:/# python volume_check.py new_volume
['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']
volume not mounted
root@sr-query:/# 

Now it's clear that python volume_check.py new_volume (its argument) is not mounted and also not in the auto mount list. 现在很明显, python volume_check.py new_volume (它的参数)没有被安装,也没有被安装在自动安装列表中。 But python volume_check.py cp is mounted and found in auto mount list... in this 2nd case I need to take the vg and lv number from the auto mount list . 但是python volume_check.py cp已安装并在自动安装列表中找到...在python volume_check.py cp情况下,我需要从自动安装列表中获取vg和lv编号

How can I change my code to obtain this solution? 如何更改我的代码以获得此解决方案? Thanks in advance. 提前致谢。

Not sure if I understand your question correctly, but I think you want to extract the number from the li[1] string, correct? 不知道我是否正确理解了您的问题,但是我认为您想从li[1]字符串中提取数字,对吗? If so, just use a regex: 如果是这样,请使用正则表达式:

>>> import re
>>> s = '/dev/vg10/lv11:cp:99'
>>> m = re.search('/vg(?P<vg>\d+)/lv(?P<lv>\d+):\w+:(?P<number>\d+)', s)
>>> print m.groupdict()
{'lv': '11', 'number': '99', 'vg': '10'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从python中的字符串中提取特定数字,然后是命令行参数变量 - Extract a particular number followed by a command line argument variable from a string in python python argh / argparse:如何将列表作为命令行参数传递? - python argh/argparse: How can I pass a list as a command-line argument? 如何使用 argparse 将列表作为命令行参数传递? - How can I pass a list as a command-line argument with argparse? 有没有一种方法可以在 python 中传递 0 命令行 arguments 并且如果传递了特定参数则具有相同的结果? - Is there a way that I can pass 0 command line arguments in python and have the same result if a particular argument is passed? 如何使命令行参数值分配给 python 中的 2 个变量选择? - How can I make command line argument values get assigned to selection of 2 variables in python? 如何在没有脚本参数的情况下从命令行启动Python调试器? - How can I start the Python debugger from the command line without a script argument? 如何从Python读取命令行参数? - How do I read a command line argument from Python? 如何使特定python命令的所有输出静音? - how can I silence all of the output from a particular python command? Python Argparse:获取用于命名空间变量的命令行参数 - Python Argparse: Get the command-line argument used for a Namespace variable 将变量从python脚本传递给C程序(命令行参数) - Pass variable from python script to C program(command line argument)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM