fix chmod error for arch system and add curl and sudo check for shell script

This commit is contained in:
DennisYu07 2026-02-18 23:52:37 -08:00
parent c6a723efef
commit db56ba22cd

View file

@ -54,10 +54,109 @@ command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Global variable for download command
DOWNLOAD_CMD="curl -f -s -S -L"
WGET_CMD="wget -q -O -"
# Function to ensure curl or wget is available
ensure_curl_or_wget() {
if command_exists curl; then
DOWNLOAD_CMD="curl -f -s -S -L"
WGET_CMD="wget -q -O -"
return 0
fi
if command_exists wget; then
echo "curl not found, using wget for downloads."
DOWNLOAD_CMD="wget -q -O -"
WGET_CMD="wget -q -O -"
return 0
fi
echo "Neither curl nor wget found. Attempting to install..."
# Check if we're root or have sudo
if [[ "$(id -u)" -eq 0 ]]; then
# Running as root, no sudo needed
SUDO_CMD=""
elif command_exists sudo && sudo -n true 2>/dev/null; then
# Have sudo without password
SUDO_CMD="sudo"
else
echo "Error: Cannot install curl - sudo is not available and not running as root."
echo "Please install curl or wget manually and run this script again."
exit 1
fi
# Try to install curl based on OS
if command_exists apt-get; then
echo "Installing curl via apt-get..."
${SUDO_CMD} apt-get update && ${SUDO_CMD} apt-get install -y curl
elif command_exists dnf; then
echo "Installing curl via dnf..."
${SUDO_CMD} dnf install -y curl
elif command_exists pacman; then
echo "Installing curl via pacman..."
${SUDO_CMD} pacman -Syu --noconfirm curl
elif command_exists zypper; then
echo "Installing curl via zypper..."
${SUDO_CMD} zypper install -y curl
elif command_exists yum; then
echo "Installing curl via yum..."
${SUDO_CMD} yum install -y curl
elif command_exists brew; then
echo "Installing curl via Homebrew..."
${SUDO_CMD} brew install curl
elif command_exists /opt/homebrew/bin/brew; then
echo "Installing curl via Homebrew (ARM)..."
${SUDO_CMD} /opt/homebrew/bin/brew install curl
else
echo "Error: Cannot install curl - no supported package manager found."
echo "Please install curl or wget manually and run this script again."
exit 1
fi
# Verify installation
if command_exists curl; then
echo "✓ curl installed successfully"
return 0
else
echo "✗ Failed to install curl"
exit 1
fi
}
# Function to check if sudo is available
check_sudo_available() {
if command_exists sudo; then
# Check if sudo actually works (non-root user may have sudo but not configured)
if sudo -n true 2>/dev/null; then
return 0
else
echo "Warning: sudo is installed but requires password."
return 1
fi
fi
# No sudo found - check if we're running as root
if [[ "$(id -u)" -eq 0 ]]; then
return 0
fi
echo "Error: sudo is not available and you are not running as root."
echo ""
echo "This script requires either:"
echo " 1. sudo access (run with a user in sudoers group)"
echo " 2. root access (run as root)"
echo ""
echo "Please run this script with proper permissions or install packages manually."
return 1
}
# Function to fix npm global directory permissions
fix_npm_permissions() {
echo "Fixing npm global directory permissions..."
# Get the actual npm global directory
NPM_GLOBAL_DIR=$(npm config get prefix 2>/dev/null)
if [[ -z "${NPM_GLOBAL_DIR}" ]] || [[ "${NPM_GLOBAL_DIR}" == *"error"* ]]; then
@ -65,7 +164,25 @@ fix_npm_permissions() {
NPM_GLOBAL_DIR="${HOME}/.npm-global"
echo "Warning: Could not determine npm prefix, using fallback: ${NPM_GLOBAL_DIR}"
fi
# SAFETY CHECK: Never modify system directories
# This prevents catastrophic failures like breaking sudo setuid binaries
case "${NPM_GLOBAL_DIR}" in
/|/usr|/usr/local|/bin|/sbin|/lib|/lib64|/opt|/snap|/var|/etc)
echo "Warning: npm prefix is a system directory (${NPM_GLOBAL_DIR})."
echo "Skipping permission fix to avoid breaking system binaries."
echo ""
echo "This is likely a system-wide npm installation."
echo "Consider using a user-owned npm prefix instead:"
echo " npm config set prefix ~/.npm-global"
echo ""
echo "Alternatively, you can manually fix permissions for your user directory:"
echo " mkdir -p ~/.npm-global"
echo " npm config set prefix ~/.npm-global"
return 0
;;
esac
# 1. Change ownership of the entire npm global directory to current user
# Using only user ownership without specifying a group for cross-platform compatibility
sudo chown -R "$(whoami)" "${NPM_GLOBAL_DIR}" 2>/dev/null || true
@ -207,9 +324,9 @@ uninstall_nvm() {
install_npm_only() {
echo "Installing npm separately..."
if command_exists curl; then
echo "Attempting to install npm using: curl -qL https://www.npmjs.com/install.sh | sh"
if curl -qL https://www.npmjs.com/install.sh | sh; then
if command_exists curl || command_exists wget; then
echo "Attempting to install npm using: npmjs.com/install.sh"
if ${DOWNLOAD_CMD} https://www.npmjs.com/install.sh | sh; then
NPM_VERSION_TMP=$(npm --version 2>/dev/null)
if command_exists npm && [[ -n "${NPM_VERSION_TMP}" ]]; then
echo "✓ npm v${NPM_VERSION_TMP} installed via direct install script"
@ -217,9 +334,9 @@ install_npm_only() {
fi
fi
else
echo "curl command not found, proceeding with alternative methods"
echo "No download tool (curl/wget) available"
fi
return 1
}
@ -280,7 +397,7 @@ install_nodejs_via_nvm() {
# Ensure cleanup on exit
trap 'rm -f "${TMP_INSTALL_SCRIPT}"' EXIT
if curl -f -s -S -o "${TMP_INSTALL_SCRIPT}" "https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install_nvm.sh"; then
if ${DOWNLOAD_CMD} "https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install_nvm.sh" > "${TMP_INSTALL_SCRIPT}"; then
if bash "${TMP_INSTALL_SCRIPT}"; then
rm -f "${TMP_INSTALL_SCRIPT}"
trap - EXIT
@ -481,7 +598,31 @@ EOF
main() {
# Initialize variables
RESTORE_NPMRC=false
# Validate HOME variable
if [[ -z "${HOME}" ]]; then
echo "Warning: HOME environment variable is not set."
if [[ "$(id -u)" -eq 0 ]]; then
export HOME="/root"
echo "Using HOME=/root for root user."
else
export HOME=$(eval echo ~$(whoami))
echo "Using HOME=${HOME}."
fi
fi
# Validate HOME directory exists
if [[ ! -d "${HOME}" ]]; then
echo "Error: HOME directory (${HOME}) does not exist."
exit 1
fi
# Check sudo availability first (basic permission check)
check_sudo_available
# Ensure curl/wget is available (needed to download NVM)
ensure_curl_or_wget
# Step 1: Check and install Node.js
install_nodejs
echo ""