Files
runner-images/images/ubuntu/scripts/helpers/etc-environment.sh
T

90 lines
3.2 KiB
Bash
Raw Normal View History

2020-10-24 23:30:27 +07:00
#!/bin/bash -e
2020-04-10 14:20:16 +05:00
################################################################################
## File: etc-environment.sh
## Desc: Helper functions for source and modify /etc/environment
################################################################################
# NB: sed expression use '%' as a delimiter in order to simplify handling
2025-01-23 02:20:34 +09:00
# values containing slashes (i.e. directory path)
2020-04-10 14:20:16 +05:00
# The values containing '%' will break the functions
2023-12-26 12:50:52 +01:00
get_etc_environment_variable() {
local variable_name=$1
# remove `variable_name=` and possible quotes from the line
grep "^${variable_name}=" /etc/environment | sed -E "s%^${variable_name}=\"?([^\"]+)\"?.*$%\1%"
2020-04-10 14:20:16 +05:00
}
2023-12-26 12:50:52 +01:00
add_etc_environment_variable() {
local variable_name=$1
local variable_value=$2
2020-04-10 14:20:16 +05:00
echo "${variable_name}=${variable_value}" | sudo tee -a /etc/environment
2020-04-10 14:20:16 +05:00
}
2023-12-26 12:50:52 +01:00
replace_etc_environment_variable() {
local variable_name=$1
local variable_value=$2
2020-04-10 14:20:16 +05:00
2025-12-12 15:03:34 -05:00
# modify /etc/environment in place by replacing a string that begins with variable_name
sudo sed -i -e "s%^${variable_name}=.*$%${variable_name}=${variable_value}%" /etc/environment
2020-04-10 14:20:16 +05:00
}
2023-12-26 12:50:52 +01:00
set_etc_environment_variable() {
local variable_name=$1
local variable_value=$2
2020-04-10 14:20:16 +05:00
2023-12-26 12:50:52 +01:00
if grep "^${variable_name}=" /etc/environment > /dev/null; then
replace_etc_environment_variable $variable_name $variable_value
2020-04-10 14:20:16 +05:00
else
2023-12-26 12:50:52 +01:00
add_etc_environment_variable $variable_name $variable_value
2020-04-10 14:20:16 +05:00
fi
}
2023-12-26 12:50:52 +01:00
prepend_etc_environment_variable() {
local variable_name=$1
local element=$2
# TODO: handle the case if the variable does not exist
2023-12-26 12:50:52 +01:00
existing_value=$(get_etc_environment_variable "${variable_name}")
set_etc_environment_variable "${variable_name}" "${element}:${existing_value}"
}
2023-12-26 12:50:52 +01:00
append_etc_environment_variable() {
local variable_name=$1
local element=$2
# TODO: handle the case if the variable does not exist
2023-12-26 12:50:52 +01:00
existing_value=$(get_etc_environment_variable "${variable_name}")
set_etc_environment_variable "${variable_name}" "${existing_value}:${element}"
}
2023-12-26 12:50:52 +01:00
prepend_etc_environment_path() {
local element=$1
2023-12-26 12:50:52 +01:00
prepend_etc_environment_variable PATH "${element}"
2020-04-10 14:20:16 +05:00
}
2023-12-26 12:50:52 +01:00
append_etc_environment_path() {
local element=$1
2023-12-26 12:50:52 +01:00
append_etc_environment_variable PATH "${element}"
2020-04-28 07:26:29 +00:00
}
2020-04-10 14:20:16 +05:00
# Process /etc/environment as if it were shell script with `export VAR=...` expressions
# The PATH variable is handled specially in order to do not override the existing PATH
# variable. The value of PATH variable read from /etc/environment is added to the end
# of value of the exiting PATH variable exactly as it would happen with real PAM app read
# /etc/environment
#
# TODO: there might be the others variables to be processed in the same way as "PATH" variable
# ie MANPATH, INFOPATH, LD_*, etc. In the current implementation the values from /etc/environment
2020-04-10 14:20:16 +05:00
# replace the values of the current environment
2023-12-26 12:50:52 +01:00
reload_etc_environment() {
2025-12-12 15:03:34 -05:00
# add `export ` to every variable of /etc/environment except PATH and eval the result shell script
2020-04-10 14:20:16 +05:00
eval $(grep -v '^PATH=' /etc/environment | sed -e 's%^%export %')
# handle PATH specially
2023-12-26 12:50:52 +01:00
etc_path=$(get_etc_environment_variable PATH)
2020-04-10 14:20:16 +05:00
export PATH="$PATH:$etc_path"
}