utils/global-env.sh

39 lines
978 B
Bash
Raw Normal View History

2024-07-27 04:15:43 +05:00
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo -e "\033[31mThis script requires superuser rights.\033[0m"
exit 0
fi
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Please provide both the VAR_NAME and VAR_VALUE as arguments."
exit 1
fi
2024-08-31 02:26:12 +05:00
trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' ERR
2024-07-27 04:15:43 +05:00
set -e
export DEBIAN_FRONTEND=noninteractive
ENV_VAR_NAME=$1
ENV_VAR_VALUE=$2
# Create a file if it does not exist
if [ ! -f /etc/environment ]; then
touch /etc/environment
fi
# Checking if a variable already exists
if grep -q "^${ENV_VAR_NAME}=" /etc/environment; then
# If the variable exists, update its value
sed -i "s/^${ENV_VAR_NAME}=.*/${ENV_VAR_NAME}=${ENV_VAR_VALUE}/" /etc/environment
else
# If the variable does not exist, add it
echo "${ENV_VAR_NAME}=${ENV_VAR_VALUE}" | tee -a /etc/environment
fi
export "${ENV_VAR_NAME}=${ENV_VAR_VALUE}"
2024-08-31 02:26:12 +05:00
trap - ERR
2024-07-27 04:15:43 +05:00
echo "Environment variable ${ENV_VAR_NAME} set to:"
printenv "${ENV_VAR_NAME}"