さて、私はpython3スクリプトを使用してMAC OS Xでエイリアスを管理しようとしています。最初に、すべてのエイリアスコマンドを1つのファイルに入れ、以下のコードを使用してこれらのエイリアスをオン/オフしようとしました:
def enable_alias(self):
alias_controller = AliasListControl() # just a simple class to handle the single file path and other unimportant things.
os.popen('cp ~/.bash_aliases ~/.bash_aliases.bak')
os.popen('cat ' + alias_controller.path + '>> ~/.bash_aliases')
os.system('source ~/.bash_aliases')
def disable_alias(self):
os.popen('mv ~/.bash_aliases.bak ~/.bash_aliases')
os.popen('source ~/.bash_aliases')# maybe I should call some other unalias commands there
ご覧のとおり、問題があります。スクリプトが実行されるとos.system('source ~/.bash_aliases')
、最初にサブシェルを開いてコマンドを実行するため、ソース操作は親シェルではなくサブシェルでのみ有効になり、コマンドが終了してサブシェルが閉じられます。これは、os.system('source ~/.bash_aliases')
行ったことは単なる無駄であることを意味します。