Created
February 26, 2025 17:45
-
-
Save AbhishekGhosh/c4d876470693f76d23831aacc1f5abf2 to your computer and use it in GitHub Desktop.
Bash script Ubuntu repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to list repositories | |
list_repos() { | |
echo "\nAvailable repositories:" | |
local i=1 | |
repos=() | |
# List repositories from sources.list | |
while IFS= read -r line; do | |
if [[ ! "$line" =~ ^# && -n "$line" ]]; then | |
repos+=("$line") | |
echo "$i) $line" | |
((i++)) | |
fi | |
done < /etc/apt/sources.list | |
# List repositories from sources.list.d/ | |
for file in /etc/apt/sources.list.d/*.list; do | |
if [ -f "$file" ]; then | |
while IFS= read -r line; do | |
if [[ ! "$line" =~ ^# && -n "$line" ]]; then | |
repos+=("$line") | |
echo "$i) $line" | |
((i++)) | |
fi | |
done < "$file" | |
fi | |
done | |
echo "" | |
} | |
# Function to remove a selected repository | |
remove_repo() { | |
if [[ ${#repos[@]} -eq 0 ]]; then | |
echo "No repositories available to remove." | |
return | |
fi | |
read -p "Enter the number of the repository to remove: " choice | |
if [[ $choice -ge 1 && $choice -le ${#repos[@]} ]]; then | |
repo_to_remove="${repos[$choice-1]}" | |
echo "Removing repository: $repo_to_remove" | |
sudo sed -i "\#$repo_to_remove#d" /etc/apt/sources.list | |
for file in /etc/apt/sources.list.d/*.list; do | |
sudo sed -i "\#$repo_to_remove#d" "$file" | |
done | |
sudo apt update | |
echo "Repository removed successfully." | |
else | |
echo "Invalid choice." | |
fi | |
} | |
# Menu interface | |
while true; do | |
echo "\nRepository Manager" | |
echo "1) List Repositories" | |
echo "2) Remove a Repository" | |
echo "3) Exit" | |
read -p "Choose an option: " option | |
case $option in | |
1) list_repos ;; | |
2) list_repos; remove_repo ;; | |
3) echo "Exiting..."; break ;; | |
*) echo "Invalid option, please try again." ;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment