Skip to content

Commit 75e6e00

Browse files
committed
Update | gpt-5 output 1
Packages install doesn't work.
1 parent 0990fb2 commit 75e6e00

File tree

1 file changed

+86
-82
lines changed

1 file changed

+86
-82
lines changed

.assets/new.sh

Lines changed: 86 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env bash
2-
# new.sh — version verbose pour techniciens
3-
4-
################################################################################
5-
# Options opérateur
6-
# --debug : active bash -x et logs DEBUG
7-
# --dry-run : n'exécute rien (affiche uniquement)
8-
# --quiet : réduit la verbosité à WARN/ERROR
9-
################################################################################
2+
# new-basics.sh — version verbose pour techniciens
3+
# Objectif :
4+
# - Détecte la distro et installe : gnupg{,2} lm-sensors curl wget htop nload screen vim git ncdu bpytop rsync man avahi-daemon tree dnsutils net-tools ripgrep
5+
# - fastfetch : depuis les dépôts si dispo, sinon script externe (GitHub)
6+
# - Remplace le .bashrc de l’utilisateur appelant (SUDO_USER le cas échéant)
7+
# - Timezone -> Europe/Paris
8+
# - Logs détaillés dans /var/log + affichage verbeux, options --debug --dry-run --quiet
109

1110
set -euo pipefail
1211

@@ -28,24 +27,20 @@ done
2827
_start_ts=$(date +%s)
2928
LOG_TS="$(date +%Y%m%d-%H%M%S)"
3029
LOG_DIR="/var/log"
31-
LOG_FILE="${LOG_DIR}/new.sh-${LOG_TS}.log"
30+
LOG_FILE="${LOG_DIR}/new-basics-${LOG_TS}.log"
3231
mkdir -p "$LOG_DIR"
33-
# Redirige TOUT vers tee (stdout + fichier)
3432
exec > >(tee -a "$LOG_FILE") 2>&1
3533

36-
# Couleurs
3734
BOLD="\e[1m"; DIM="\e[2m"; RED="\e[31m"; YEL="\e[33m"; GRN="\e[32m"; BLU="\e[34m"; C0="\e[0m"
38-
3935
_now() { date "+%Y-%m-%d %H:%M:%S%z"; }
4036
_since() { local s=$1; printf "%ds" $(( $(date +%s) - s )); }
4137

42-
log() { printf "%b[%s] [INFO ]%b %s\n" "$GRN" "$(_now)" "$C0" "$*"; }
43-
warn() { printf "%b[%s] [WARN ]%b %s\n" "$YEL" "$(_now)" "$C0" "$*"; }
44-
err() { printf "%b[%s] [ERROR]%b %s\n" "$RED" "$(_now)" "$C0" "$*"; }
45-
debug() { [[ $DEBUG -eq 1 ]] && printf "%b[%s] [DEBUG]%b %s\n" "$BLU" "$(_now)" "$C0" "$*"; }
46-
logq() { [[ $QUIET -eq 0 ]] && log "$@" || true; }
38+
log() { printf "%b[%s] [INFO ]%b %s\n" "$GRN" "$(_now)" "$C0" "$*"; }
39+
warn() { printf "%b[%s] [WARN ]%b %s\n" "$YEL" "$(_now)" "$C0" "$*"; }
40+
err() { printf "%b[%s] [ERROR]%b %s\n" "$RED" "$(_now)" "$C0" "$*"; }
41+
debug() { [[ $DEBUG -eq 1 ]] && printf "%b[%s] [DEBUG]%b %s\n" "$BLU" "$(_now)" "$C0" "$*"; }
42+
logq() { [[ $QUIET -eq 0 ]] && log "$@" || true; }
4743

48-
# Affiche et exécute une commande (ou simule en dry-run)
4944
run() {
5045
local desc="$1"; shift
5146
local ts=$(date +%s)
@@ -90,78 +85,80 @@ fi
9085
log "Distribution détectée: ID=${DIST_ID} | ID_LIKE=${DIST_LIKE}"
9186

9287
# ============================== Gestionnaire paquets ===========================
93-
PKG_MGR="" PKG_UPDATE="" PKG_UPGRADE="" PKG_INSTALL="" PKG_QUERY=""
88+
PKG_MGR="" PKG_UPDATE="" PKG_UPGRADE="" PKG_INSTALL="" PKG_Q_AVAIL=""
9489
case "$DIST_ID" in
95-
debian|ubuntu|linuxmint|pop)
90+
debian|ubuntu|linuxmint|pop|kali)
9691
export DEBIAN_FRONTEND=noninteractive
9792
PKG_MGR="apt"
9893
PKG_UPDATE="apt-get update -y"
9994
PKG_UPGRADE="apt-get -y full-upgrade --autoremove --purge"
10095
PKG_INSTALL="apt-get install -y"
101-
PKG_QUERY="apt-cache policy"
96+
# 0 si dispo (Candidate != (none))
97+
PKG_Q_AVAIL='sh -c "[[ \$(apt-cache policy \"$1\" 2>/dev/null | awk '\''/Candidate:/ {print \$2}'\'' ) != \"(none)\" ]]" _'
10298
;;
10399
fedora)
104100
PKG_MGR="dnf"
105101
PKG_UPDATE="dnf -y makecache"
106102
PKG_UPGRADE="dnf -y upgrade --refresh"
107103
PKG_INSTALL="dnf -y install"
108-
PKG_QUERY="dnf info"
104+
PKG_Q_AVAIL="dnf -q info"
109105
;;
110106
rhel|centos|rocky|almalinux)
111107
if command -v dnf >/dev/null 2>&1; then
112-
PKG_MGR="dnf"; PKG_UPDATE="dnf -y makecache"; PKG_UPGRADE="dnf -y upgrade --refresh"; PKG_INSTALL="dnf -y install"; PKG_QUERY="dnf info"
108+
PKG_MGR="dnf"; PKG_UPDATE="dnf -y makecache"; PKG_UPGRADE="dnf -y upgrade --refresh"; PKG_INSTALL="dnf -y install"; PKG_Q_AVAIL="dnf -q info"
113109
else
114-
PKG_MGR="yum"; PKG_UPDATE="yum -y makecache"; PKG_UPGRADE="yum -y update"; PKG_INSTALL="yum -y install"; PKG_QUERY="yum info"
110+
PKG_MGR="yum"; PKG_UPDATE="yum -y makecache"; PKG_UPGRADE="yum -y update"; PKG_INSTALL="yum -y install"; PKG_Q_AVAIL="yum -q info"
115111
fi
116112
;;
117113
arch|artix|manjaro)
118114
PKG_MGR="pacman"
119115
PKG_UPDATE="pacman -Sy --noconfirm"
120116
PKG_UPGRADE="pacman -Syu --noconfirm"
121117
PKG_INSTALL="pacman -S --noconfirm --needed"
122-
PKG_QUERY="pacman -Si"
118+
PKG_Q_AVAIL="pacman -Si"
123119
;;
124120
opensuse*|sles)
125121
PKG_MGR="zypper"
126122
PKG_UPDATE="zypper --non-interactive refresh"
127123
PKG_UPGRADE="zypper --non-interactive update"
128124
PKG_INSTALL="zypper --non-interactive install --no-confirm"
129-
PKG_QUERY="zypper info"
125+
PKG_Q_AVAIL="zypper info"
130126
;;
131127
alpine)
132128
PKG_MGR="apk"
133129
PKG_UPDATE="apk update"
134130
PKG_UPGRADE="apk upgrade"
135131
PKG_INSTALL="apk add --no-cache"
136-
PKG_QUERY="apk info -e"
132+
# 0 si apk search -x renvoie un match exact
133+
PKG_Q_AVAIL='sh -c "apk search -x \"$1\" >/dev/null 2>&1" _'
137134
;;
138135
*)
139136
case "$DIST_LIKE" in
140137
*debian*) DIST_ID="debian"; export DEBIAN_FRONTEND=noninteractive
141-
PKG_MGR="apt"; PKG_UPDATE="apt-get update -y"; PKG_UPGRADE="apt-get -y full-upgrade --autoremove --purge"; PKG_INSTALL="apt-get install -y"; PKG_QUERY="apt-cache policy" ;;
142-
*rhel*|*fedora*) DIST_ID="rhel"
138+
PKG_MGR="apt"; PKG_UPDATE="apt-get update -y"; PKG_UPGRADE="apt-get -y full-upgrade --autoremove --purge"; PKG_INSTALL="apt-get install -y"
139+
PKG_Q_AVAIL='sh -c "[[ \$(apt-cache policy \"$1\" 2>/dev/null | awk '\''/Candidate:/ {print \$2}'\'' ) != \"(none)\" ]]" _'
140+
;;
141+
*rhel*|*fedora*)
143142
if command -v dnf >/dev/null 2>&1; then
144-
PKG_MGR="dnf"; PKG_UPDATE="dnf -y makecache"; PKG_UPGRADE="dnf -y upgrade --refresh"; PKG_INSTALL="dnf -y install"; PKG_QUERY="dnf info"
143+
PKG_MGR="dnf"; PKG_UPDATE="dnf -y makecache"; PKG_UPGRADE="dnf -y upgrade --refresh"; PKG_INSTALL="dnf -y install"; PKG_Q_AVAIL="dnf -q info"
145144
else
146-
PKG_MGR="yum"; PKG_UPDATE="yum -y makecache"; PKG_UPGRADE="yum -y update"; PKG_INSTALL="yum -y install"; PKG_QUERY="yum info"
145+
PKG_MGR="yum"; PKG_UPDATE="yum -y makecache"; PKG_UPGRADE="yum -y update"; PKG_INSTALL="yum -y install"; PKG_Q_AVAIL="yum -q info"
147146
fi ;;
148147
*)
149148
err "Distribution non gérée (ID=$DIST_ID ID_LIKE=$DIST_LIKE)"; exit 1 ;;
150149
esac
151150
;;
152151
esac
153152
log "Gestionnaire de paquets: $PKG_MGR"
154-
debug "CMD update='$PKG_UPDATE' upgrade='$PKG_UPGRADE' install='$PKG_INSTALL' query='$PKG_QUERY'"
155153

156-
# ============================== Helpers paquets ================================
157154
pkg_available() {
158155
local pkg="$1"
159156
case "$PKG_MGR" in
160-
apt) $PKG_QUERY "$pkg" >/dev/null 2>&1 ;;
161-
dnf|yum) $PKG_QUERY "$pkg" >/dev/null 2>&1 ;;
162-
pacman) $PKG_QUERY "$pkg" >/dev/null 2>&1 ;;
163-
zypper) $PKG_QUERY "$pkg" >/dev/null 2>&1 ;;
164-
apk) $PKG_QUERY "$pkg" >/dev/null 2>&1 ;;
157+
apt) eval "$PKG_Q_AVAIL" -- "$pkg" ;;
158+
dnf|yum) $PKG_Q_AVAIL "$pkg" >/dev/null 2>&1 ;;
159+
pacman) $PKG_Q_AVAIL "$pkg" >/dev/null 2>&1 ;;
160+
zypper) $PKG_Q_AVAIL "$pkg" >/dev/null 2>&1 ;;
161+
apk) eval "$PKG_Q_AVAIL" -- "$pkg" ;;
165162
*) return 1 ;;
166163
esac
167164
}
@@ -196,56 +193,59 @@ run "Upgrade système" bash -c "$PKG_UPGRADE" || warn "Upgrade partielle/échou
196193
log "Section MAJ terminée (durée $(_since "$ts_update"))"
197194

198195
# ============================== Mapping paquets ===============================
199-
# Paquets communs (mappés selon distro)
200-
PKG_GNUPG=(gnupg gnupg2) # apt gère les deux; autres distros n'installeront que celui dispo
201-
PKG_LMSENS=(lm-sensors)
196+
# Liste demandée : gnupg{,2} lm-sensors curl wget htop nload screen vim git ncdu bpytop rsync man avahi-daemon tree dnsutils net-tools ripgrep
197+
PKG_GNUPG=(gnupg gnupg2)
198+
PKG_LMSENS_DEB_RPM=(lm-sensors)
199+
PKG_LMSENS_ARCH=(lm_sensors)
202200
PKG_COMMON=(curl wget htop nload screen vim git ncdu rsync tree net-tools ripgrep)
203-
PKG_MAN_DEB=(man-db)
201+
PKG_MAN_DEB=(man-db manpages)
202+
PKG_MAN_RPM=(man-db man-pages)
203+
PKG_MAN_ARCH=(man-db man-pages)
204204
PKG_DNS_DEB=(dnsutils)
205205
PKG_DNS_RPM=(bind-utils)
206-
PKG_DNS_ARCH_ALP=(bind-tools)
206+
PKG_DNS_ARCH=(bind) # Arch fournit dig/nslookup via 'bind'
207+
PKG_DNS_ALP=(bind-tools)
207208
PKG_AVAHI_DEB=(avahi-daemon)
208-
PKG_AVAHI_OTH=(avahi) # Arch/RPM/openSUSE/Alpine
209+
PKG_AVAHI_OTH=(avahi avahi-daemon) # RPM/Arch/openSUSE/Alpine (selon cas)
209210
PKG_BPY=(bpytop)
210-
PKG_BTOP=(btop)
211+
PKG_BTOP=(btop bashtop) # fallback si bpytop indispo
211212

212213
log "Sélection des paquets selon famille: $DIST_ID ($PKG_MGR)"
213-
214214
case "$PKG_MGR" in
215215
apt)
216216
install_if_exists "gnupg" "${PKG_GNUPG[@]}"
217-
install_if_exists "lm-sensors" "${PKG_LMSENS[@]}"
217+
install_if_exists "lm-sensors" "${PKG_LMSENS_DEB_RPM[@]}"
218218
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_DEB[@]}" "${PKG_DNS_DEB[@]}" "${PKG_AVAHI_DEB[@]}"
219-
# bpytop ou btop
220219
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
221220
;;
222221
dnf|yum)
223222
install_if_exists "gnupg" gnupg
224-
install_if_exists "lm-sensors" "${PKG_LMSENS[@]}"
225-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_DNS_RPM[@]}" "${PKG_AVAHI_OTH[@]}"
223+
install_if_exists "lm-sensors" "${PKG_LMSENS_DEB_RPM[@]}"
224+
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_RPM[@]}" "${PKG_AVAHI_OTH[@]}"
226225
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
227226
;;
228227
pacman)
229228
install_if_exists "gnupg" gnupg
230-
install_if_exists "lm-sensors" lm_sensors
231-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_DNS_ARCH_ALP[@]}" "${PKG_AVAHI_OTH[@]}"
229+
install_if_exists "lm-sensors" "${PKG_LMSENS_ARCH[@]}"
230+
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_ARCH[@]}" "${PKG_DNS_ARCH[@]}" "${PKG_AVAHI_OTH[@]}"
232231
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
233232
;;
234233
zypper)
235-
install_if_exists "gnupg" gpg2
236-
install_if_exists "lm-sensors" "${PKG_LMSENS[@]}"
237-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_DNS_RPM[@]}" "${PKG_AVAHI_OTH[@]}"
234+
install_if_exists "gnupg" gpg2 gnupg
235+
install_if_exists "lm-sensors" "${PKG_LMSENS_DEB_RPM[@]}"
236+
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_RPM[@]}" "${PKG_AVAHI_OTH[@]}"
238237
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
239238
;;
240239
apk)
241240
install_if_exists "gnupg" gnupg
242241
install_if_exists "lm-sensors" lm-sensors
243-
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_DNS_ARCH_ALP[@]}" "${PKG_AVAHI_OTH[@]}"
242+
install_if_exists "commun" "${PKG_COMMON[@]}" "${PKG_MAN_RPM[@]}" "${PKG_DNS_ALP[@]}" "${PKG_AVAHI_OTH[@]}"
244243
if pkg_available "${PKG_BPY[0]}"; then install_if_exists "monitoring" "${PKG_BPY[@]}"; else install_if_exists "monitoring" "${PKG_BTOP[@]}"; fi
245244
;;
246245
esac
247246

248247
# ============================== Fastfetch (repo > fallback) ====================
248+
FASTFETCH_URL="https://raw.githubusercontent.com/OverStyleFR/AutoScriptBash/main/.assets/fastfetch-install.sh"
249249
install_fastfetch() {
250250
local ts=$(date +%s)
251251
if pkg_available fastfetch; then
@@ -255,15 +255,14 @@ install_fastfetch() {
255255
return 0
256256
fi
257257
warn "fastfetch non dispo dans les dépôts — fallback script externe"
258-
local URL="https://raw.githubusercontent.com/OverStyleFR/AutoScriptBash/main/.assets/fastfetch-install.sh"
259258
if [[ $DRYRUN -eq 1 ]]; then
260-
printf "[DRYRUN] bash <(curl -fsSL %s)\n" "$URL"
259+
printf "[DRYRUN] bash <(curl -fsSL %s)\n" "$FASTFETCH_URL"
261260
return 0
262261
fi
263262
if command -v curl >/dev/null 2>&1; then
264-
run "Installer fastfetch (script)" bash -c "bash <(curl -fsSL \"$URL\")"
263+
run "Installer fastfetch (script)" bash -c "bash <(curl -fsSL \"$FASTFETCH_URL\")"
265264
elif command -v wget >/dev/null 2>&1; then
266-
run "Installer fastfetch (script)" bash -c "bash <(wget -qO- \"$URL\")"
265+
run "Installer fastfetch (script)" bash -c "bash <(wget -qO- \"$FASTFETCH_URL\")"
267266
else
268267
err "Ni curl ni wget disponibles — fastfetch non installé."
269268
return 1
@@ -276,41 +275,47 @@ install_fastfetch || warn "Installation fastfetch échouée (script)."
276275
if command -v timedatectl >/dev/null 2>&1; then
277276
run "Réglage timezone Europe/Paris" timedatectl set-timezone Europe/Paris || warn "Echec réglage timezone"
278277
else
279-
warn "timedatectl indisponible — saut du réglage timezone."
280-
fi
281-
282-
# ============================== Cron @reboot ===================================
283-
if command -v crontab >/dev/null 2>&1; then
284-
log "Ajout cron @reboot (ping court vers 1.1.1.1)"
285-
if [[ $DRYRUN -eq 0 ]]; then
286-
(crontab -l 2>/dev/null | grep -v "ping -c 5 1\.1\.1\.1" 2>/dev/null; echo '@reboot /bin/ping -c 5 1.1.1.1 >/dev/null 2>&1 || true') | crontab -
287-
else
288-
echo "[DRYRUN] crontab append: @reboot /bin/ping -c 5 1.1.1.1 >/dev/null 2>&1 || true"
289-
fi
290-
else
291-
warn "crontab indisponible (cron non installé ?)"
278+
warn "timedatectl indisponible — tentative via /etc/localtime"
279+
ZF="/usr/share/zoneinfo/Europe/Paris"
280+
[[ -f "$ZF" ]] && run "Lien /etc/localtime → Europe/Paris" ln -sf "$ZF" /etc/localtime || warn "Zoneinfo non trouvée"
281+
[[ -w /etc/timezone ]] && echo "Europe/Paris" >/etc/timezone || true
292282
fi
293283

294284
# ============================== .bashrc perso =================================
295285
BRC_URL="https://raw.githubusercontent.com/OverStyleFR/AutoScriptBash/main/.assets/.bashrc"
296-
if [[ -f "$HOME/.bashrc" ]]; then
297-
run "Backup ~/.bashrc" cp -a "$HOME/.bashrc" "$HOME/.bashrc.bak.$(date +%Y%m%d-%H%M%S)" || true
286+
TARGET_USER="${SUDO_USER:-root}"
287+
TARGET_HOME="$(eval echo "~$TARGET_USER")"
288+
TARGET_RC="$TARGET_HOME/.bashrc"
289+
BK="$TARGET_HOME/.bashrc.bak.$(date +%Y%m%d-%H%M%S)"
290+
291+
log "Remplacement du .bashrc pour $TARGET_USER ($TARGET_HOME)"
292+
if [[ -f "$TARGET_RC" ]]; then
293+
run "Backup $TARGET_RC" cp -a "$TARGET_RC" "$BK" || true
298294
fi
299295
if command -v curl >/dev/null 2>&1; then
300-
[[ $DRYRUN -eq 1 ]] && echo "[DRYRUN] curl -fsSL $BRC_URL -o $HOME/.bashrc" || run "Télécharger .bashrc" curl -fsSL "$BRC_URL" -o "$HOME/.bashrc"
296+
[[ $DRYRUN -eq 1 ]] && echo "[DRYRUN] curl -fsSL $BRC_URL -o $TARGET_RC" \
297+
|| run "Télécharger .bashrc" curl -fsSL "$BRC_URL" -o "$TARGET_RC"
301298
elif command -v wget >/dev/null 2>&1; then
302-
[[ $DRYRUN -eq 1 ]] && echo "[DRYRUN] wget -q $BRC_URL -O $HOME/.bashrc" || run "Télécharger .bashrc" wget -q "$BRC_URL" -O "$HOME/.bashrc"
299+
[[ $DRYRUN -eq 1 ]] && echo "[DRYRUN] wget -q $BRC_URL -O $TARGET_RC" \
300+
|| run "Télécharger .bashrc" wget -q "$BRC_URL" -O "$TARGET_RC"
303301
else
304302
warn "Ni curl ni wget pour récupérer le .bashrc"
305303
fi
306-
307-
# Recharge seulement en shell interactif bash
308-
if [[ -n "${PS1:-}" && -n "${BASH_VERSION:-}" && $DRYRUN -eq 0 ]]; then
304+
# Permissions
305+
if [[ $DRYRUN -eq 0 && -f "$TARGET_RC" ]]; then
306+
run "Chown .bashrc" chown "$TARGET_USER":"$TARGET_USER" "$TARGET_RC" || true
307+
fi
308+
# Optionnel: copie pour /etc/skel
309+
if [[ -d /etc/skel && -f "$TARGET_RC" ]]; then
310+
run "Copie .bashrc vers /etc/skel" cp -f "$TARGET_RC" /etc/skel/.bashrc || true
311+
fi
312+
# Recharge si session bash interactive
313+
if [[ -n "${PS1:-}" && -n "${BASH_VERSION:-}" && $DRYRUN -eq 0 && "$TARGET_HOME" == "$HOME" ]]; then
309314
# shellcheck disable=SC1090
310315
run "Recharger ~/.bashrc" bash -lc "source \"$HOME/.bashrc\"" || true
311316
fi
312317

313-
# ============================== Avahi =========================================
318+
# ============================== Avahi (enable si présent) =====================
314319
if command -v systemctl >/dev/null 2>&1; then
315320
if systemctl list-unit-files | grep -q '^avahi-daemon\.service'; then
316321
run "Activer avahi-daemon" systemctl enable --now avahi-daemon || warn "Impossible d'activer avahi-daemon"
@@ -331,5 +336,4 @@ echo " - Durée totale : $(_since "$_start_ts")"
331336
echo -e "${BOLD}=========================================================================${C0}"
332337

333338
log "Configuration terminée."
334-
335339
# Fin

0 commit comments

Comments
 (0)