@@ -100,7 +100,7 @@ ${COLOR_BOLD}OPTIONS${COLOR_RESET}
100100
101101${COLOR_BOLD} EXAMPLES${COLOR_RESET}
102102 ${COLOR_DIM} # Run the interactive kernel selector${COLOR_RESET}
103- ${COLOR_GREEN} \$ ${COLOR_RESET} sudo ${SCRIPT_NAME}
103+ ${COLOR_GREEN} \$ ${COLOR_RESET} ${SCRIPT_NAME}
104104
105105 ${COLOR_DIM} # Display help${COLOR_RESET}
106106 ${COLOR_GREEN} \$ ${COLOR_RESET} ${SCRIPT_NAME} --help
@@ -121,7 +121,7 @@ ${COLOR_BOLD}EXIT STATUS${COLOR_RESET}
121121 ${COLOR_RED} 1${COLOR_RESET} Error (permissions, no kernels found, user cancelled, etc.)
122122
123123${COLOR_BOLD} NOTES${COLOR_RESET}
124- ${COLOR_YELLOW} ⚠${COLOR_RESET} This script requires ${COLOR_BOLD} sudo${COLOR_RESET} privileges to modify GRUB configuration
124+ ${COLOR_YELLOW} ⚠${COLOR_RESET} This script automatically uses ${COLOR_BOLD} sudo${COLOR_RESET} when needed
125125 ${COLOR_YELLOW} ⚠${COLOR_RESET} A backup of ${COLOR_DIM}${GRUB_CONFIG}${COLOR_RESET} is created before modification
126126 ${COLOR_YELLOW} ⚠${COLOR_RESET} Kernel images smaller than 1MB are considered invalid
127127
@@ -144,14 +144,6 @@ show_version() {
144144 echo -e " ${COLOR_DIM} Interactive GRUB default kernel selection tool${COLOR_RESET} "
145145}
146146
147- check_root () {
148- if [[ $EUID -ne 0 ]]; then
149- print_error " This script requires root privileges"
150- print_info " Please run with: ${COLOR_BOLD} sudo ${SCRIPT_NAME}${COLOR_RESET} "
151- exit 1
152- fi
153- }
154-
155147check_requirements () {
156148 local missing_tools=()
157149
@@ -171,12 +163,19 @@ check_requirements() {
171163 fi
172164}
173165
166+ check_sudo () {
167+ if ! command -v sudo & > /dev/null; then
168+ print_error " sudo command not found. This script requires sudo privileges."
169+ exit 1
170+ fi
171+ }
172+
174173backup_grub_config () {
175174 local backup_file=" ${GRUB_CONFIG} .backup.$( date +%Y%m%d_%H%M%S) "
176175
177176 if [[ -f " $GRUB_CONFIG " ]]; then
178177 print_step " Creating backup of GRUB configuration..."
179- cp " $GRUB_CONFIG " " $backup_file "
178+ sudo cp " $GRUB_CONFIG " " $backup_file "
180179 print_success " Backup created: ${COLOR_DIM} $backup_file ${COLOR_RESET} "
181180 fi
182181}
@@ -265,15 +264,15 @@ update_grub_config() {
265264
266265 print_step " Updating GRUB configuration..."
267266
268- # Check if GRUB_TOP_LEVEL exists (commented or uncommented )
267+ # Read the current config without sudo (reading doesn't need sudo if file is readable )
269268 if grep -q " ^#*GRUB_TOP_LEVEL=" " $GRUB_CONFIG " 2> /dev/null; then
270- # Exists - uncomment and update
269+ # Exists - uncomment and update (needs sudo)
271270 print_info " GRUB_TOP_LEVEL found, updating value..."
272- sed -i " s|^#*GRUB_TOP_LEVEL=.*|GRUB_TOP_LEVEL=\" ${BOOT_DIR} /${kernel_name} \" |" " $GRUB_CONFIG "
271+ sudo sed -i " s|^#*GRUB_TOP_LEVEL=.*|GRUB_TOP_LEVEL=\" ${BOOT_DIR} /${kernel_name} \" |" " $GRUB_CONFIG "
273272 else
274- # Doesn't exist - add it
273+ # Doesn't exist - add it (needs sudo)
275274 print_info " Adding GRUB_TOP_LEVEL to configuration..."
276- echo " GRUB_TOP_LEVEL=\" ${BOOT_DIR} /${kernel_name} \" " >> " $GRUB_CONFIG "
275+ echo " GRUB_TOP_LEVEL=\" ${BOOT_DIR} /${kernel_name} \" " | sudo tee -a " $GRUB_CONFIG " > /dev/null
277276 fi
278277
279278 print_success " GRUB_TOP_LEVEL set to: ${COLOR_BOLD}${BOOT_DIR} /${kernel_name}${COLOR_RESET} "
@@ -282,7 +281,7 @@ update_grub_config() {
282281regenerate_grub () {
283282 print_step " Regenerating GRUB configuration..."
284283
285- if grub-mkconfig -o " $GRUB_MKCONFIG " 2>&1 ; then
284+ if sudo grub-mkconfig -o " $GRUB_MKCONFIG " 2>&1 ; then
286285 print_success " GRUB configuration regenerated successfully!"
287286 else
288287 print_error " Failed to regenerate GRUB configuration"
@@ -333,8 +332,8 @@ main() {
333332 print_header " GRUB Kernel Selector v${SCRIPT_VERSION} "
334333 echo " "
335334
336- check_root
337335 check_requirements
336+ check_sudo
338337
339338 # Find valid kernel images
340339 local -a kernels=()
0 commit comments