[英]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)? 仅当vg10或lv10包含一个卷(
cp
, sp
是卷)时,如何才能得到该数字? 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.