Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify /etc/environment from a bash script

Currently, I'm trying to automate a procedure we use at work. Whenever we install Oracle's JDK, we need to manually add it to our global PATH variable. Here's an excerpt from the procedure :

sudo vi /etc/environment
add this at the beginning of the PATH : "/opt/jdk1.6.0_45/bin:"

Here is the content of /etc/environment on my computer :

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

Here is what it will look like after its modification :

PATH="/opt/jdk1.6.0_45/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

Do not forget that this file is not a script, but rather a file containing KEY=VALUES. This file stores the system-wide locale and path settings.

My question is how can I add a new path to the PATH variable from /etc/environment without involving any manual operation, preferably using only a bash script. Additionnaly, I would like to avoid seeing my JDK path added more than once if I run the resulting script twice.

like image 627
slaadvak Avatar asked Mar 19 '23 06:03

slaadvak


1 Answers

You can do this using sed to first delete and then insert the jdk path:

#!/bin/bash
sed -e 's|/opt/jdk1.6.0_45/bin:||g' -i /etc/environment 
sed -e 's|PATH="\(.*\)"|PATH="/opt/jdk1.6.0_45/bin:\1"|g' -i /etc/environment
like image 107
Symfrog Avatar answered Mar 26 '23 02:03

Symfrog