Editing
Software Development Platforms
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Scripts to Set Environment Variables== If you want to use different versions of installed software, or not have everything on your path all the time, it is convenient to use scripts to modify the environment variables. For convenience, here is a script that provides some useful functions for modifying paths. I name it setenv_functions.sh and put it in /usr/local/bin. ---- <source lang=bash> #!/bin/bash # # setenv_functions.sh # # Eric Nodwell, January 2011 # # Provides functions useful for modifying system paths. # # You call this from other bach scripts like this: # # source /usr/local/bin/setenv_functions.sh # # Then you can make use of the functions defined below. # # Example usage: add_path PATH /usr/my/path # function add_path { echo -e "Adding to ${1}: \033[1;34m${2}\033[0m" eval "OLDLIST=\"\$$1\"" if [ -n "${OLDLIST}" ] ; then eval "$1=\"$2:\$$1\"" else eval "$1=\"$2\"" fi eval "export $1" } # Example usage: remove_path PATH /usr/my/path # # The second argument can be a regular expression for pattern matching. # function remove_path { eval "OLDLIST=\"\$$1\"" REMOVALLIST=$( echo ${OLDLIST} | tr -s ":" "\n" | grep -wE "${2}" | tr -s "\n" ":" | sed "s/:$//" ) if [ -n "${REMOVALLIST}" ] ; then echo -e "Removing from ${1}: \033[1;31m${REMOVALLIST}\033[0m" NEWLIST=$( echo ${OLDLIST} | tr -s ":" "\n" | grep -vwE "${2}" | tr -s "\n" ":" | sed "s/:$//" ) eval "$1=\"$NEWLIST\"" eval "export $1" fi } # Example usage: check_path PATH /usr/my/path # # The second argument can be a regular expression for pattern matching. # function check_path { eval "OLDLIST=\"\$$1\"" CHECKLIST=$( echo ${OLDLIST} | tr -s ":" "\n" | grep -wE "${2}" | tr -s "\n" ":" | sed "s/:$//" ) test -n "${CHECKLIST}" return $? } # Example usage: add_path PATH /usr/my/path # function add_path { echo -e "Adding to ${1}: \033[1;34m${2}\033[0m" eval "OLDLIST=\"\$$1\"" if [ -n "${OLDLIST}" ] ; then eval "$1=\"$2:\$$1\"" else eval "$1=\"$2\"" fi eval "export $1" } # Example usage: remove_path PATH /usr/my/path # # The second argument can be a regular expression for pattern matching. # function remove_path { eval "OLDLIST=\"\$$1\"" REMOVALLIST=$( echo ${OLDLIST} | tr -s ":" "\n" | grep -wE "${2}" | tr -s "\n" ":" | sed "s/:$//" ) if [ -n "${REMOVALLIST}" ] ; then echo -e "Removing from ${1}: \033[1;31m${REMOVALLIST}\033[0m" NEWLIST=$( echo ${OLDLIST} | tr -s ":" "\n" | grep -vwE "${2}" | tr -s "\n" ":" | sed "s/:$//" ) eval "$1=\"$NEWLIST\"" eval "export $1" fi } # Example usage: check_path PATH /usr/my/path # # The second argument can be a regular expression for pattern matching. # function check_path { eval "OLDLIST=\"\$$1\"" CHECKLIST=$( echo ${OLDLIST} | tr -s ":" "\n" | grep -wE "${2}" | tr -s "\n" ":" | sed "s/:$//" ) test -n "${CHECKLIST}" return $? } </source> ---- There will be several examples below making use of this script. See for example [[#asciidoc|asciidoc]] , where a script /opt/asciidoc-8.6.3/setenv.sh is created . You use it like this: source /opt/asciidoc-8.6.3/setenv.sh The output will look something like: Adding to DYLD_LIBRARY_PATH: /opt/boost-1.45.0/lib Adding to PATH: /opt/source-highlight-3.1.4/bin Adding to DYLD_LIBRARY_PATH: /opt/source-highlight-3.1.4/lib Adding to PATH: /opt/dblatex-0.3/bin Adding to PYTHONPATH: /opt/dblatex-0.3/lib/python2.7/site-packages Adding to PATH: /opt/asciidoc-8.6.3/bin
Summary:
Please note that all contributions to bone.wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BoneWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
associated-pages
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Documents
Recent changes
Help
Tools
What links here
Related changes
Page information