Created
April 4, 2024 03:56
-
-
Save tgran2028/68ca73729d09abc4ab7a44f05949bf05 to your computer and use it in GitHub Desktop.
Script for making updates to /etc/environment file. Includes various safeguards, and records of all changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define the name of the revised environment file. | |
REVISED_ENV_FILE='e.sh' | |
# Check if REVISED_ENV_FILE exists, if not, execute the following commands. | |
if [ ! -f "$REVISED_ENV_FILE" ]; then | |
# Copy the default system environment file to the revised file. | |
cp /etc/environment "$REVISED_ENV_FILE" | |
# Back up the revised file as environment.bak with sudo privileges. | |
sudo cp "$REVISED_ENV_FILE" environment.bak | |
# Create an additional backup with a dot prefix and gzip it. | |
sudo cp environment.bak .environment.bak | |
sudo gzip .environment.bak | |
# Set read-only permissions for environment.bak. | |
sudo chmod 444 environment.bak | |
fi | |
# Assign the current directory of this script to 'HERE' variable. | |
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
# Navigate to the directory stored in 'HERE' or exit with an error code. | |
cd "$HERE" || exit 1 | |
# Store the current date and time as a timestamp. | |
TIMESTAMP="$(date +%s)" | |
# Create a timestamped backup of current /etc/environment in /tmp. | |
sudo cp /etc/environment "/tmp/etc.environment.bak.${TIMESTAMP}" | |
# Ensure the data directory exists and define ENV_FILE path using TIMESTAMP. | |
mkdir -p ./data | |
ENV_FILE="./data/env.${TIMESTAMP}" | |
# Copy the revised environment file to a new file with 'new' suffix. | |
sudo cp "$REVISED_ENV_FILE" "$ENV_FILE.new" | |
# Create an old backup of the environment file with 'old' suffix and set its permissions to read-only. | |
sudo cp /etc/environment "$ENV_FILE.old" && sudo chmod 444 "$ENV_FILE.old" | |
# Replace the current system environment file with our revised file. | |
sudo cp -fv "$ENV_FILE.new" /etc/environment | |
# Restore system environment file's permissions to owner read/write and group/others read. | |
sudo chmod 644 /etc/environment | |
# Set the revised file's permissions to read-only. | |
sudo chmod 444 "$ENV_FILE.new" | |
# Print out section headers and content of the old, new, and system environment files using bat command. | |
echo ' | |
----------------------------------------------- | |
- old - | |
-----------------------------------------------' | |
bat -l sh -P --plain -f "$ENV_FILE.old" | |
echo ' | |
----------------------------------------------- | |
- new - | |
-----------------------------------------------' | |
bat -l sh -P --plain -f "$ENV_FILE.new" | |
echo ' | |
----------------------------------------------- | |
- /etc/environment - | |
-----------------------------------------------' | |
bat -l sh -P --plain -f /etc/environment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment