The user types 'sudo python helper.py' to run my file. I want the program to create an alias so the user only has to type 'helper'. So, when the python script runs I have it create an alias (after checking if the alias already exists).
lStr= 'alias helper=\'sudo python "'+os.path.abspath(__file__)+'"\''
print lStr
subprocess.Popen(lStr,shell=True)
But when the program exits, the alias still doesn't exist and I don't know why not. If I manually copy the result of the print statement into bash, then, the alias is created properly. It doesn't work when it is called from within the python program though. Any ideas?
Because my program has a substantial amount of interaction, I can't use the answer given at Use python to dynamically create bash aliases (eval "$(python ...)"
), because that would be capturing menu text and other content involved in user interaction.
I can (and do) update ~/.bash_aliases
, so users already get new aliases the next time they open a shell -- but is there a way to have more immediate effect even when a program interacts with the user?