我正在用python创建一个shell模拟器,但是我想必须将用户输入保存到名为x的变量中(如果它是系统命令)
我试图列出每个命令的清单,但这太浪费时间了
0 blah blah card# visa 561100004331****
1 blah blah card# visa 561100004331**** with text after
2 561100004331****
3 visa: 561100004331**** and random number > 16 digits: 0011237324763246723487243
答案 0 :(得分:0)
如果您已经有了命令列表,则可以执行以下操作:
>>> from subprocess import Popen, PIPE
>>>
>>>
>>> def get_stderr(cmd):
... return Popen(cmd, shell=True, stderr=PIPE).communicate()[1]
...
>>>
>>> for c in ['garbage_cmd1', 'grep', 'which', 'garbage_cmd2']:
... if 'not found' in get_stderr(c).decode():
... print('%s is not valid' % c)
... else:
... print('%s is valid' % c)
...
garbage_cmd1 is not valid
grep is valid
which is valid
garbage_cmd2 is not valid
如果您使用的是Linux计算机,则还可以运行compgen -c >> cmds.txt