sync-bashrc-all.sh
· 3.6 KiB · Bash
Eredeti
#!/bin/bash
#
# sync-bashrc-all.sh (v3)
#
# 0. Pastikan dependency vivid + fastfetch tersedia
# 1. Sinkronkan /etc/skel/.bashrc
# 2. Sinkronkan /etc/shell-prompt.sh
# 3. Sinkronkan /etc/fastfetch/config.jsonc
# 4. Terapkan .bashrc ke semua user existing di /home
# 5. Terapkan .bashrc yang sama ke root
#
# Jalankan: sudo bash sync-bashrc-all.sh
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
BASHRC_URL="https://gist.darto.id/dartokloning/866220d4bda046fe93508ed50dac1965/download/HEAD/.bashrc"
PROMPT_URL="https://gist.darto.id/dartokloning/def50cb1b3024c3b8683960c873caf34/download/HEAD/shell-prompt.sh"
FASTFETCH_CONFIG_URL="https://gist.darto.id/dartokloning/655822e458964d0a93085c205506443f/download/HEAD/config.jsonc"
SKEL_BASHRC="/etc/skel/.bashrc"
PROMPT_FILE="/etc/shell-prompt.sh"
FASTFETCH_CONFIG_FILE="/etc/fastfetch/config.jsonc"
if [ "$(id -u)" -ne 0 ]; then
echo "Skrip ini harus dijalankan sebagai root (pakai sudo)." >&2
exit 1
fi
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
fetch() {
local url="$1" dest="$2"
if ! wget -q -O "$dest" "$url"; then
echo "Error: gagal download $url (cek koneksi internet)." >&2
exit 1
fi
}
sync_file() {
local url="$1" dest="$2" label="$3" mode="${4:-}"
local tmp="$TMP_DIR/$(basename "$dest")"
fetch "$url" "$tmp"
mkdir -p "$(dirname "$dest")"
if [ ! -f "$dest" ] || ! cmp -s "$tmp" "$dest"; then
cp "$tmp" "$dest"
[ -n "$mode" ] && chmod "$mode" "$dest"
echo " -> $label diperbarui."
return 0
else
echo " -> $label sudah sesuai, tidak ada perubahan."
return 1
fi
}
apply_bashrc() {
local user="$1" home_dir="$2"
local target="$home_dir/.bashrc"
local backup="$home_dir/.bashrc.original"
if [ -f "$target" ] && cmp -s "$SKEL_BASHRC" "$target"; then
echo "[SKIP] $user - .bashrc sudah sesuai template"
return
fi
if [ -f "$target" ] && [ ! -f "$backup" ]; then
cp -a "$target" "$backup"
echo "[BACKUP] $user - .bashrc lama disimpan sebagai .bashrc.original"
fi
cp "$SKEL_BASHRC" "$target"
if [ "$user" = "root" ]; then
chown root:root "$target"
else
chown "$user":"$(id -gn "$user")" "$target"
fi
echo "[APPLY] $user - .bashrc diperbarui dari template skel"
}
echo "== 0. Pastikan dependency (vivid, fastfetch) terinstall =="
need_install=()
command -v vivid >/dev/null 2>&1 || need_install+=("vivid")
command -v fastfetch >/dev/null 2>&1 || need_install+=("fastfetch")
if [ "${#need_install[@]}" -gt 0 ]; then
echo " -> Menginstall: ${need_install[*]}"
apt-get update -qq
apt-get install -y "${need_install[@]}"
else
echo " -> vivid & fastfetch sudah terinstall, skip."
fi
echo
echo "== 1. Cek /etc/skel/.bashrc =="
sync_file "$BASHRC_URL" "$SKEL_BASHRC" "$SKEL_BASHRC" || true
echo
echo "== 2. Cek /etc/shell-prompt.sh =="
sync_file "$PROMPT_URL" "$PROMPT_FILE" "$PROMPT_FILE" "644" || true
echo
echo "== 3. Cek /etc/fastfetch/config.jsonc =="
sync_file "$FASTFETCH_CONFIG_URL" "$FASTFETCH_CONFIG_FILE" "$FASTFETCH_CONFIG_FILE" "644" || true
echo
echo "== 4. Terapkan template ke user existing di /home =="
for home_dir in /home/*/; do
[ -d "$home_dir" ] || continue
home_dir="${home_dir%/}"
user="$(basename "$home_dir")"
id "$user" >/dev/null 2>&1 || continue
apply_bashrc "$user" "$home_dir"
done
echo
echo "== 5. Terapkan template ke root =="
apply_bashrc "root" "/root"
echo
echo "Selesai."
echo "User yang sedang login perlu 'source ~/.bashrc' atau login ulang."
echo "Untuk menguji root prompt: sudo -i"
| 1 | #!/bin/bash |
| 2 | # |
| 3 | # sync-bashrc-all.sh (v3) |
| 4 | # |
| 5 | # 0. Pastikan dependency vivid + fastfetch tersedia |
| 6 | # 1. Sinkronkan /etc/skel/.bashrc |
| 7 | # 2. Sinkronkan /etc/shell-prompt.sh |
| 8 | # 3. Sinkronkan /etc/fastfetch/config.jsonc |
| 9 | # 4. Terapkan .bashrc ke semua user existing di /home |
| 10 | # 5. Terapkan .bashrc yang sama ke root |
| 11 | # |
| 12 | # Jalankan: sudo bash sync-bashrc-all.sh |
| 13 | |
| 14 | set -euo pipefail |
| 15 | export DEBIAN_FRONTEND=noninteractive |
| 16 | |
| 17 | BASHRC_URL="https://gist.darto.id/dartokloning/866220d4bda046fe93508ed50dac1965/download/HEAD/.bashrc" |
| 18 | PROMPT_URL="https://gist.darto.id/dartokloning/def50cb1b3024c3b8683960c873caf34/download/HEAD/shell-prompt.sh" |
| 19 | FASTFETCH_CONFIG_URL="https://gist.darto.id/dartokloning/655822e458964d0a93085c205506443f/download/HEAD/config.jsonc" |
| 20 | |
| 21 | SKEL_BASHRC="/etc/skel/.bashrc" |
| 22 | PROMPT_FILE="/etc/shell-prompt.sh" |
| 23 | FASTFETCH_CONFIG_FILE="/etc/fastfetch/config.jsonc" |
| 24 | |
| 25 | if [ "$(id -u)" -ne 0 ]; then |
| 26 | echo "Skrip ini harus dijalankan sebagai root (pakai sudo)." >&2 |
| 27 | exit 1 |
| 28 | fi |
| 29 | |
| 30 | TMP_DIR="$(mktemp -d)" |
| 31 | trap 'rm -rf "$TMP_DIR"' EXIT |
| 32 | |
| 33 | fetch() { |
| 34 | local url="$1" dest="$2" |
| 35 | if ! wget -q -O "$dest" "$url"; then |
| 36 | echo "Error: gagal download $url (cek koneksi internet)." >&2 |
| 37 | exit 1 |
| 38 | fi |
| 39 | } |
| 40 | |
| 41 | sync_file() { |
| 42 | local url="$1" dest="$2" label="$3" mode="${4:-}" |
| 43 | local tmp="$TMP_DIR/$(basename "$dest")" |
| 44 | |
| 45 | fetch "$url" "$tmp" |
| 46 | mkdir -p "$(dirname "$dest")" |
| 47 | |
| 48 | if [ ! -f "$dest" ] || ! cmp -s "$tmp" "$dest"; then |
| 49 | cp "$tmp" "$dest" |
| 50 | [ -n "$mode" ] && chmod "$mode" "$dest" |
| 51 | echo " -> $label diperbarui." |
| 52 | return 0 |
| 53 | else |
| 54 | echo " -> $label sudah sesuai, tidak ada perubahan." |
| 55 | return 1 |
| 56 | fi |
| 57 | } |
| 58 | |
| 59 | apply_bashrc() { |
| 60 | local user="$1" home_dir="$2" |
| 61 | local target="$home_dir/.bashrc" |
| 62 | local backup="$home_dir/.bashrc.original" |
| 63 | |
| 64 | if [ -f "$target" ] && cmp -s "$SKEL_BASHRC" "$target"; then |
| 65 | echo "[SKIP] $user - .bashrc sudah sesuai template" |
| 66 | return |
| 67 | fi |
| 68 | |
| 69 | if [ -f "$target" ] && [ ! -f "$backup" ]; then |
| 70 | cp -a "$target" "$backup" |
| 71 | echo "[BACKUP] $user - .bashrc lama disimpan sebagai .bashrc.original" |
| 72 | fi |
| 73 | |
| 74 | cp "$SKEL_BASHRC" "$target" |
| 75 | |
| 76 | if [ "$user" = "root" ]; then |
| 77 | chown root:root "$target" |
| 78 | else |
| 79 | chown "$user":"$(id -gn "$user")" "$target" |
| 80 | fi |
| 81 | |
| 82 | echo "[APPLY] $user - .bashrc diperbarui dari template skel" |
| 83 | } |
| 84 | |
| 85 | echo "== 0. Pastikan dependency (vivid, fastfetch) terinstall ==" |
| 86 | need_install=() |
| 87 | command -v vivid >/dev/null 2>&1 || need_install+=("vivid") |
| 88 | command -v fastfetch >/dev/null 2>&1 || need_install+=("fastfetch") |
| 89 | |
| 90 | if [ "${#need_install[@]}" -gt 0 ]; then |
| 91 | echo " -> Menginstall: ${need_install[*]}" |
| 92 | apt-get update -qq |
| 93 | apt-get install -y "${need_install[@]}" |
| 94 | else |
| 95 | echo " -> vivid & fastfetch sudah terinstall, skip." |
| 96 | fi |
| 97 | |
| 98 | echo |
| 99 | echo "== 1. Cek /etc/skel/.bashrc ==" |
| 100 | sync_file "$BASHRC_URL" "$SKEL_BASHRC" "$SKEL_BASHRC" || true |
| 101 | |
| 102 | echo |
| 103 | echo "== 2. Cek /etc/shell-prompt.sh ==" |
| 104 | sync_file "$PROMPT_URL" "$PROMPT_FILE" "$PROMPT_FILE" "644" || true |
| 105 | |
| 106 | echo |
| 107 | echo "== 3. Cek /etc/fastfetch/config.jsonc ==" |
| 108 | sync_file "$FASTFETCH_CONFIG_URL" "$FASTFETCH_CONFIG_FILE" "$FASTFETCH_CONFIG_FILE" "644" || true |
| 109 | |
| 110 | echo |
| 111 | echo "== 4. Terapkan template ke user existing di /home ==" |
| 112 | for home_dir in /home/*/; do |
| 113 | [ -d "$home_dir" ] || continue |
| 114 | home_dir="${home_dir%/}" |
| 115 | user="$(basename "$home_dir")" |
| 116 | |
| 117 | id "$user" >/dev/null 2>&1 || continue |
| 118 | apply_bashrc "$user" "$home_dir" |
| 119 | done |
| 120 | |
| 121 | echo |
| 122 | echo "== 5. Terapkan template ke root ==" |
| 123 | apply_bashrc "root" "/root" |
| 124 | |
| 125 | echo |
| 126 | echo "Selesai." |
| 127 | echo "User yang sedang login perlu 'source ~/.bashrc' atau login ulang." |
| 128 | echo "Untuk menguji root prompt: sudo -i" |