mirror of
https://github.com/buildplan/du_setup.git
synced 2025-12-18 02:05:32 +00:00
improvements to bashrc function
This commit is contained in:
parent
2cb3cc6a0e
commit
c1e57a3858
59
du_setup.sh
59
du_setup.sh
@ -1050,10 +1050,15 @@ cleanup_provider_packages() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- (This function goes AFTER cleanup_provider_packages() and BEFORE setup_user()) ---
|
||||||
|
|
||||||
configure_custom_bashrc() {
|
configure_custom_bashrc() {
|
||||||
local USER_HOME="$1"
|
local USER_HOME="$1"
|
||||||
local USERNAME="$2"
|
local USERNAME="$2"
|
||||||
local BASHRC_PATH="$USER_HOME/.bashrc"
|
local BASHRC_PATH="$USER_HOME/.bashrc"
|
||||||
|
local temp_source_bashrc=""
|
||||||
|
|
||||||
|
trap 'rm -f "$temp_source_bashrc" 2>/dev/null' RETURN ERR INT TERM
|
||||||
|
|
||||||
if ! confirm "Replace default .bashrc for '$USERNAME' with a custom one?" "n"; then
|
if ! confirm "Replace default .bashrc for '$USERNAME' with a custom one?" "n"; then
|
||||||
print_info "Skipping custom .bashrc configuration."
|
print_info "Skipping custom .bashrc configuration."
|
||||||
@ -1061,11 +1066,17 @@ configure_custom_bashrc() {
|
|||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
print_info "Creating custom .bashrc for '$USERNAME'..."
|
print_info "Preparing custom .bashrc for '$USERNAME'..."
|
||||||
log "Creating custom .bashrc at $BASHRC_PATH"
|
|
||||||
|
|
||||||
local bashrc_content
|
temp_source_bashrc=$(mktemp "/tmp/custom_bashrc_source.XXXXXX")
|
||||||
bashrc_content=$(cat <<'EOF'
|
if [[ -z "$temp_source_bashrc" || ! -f "$temp_source_bashrc" ]]; then
|
||||||
|
print_error "Failed to create temporary file for .bashrc content."
|
||||||
|
log "Error: mktemp failed for bashrc source."
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
chmod 600 "$temp_source_bashrc"
|
||||||
|
|
||||||
|
if ! cat > "$temp_source_bashrc" <<'EOF'; then
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
# ===================================================================
|
# ===================================================================
|
||||||
# Universal Portable .bashrc for Modern Terminals
|
# Universal Portable .bashrc for Modern Terminals
|
||||||
@ -2228,49 +2239,51 @@ alias commands='compgen -A function -A alias | grep -v "^_" | sort | column'
|
|||||||
# - Consider moving rarely-used functions to separate files
|
# - Consider moving rarely-used functions to separate files
|
||||||
# - Use 'time bash -i -c exit' to measure startup time
|
# - Use 'time bash -i -c exit' to measure startup time
|
||||||
EOF
|
EOF
|
||||||
) # End of bashrc_content assignment
|
print_error "Failed to write .bashrc content to temporary file $temp_source_bashrc."
|
||||||
|
log "Critical error: Failed to write bashrc content to $temp_source_bashrc."
|
||||||
# Back up existing .bashrc if it's not the default
|
rm -f "$temp_source_bashrc" 2>/dev/null
|
||||||
if [[ -f "$BASHRC_PATH" ]] && ! grep -q "generated by /usr/sbin/adduser" "$BASHRC_PATH" 2>/dev/null; then
|
return 0
|
||||||
local BASHRC_BACKUP="$BASHRC_PATH.backup_$(date +%Y%m%d_%H%M%S)"
|
|
||||||
print_info "Backing up existing non-default .bashrc to $BASHRC_BACKUP"
|
|
||||||
cp "$BASHRC_PATH" "$BASHRC_BACKUP"
|
|
||||||
log "Backed up existing .bashrc to $BASHRC_BACKUP"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local temp_bashrc_path
|
log "Successfully created temporary .bashrc source at $temp_source_bashrc"
|
||||||
temp_bashrc_path="/tmp/custom_bashrc_for_${USERNAME}.txt"
|
|
||||||
if ! printf '%s\n' "$bashrc_content" | tee "$BASHRC_PATH" > /dev/null; then
|
local temp_fallback_path="/tmp/custom_bashrc_for_${USERNAME}.txt"
|
||||||
|
|
||||||
|
if ! tee "$BASHRC_PATH" < "$temp_source_bashrc" > /dev/null; then
|
||||||
print_error "Failed to automatically write custom .bashrc to $BASHRC_PATH."
|
print_error "Failed to automatically write custom .bashrc to $BASHRC_PATH."
|
||||||
log "Error writing custom .bashrc for $USERNAME to $BASHRC_PATH (likely permissions issue)."
|
log "Error writing custom .bashrc for $USERNAME to $BASHRC_PATH (likely permissions issue)."
|
||||||
|
|
||||||
if printf '%s\n' "$bashrc_content" | tee "$temp_bashrc_path" > /dev/null; then
|
if cp "$temp_source_bashrc" "$temp_fallback_path"; then
|
||||||
chmod 644 "$temp_bashrc_path"
|
chmod 644 "$temp_fallback_path"
|
||||||
print_warning "ACTION REQUIRED: The custom .bashrc content has been saved to:"
|
print_warning "ACTION REQUIRED: The custom .bashrc content has been saved to:"
|
||||||
print_warning " ${temp_bashrc_path}"
|
print_warning " ${temp_fallback_path}"
|
||||||
print_info "After setup, please manually copy it:"
|
print_info "After setup, please manually copy it:"
|
||||||
print_info " sudo cp ${temp_bashrc_path} ${BASHRC_PATH}"
|
print_info " sudo cp ${temp_fallback_path} ${BASHRC_PATH}"
|
||||||
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
|
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
|
||||||
print_info " sudo chmod 644 ${BASHRC_PATH}"
|
print_info " sudo chmod 644 ${BASHRC_PATH}"
|
||||||
log "Saved custom .bashrc content to $temp_bashrc_path for manual installation."
|
log "Saved custom .bashrc content to $temp_fallback_path for manual installation."
|
||||||
else
|
else
|
||||||
print_error "Also failed to save custom .bashrc content to $temp_bashrc_path."
|
print_error "Also failed to save custom .bashrc content to $temp_fallback_path."
|
||||||
log "Critical error: Failed both writing to $BASHRC_PATH and saving to $temp_bashrc_path."
|
log "Critical error: Failed both writing to $BASHRC_PATH and copying $temp_source_bashrc to $temp_fallback_path."
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
if ! chown "$USERNAME:$USERNAME" "$BASHRC_PATH" || ! chmod 644 "$BASHRC_PATH"; then
|
if ! chown "$USERNAME:$USERNAME" "$BASHRC_PATH" || ! chmod 644 "$BASHRC_PATH"; then
|
||||||
print_warning "Failed to set correct ownership/permissions on $BASHRC_PATH."
|
print_warning "Failed to set correct ownership/permissions on $BASHRC_PATH."
|
||||||
log "Failed to chown/chmod $BASHRC_PATH"
|
log "Failed to chown/chmod $BASHRC_PATH"
|
||||||
print_warning "ACTION REQUIRED: Please manually set ownership/permissions:"
|
print_warning "ACTION REQUIRED: Please manually set ownership/permissions:"
|
||||||
|
print_info " sudo cp ${temp_source_bashrc} ${BASHRC_PATH} # You might need this if the file is corrupted"
|
||||||
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
|
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
|
||||||
print_info " sudo chmod 644 ${BASHRC_PATH}"
|
print_info " sudo chmod 644 ${BASHRC_PATH}"
|
||||||
|
temp_source_bashrc=""
|
||||||
else
|
else
|
||||||
print_success "Custom .bashrc created for '$USERNAME'."
|
print_success "Custom .bashrc created for '$USERNAME'."
|
||||||
log "Custom .bashrc configuration completed for $USERNAME."
|
log "Custom .bashrc configuration completed for $USERNAME."
|
||||||
|
rm -f "$temp_fallback_path" 2>/dev/null
|
||||||
fi
|
fi
|
||||||
rm -f "$temp_bashrc_path" 2>/dev/null
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
rm -f "$temp_source_bashrc" 2>/dev/null
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user