mirror of
https://github.com/buildplan/du_setup.git
synced 2025-12-29 16:14:59 +00:00
Improve error handling iin bacshrc fuction
This commit is contained in:
61
du_setup.sh
61
du_setup.sh
@@ -1064,15 +1064,8 @@ configure_custom_bashrc() {
|
|||||||
print_info "Creating custom .bashrc for '$USERNAME'..."
|
print_info "Creating custom .bashrc for '$USERNAME'..."
|
||||||
log "Creating custom .bashrc at $BASHRC_PATH"
|
log "Creating custom .bashrc at $BASHRC_PATH"
|
||||||
|
|
||||||
# Back up existing .bashrc if it's not the default skeleton
|
local bashrc_content
|
||||||
if [[ -f "$BASHRC_PATH" ]] && ! grep -q "generated by /usr/sbin/adduser" "$BASHRC_PATH" 2>/dev/null; then
|
bashrc_content=$(cat <<'EOF'
|
||||||
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
|
|
||||||
|
|
||||||
if ! tee "$BASHRC_PATH" > /dev/null <<'EOF'
|
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
# ===================================================================
|
# ===================================================================
|
||||||
# Universal Portable .bashrc for Modern Terminals
|
# Universal Portable .bashrc for Modern Terminals
|
||||||
@@ -2235,20 +2228,50 @@ 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
|
||||||
then
|
) # End of bashrc_content assignment
|
||||||
print_error "Failed to write custom .bashrc to $BASHRC_PATH."
|
|
||||||
log "Failed to write custom .bashrc."
|
# Back up existing .bashrc if it's not the default
|
||||||
return 1
|
if [[ -f "$BASHRC_PATH" ]] && ! grep -q "generated by /usr/sbin/adduser" "$BASHRC_PATH" 2>/dev/null; then
|
||||||
|
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
|
||||||
|
|
||||||
# Set correct ownership and permissions
|
local temp_bashrc_path
|
||||||
if ! chown "$USERNAME:$USERNAME" "$BASHRC_PATH" || ! chmod 644 "$BASHRC_PATH"; then
|
temp_bashrc_path="/tmp/custom_bashrc_for_${USERNAME}.txt"
|
||||||
print_warning "Failed to set correct ownership/permissions on $BASHRC_PATH."
|
if ! printf '%s\n' "$bashrc_content" | tee "$BASHRC_PATH" > /dev/null; then
|
||||||
log "Failed to chown/chmod $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)."
|
||||||
|
|
||||||
|
if printf '%s\n' "$bashrc_content" | tee "$temp_bashrc_path" > /dev/null; then
|
||||||
|
chmod 644 "$temp_bashrc_path"
|
||||||
|
print_warning "ACTION REQUIRED: The custom .bashrc content has been saved to:"
|
||||||
|
print_warning " ${temp_bashrc_path}"
|
||||||
|
print_info "After setup, please manually copy it:"
|
||||||
|
print_info " sudo cp ${temp_bashrc_path} ${BASHRC_PATH}"
|
||||||
|
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
|
||||||
|
print_info " sudo chmod 644 ${BASHRC_PATH}"
|
||||||
|
log "Saved custom .bashrc content to $temp_bashrc_path for manual installation."
|
||||||
|
else
|
||||||
|
print_error "Also failed to save custom .bashrc content to $temp_bashrc_path."
|
||||||
|
log "Critical error: Failed both writing to $BASHRC_PATH and saving to $temp_bashrc_path."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if ! chown "$USERNAME:$USERNAME" "$BASHRC_PATH" || ! chmod 644 "$BASHRC_PATH"; then
|
||||||
|
print_warning "Failed to set correct ownership/permissions on $BASHRC_PATH."
|
||||||
|
log "Failed to chown/chmod $BASHRC_PATH"
|
||||||
|
print_warning "ACTION REQUIRED: Please manually set ownership/permissions:"
|
||||||
|
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
|
||||||
|
print_info " sudo chmod 644 ${BASHRC_PATH}"
|
||||||
|
else
|
||||||
|
print_success "Custom .bashrc created for '$USERNAME'."
|
||||||
|
log "Custom .bashrc configuration completed for $USERNAME."
|
||||||
|
fi
|
||||||
|
rm -f "$temp_bashrc_path" 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
print_success "Custom .bashrc created for '$USERNAME'."
|
return 0
|
||||||
log "Custom .bashrc configuration completed for $USERNAME."
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- USER INTERACTION ---
|
# --- USER INTERACTION ---
|
||||||
|
|||||||
Reference in New Issue
Block a user