Created
June 14, 2012 17:02
-
-
Save aji/2931493 to your computer and use it in GitHub Desktop.
askenv -- sudo-esque method of temporarily setting environment variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import sys, os | |
| import getpass | |
| import copy | |
| if len(sys.argv) <3: | |
| print('usage: {0} =VARIABLE [=VARIABLE ...] CMD [ARGS ...]'.format(sys.argv[0])) | |
| print('Prompts for each VARIABLE in succession and executes CMD') | |
| sys.exit(1) | |
| keepenv = ['HOME', 'PATH', 'TERM', 'USER', 'DISPLAY', 'LANG', 'PS1'] | |
| env = copy.deepcopy(os.environ) | |
| for key in os.environ: | |
| if not key in keepenv: | |
| del env[key] | |
| for i in range(len(sys.argv[1:])): | |
| if sys.argv[i+1][0] == '=': | |
| env[sys.argv[i+1][1:]] = getpass.getpass(sys.argv[i+1][1:] + ': ') | |
| else: | |
| os.execvpe(sys.argv[i+1], sys.argv[i+1:], env) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [69] aiadicicco@sphinx askenv $ python askenv.py =FOO =BAR sh | |
| FOO: | |
| BAR: | |
| [1] aiadicicco@sphinx askenv $ export | |
| export BAR="this is bar" | |
| export DISPLAY=":0.0" | |
| export FOO="this is foo" | |
| export HOME="/home/aiadicicco" | |
| export LANG="en_US.UTF-8" | |
| export OLDPWD | |
| export PATH="/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" | |
| export PS1="\\[\\e[1m\\][\\#] \\u@\\h \\W \\\$ \\[\\e[0m\\]" | |
| export PWD="/home/aiadicicco/work/sandbox/askenv" | |
| export SHLVL="1" | |
| export TERM="screen" | |
| export USER="aiadicicco" | |
| [2] aiadicicco@sphinx askenv $ exit | |
| [70] aiadicicco@sphinx askenv $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment