utils/global-env.sh
2024-08-31 02:26:12 +05:00

39 lines
978 B
Bash

#!/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
trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' ERR
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}"
trap - ERR
echo "Environment variable ${ENV_VAR_NAME} set to:"
printenv "${ENV_VAR_NAME}"