Untitled
cion
plain_text
17 days ago
2.5 kB
3
Indexable
Never
The issue you're facing occurs because the output of select_services is being treated as a service name rather than a list of service names. Specifically, when selecting services interactively, the menu labels (1., 2., etc.) are getting included in the list, which causes invalid service names to be passed to systemctl. Here's how you can fix it: Fix the select_services function: 1. Ensure you don't include the selection numbers in the service names. 2. Modify the while loop to handle selections properly. Updated select_services function: select_services() { echo "Select services (separate by space, e.g., 1 3 5):" for i in "${!SERVICES[@]}"; do echo "$((i+1)). ${SERVICES[$i]}" done read -r -a selected selected_services=() for i in "${selected[@]}"; do selected_services+=("${SERVICES[$((i-1))]}") # Add selected services to array done echo "${selected_services[@]}" # Print selected services } Changes in case block: For each option where select_services is used, remove the incorrect while-loop logic. Instead, directly assign selected_services and iterate over it: For example, the corrected part of the case block for option 2 (starting a service) should look like this: 2) selected_services=$(select_services) for service in $selected_services; do start_service "$service" done ;; Similarly, update the other blocks (e.g., for stopping, enabling, disabling services). Final script section example: 2) selected_services=$(select_services) for service in $selected_services; do start_service "$service" done ;; 3) selected_services=$(select_services) for service in $selected_services; do stop_service "$service" done ;; 4) selected_services=$(select_services) for service in $selected_services; do enable_service "$service" done ;; 5) selected_services=$(select_services) for service in $selected_services; do disable_service "$service" done ;; Summary of Fixes: The select_services function now properly returns only service names. You removed the unnecessary while IFS= read -r line structure that was incorrectly processing the service list output. For service management commands (start, stop, enable, disable), the selected services are passed correctly to systemctl. This should fix the issue where invalid service names were being generated and passed to systemctl.
Leave a Comment