2016-03-22 8 views
0

私は、端末に別名を入力したときと同じように、私が選んだユーザーのエイリアスを表示するために、Pythonでスクリプトを作成しようとしています。Pythonでユーザーエイリアスを表示する方法

tt = open("/etc/passwd" , "r") 
with tt as f2: 
    with open("passwd" , "w+") as f1: 
     f1.write(f2.read()) 
     f1.seek(0,0) 
     command = f1.read() 
     print 
     print command 

chose = raw_input("select user's name from this list > ") 
rootlist = "1) Show user groups \n2) Show user id \n3) Show users alias\n4) Add new alias \n5) Change Password \n6) Back" 
print 
print rootlist 
print 
chose2 = int(raw_input("Choose a command > ")) 
if choose == 3: 
    os.system("alias ") 

しかしos.system(「エイリアス」)は動作しないと私はtはそれを行う適切な方法を見つけるように見えることはできません。 これまでのコードは次のようになります。

答えて

0

エイリアスこれは、あなたのシェルコマンドにbashへの呼び出しを追加することで解決することができ、問題である

$ type -a alias 
alias is a shell builtin 

ここで見ることができ、組み込みシェルです

import os 
os.system('bash -i -c "alias"') 

または優先サブプロセスモジュールを使用する方法

from subprocess import Popen, PIPE, STDOUT 

cmd = 'bash -i -c "alias"' 
event = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) 
output = event.communicate()[0] 
print(output) 
関連する問題