Skip to content

Instantly share code, notes, and snippets.

@noobzhax
Created July 13, 2024 13:10
Show Gist options
  • Save noobzhax/ffaf78bcc54c05475319a1a590d489df to your computer and use it in GitHub Desktop.
Save noobzhax/ffaf78bcc54c05475319a1a590d489df to your computer and use it in GitHub Desktop.
List screen session easyly.
#!/bin/bash
# List existing screen sessions and assign numbers
sessions=($(screen -ls | grep -oP '^\s*\d+\.\S+'))
if [ ${#sessions[@]} -eq 0 ]; then
echo "No screen sessions found."
exit 1
fi
echo "Available screen sessions:"
for i in "${!sessions[@]}"; do
echo "$((i+1)). ${sessions[$i]}"
done
# Prompt user to enter the session number
read -p "Enter the session number to attach: " session_number
# Validate the input is a number
if ! [[ "$session_number" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter a valid session number."
exit 1
fi
# Validate the session number is within the range
if ((session_number < 1 || session_number > ${#sessions[@]})); then
echo "Invalid session number. Please select a number between 1 and ${#sessions[@]}."
exit 1
fi
# Attach to the selected screen session
selected_session=${sessions[$((session_number-1))]}
screen -r "$selected_session"
# Check if the screen command was successful
if [ $? -ne 0 ]; then
echo "Failed to attach to screen session $selected_session."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment