sync-bashrc-all.sh
· 2.9 KiB · Bash
Raw
#!/bin/bash
#
# sync-bashrc-all.sh
#
# Skrip all-in-one:
# 1. Cek /etc/skel/.bashrc vs versi remote (gist) -> update kalau beda
# 2. Cek /etc/shell-prompt.sh vs versi remote (gist) -> download/update kalau
# belum ada atau beda
# 3. Terapkan /etc/skel/.bashrc ke semua user existing di /home yang
# .bashrc-nya belum sesuai
#
# Jalankan sebagai root: sudo bash sync-bashrc-all.sh
set -euo pipefail
BASHRC_URL="https://gist.darto.id/dartokloning/866220d4bda046fe93508ed50dac1965/download/HEAD/.bashrc"
PROMPT_URL="https://gist.darto.id/dartokloning/def50cb1b3024c3b8683960c873caf34/download/HEAD/shell-prompt.sh"
SKEL_BASHRC="/etc/skel/.bashrc"
PROMPT_FILE="/etc/shell-prompt.sh"
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() {
# fetch URL DEST_TMPFILE -> berhenti dengan pesan jelas kalau gagal
local url="$1" dest="$2"
if ! wget -q -O "$dest" "$url"; then
echo "Error: gagal download $url (cek koneksi internet)." >&2
exit 1
fi
}
echo "== 1. Cek /etc/skel/.bashrc =="
tmp_bashrc="$TMP_DIR/.bashrc"
fetch "$BASHRC_URL" "$tmp_bashrc"
if [ ! -f "$SKEL_BASHRC" ] || ! cmp -s "$tmp_bashrc" "$SKEL_BASHRC"; then
cp "$tmp_bashrc" "$SKEL_BASHRC"
echo " -> $SKEL_BASHRC diperbarui dari gist."
else
echo " -> $SKEL_BASHRC sudah sesuai dengan gist, tidak ada perubahan."
fi
echo
echo "== 2. Cek /etc/shell-prompt.sh =="
tmp_prompt="$TMP_DIR/shell-prompt.sh"
fetch "$PROMPT_URL" "$tmp_prompt"
if [ ! -f "$PROMPT_FILE" ] || ! cmp -s "$tmp_prompt" "$PROMPT_FILE"; then
cp "$tmp_prompt" "$PROMPT_FILE"
chmod 644 "$PROMPT_FILE"
echo " -> $PROMPT_FILE diperbarui dari gist."
else
echo " -> $PROMPT_FILE sudah sesuai dengan gist, tidak ada perubahan."
fi
echo
echo "== 3. Terapkan template ke user existing di /home =="
for home_dir in /home/*/; do
home_dir="${home_dir%/}"
user="$(basename "$home_dir")"
# skip kalau bukan user valid (mis. lost+found, direktori nyasar)
if ! id "$user" >/dev/null 2>&1; then
continue
fi
target="$home_dir/.bashrc"
# kalau .bashrc user sudah identik dengan template skel, skip
if [ -f "$target" ] && cmp -s "$SKEL_BASHRC" "$target"; then
echo "[SKIP] $user - .bashrc sudah sesuai template"
continue
fi
# backup .bashrc lama, hanya sekali (tidak menimpa backup yang sudah ada)
if [ -f "$target" ] && [ ! -f "$home_dir/.bashrc.original" ]; then
cp -a "$target" "$home_dir/.bashrc.original"
echo "[BACKUP] $user - .bashrc lama disimpan sebagai .bashrc.original"
fi
cp "$SKEL_BASHRC" "$target"
chown "$user":"$user" "$target"
echo "[APPLY] $user - .bashrc diperbarui dari template skel"
done
echo
echo "Selesai."
echo "Catatan: user yang sedang login perlu 'source ~/.bashrc' atau login ulang"
echo "agar shell aktifnya ikut memakai .bashrc yang baru."
| 1 | #!/bin/bash |
| 2 | # |
| 3 | # sync-bashrc-all.sh |
| 4 | # |
| 5 | # Skrip all-in-one: |
| 6 | # 1. Cek /etc/skel/.bashrc vs versi remote (gist) -> update kalau beda |
| 7 | # 2. Cek /etc/shell-prompt.sh vs versi remote (gist) -> download/update kalau |
| 8 | # belum ada atau beda |
| 9 | # 3. Terapkan /etc/skel/.bashrc ke semua user existing di /home yang |
| 10 | # .bashrc-nya belum sesuai |
| 11 | # |
| 12 | # Jalankan sebagai root: sudo bash sync-bashrc-all.sh |
| 13 | |
| 14 | set -euo pipefail |
| 15 | |
| 16 | BASHRC_URL="https://gist.darto.id/dartokloning/866220d4bda046fe93508ed50dac1965/download/HEAD/.bashrc" |
| 17 | PROMPT_URL="https://gist.darto.id/dartokloning/def50cb1b3024c3b8683960c873caf34/download/HEAD/shell-prompt.sh" |
| 18 | |
| 19 | SKEL_BASHRC="/etc/skel/.bashrc" |
| 20 | PROMPT_FILE="/etc/shell-prompt.sh" |
| 21 | |
| 22 | if [ "$(id -u)" -ne 0 ]; then |
| 23 | echo "Skrip ini harus dijalankan sebagai root (pakai sudo)." >&2 |
| 24 | exit 1 |
| 25 | fi |
| 26 | |
| 27 | TMP_DIR="$(mktemp -d)" |
| 28 | trap 'rm -rf "$TMP_DIR"' EXIT |
| 29 | |
| 30 | fetch() { |
| 31 | # fetch URL DEST_TMPFILE -> berhenti dengan pesan jelas kalau gagal |
| 32 | local url="$1" dest="$2" |
| 33 | if ! wget -q -O "$dest" "$url"; then |
| 34 | echo "Error: gagal download $url (cek koneksi internet)." >&2 |
| 35 | exit 1 |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | echo "== 1. Cek /etc/skel/.bashrc ==" |
| 40 | tmp_bashrc="$TMP_DIR/.bashrc" |
| 41 | fetch "$BASHRC_URL" "$tmp_bashrc" |
| 42 | |
| 43 | if [ ! -f "$SKEL_BASHRC" ] || ! cmp -s "$tmp_bashrc" "$SKEL_BASHRC"; then |
| 44 | cp "$tmp_bashrc" "$SKEL_BASHRC" |
| 45 | echo " -> $SKEL_BASHRC diperbarui dari gist." |
| 46 | else |
| 47 | echo " -> $SKEL_BASHRC sudah sesuai dengan gist, tidak ada perubahan." |
| 48 | fi |
| 49 | |
| 50 | echo |
| 51 | echo "== 2. Cek /etc/shell-prompt.sh ==" |
| 52 | tmp_prompt="$TMP_DIR/shell-prompt.sh" |
| 53 | fetch "$PROMPT_URL" "$tmp_prompt" |
| 54 | |
| 55 | if [ ! -f "$PROMPT_FILE" ] || ! cmp -s "$tmp_prompt" "$PROMPT_FILE"; then |
| 56 | cp "$tmp_prompt" "$PROMPT_FILE" |
| 57 | chmod 644 "$PROMPT_FILE" |
| 58 | echo " -> $PROMPT_FILE diperbarui dari gist." |
| 59 | else |
| 60 | echo " -> $PROMPT_FILE sudah sesuai dengan gist, tidak ada perubahan." |
| 61 | fi |
| 62 | |
| 63 | echo |
| 64 | echo "== 3. Terapkan template ke user existing di /home ==" |
| 65 | for home_dir in /home/*/; do |
| 66 | home_dir="${home_dir%/}" |
| 67 | user="$(basename "$home_dir")" |
| 68 | |
| 69 | # skip kalau bukan user valid (mis. lost+found, direktori nyasar) |
| 70 | if ! id "$user" >/dev/null 2>&1; then |
| 71 | continue |
| 72 | fi |
| 73 | |
| 74 | target="$home_dir/.bashrc" |
| 75 | |
| 76 | # kalau .bashrc user sudah identik dengan template skel, skip |
| 77 | if [ -f "$target" ] && cmp -s "$SKEL_BASHRC" "$target"; then |
| 78 | echo "[SKIP] $user - .bashrc sudah sesuai template" |
| 79 | continue |
| 80 | fi |
| 81 | |
| 82 | # backup .bashrc lama, hanya sekali (tidak menimpa backup yang sudah ada) |
| 83 | if [ -f "$target" ] && [ ! -f "$home_dir/.bashrc.original" ]; then |
| 84 | cp -a "$target" "$home_dir/.bashrc.original" |
| 85 | echo "[BACKUP] $user - .bashrc lama disimpan sebagai .bashrc.original" |
| 86 | fi |
| 87 | |
| 88 | cp "$SKEL_BASHRC" "$target" |
| 89 | chown "$user":"$user" "$target" |
| 90 | echo "[APPLY] $user - .bashrc diperbarui dari template skel" |
| 91 | done |
| 92 | |
| 93 | echo |
| 94 | echo "Selesai." |
| 95 | echo "Catatan: user yang sedang login perlu 'source ~/.bashrc' atau login ulang" |
| 96 | echo "agar shell aktifnya ikut memakai .bashrc yang baru." |