I am working on generating some auto install scripts that would work on different versions of Ubuntu. Therefore, I try to keep the add-apt-repository
statements generic as seen below.
add-apt-repository -y "deb http://archive.canonical.com/ $(lsb_release -sc) partner"
add-apt-repository -y ppa:graphics-drivers
However, as some repos don't exist for certain versions, auto generated urls might lead to errors like below.
Err:22 http://ppa.launchpad.net/wireshark-dev/stable/ubuntu disco Release
404 Not Found [IP: 91.189.95.83 80]
E: The repository 'http://ppa.launchpad.net/wireshark-dev/stable/ubuntu disco Release' does not have a Release file.
N: Updating from such a repository can't be done securely, an is therefore disabled by default.
I know that this can't be kept 100% flexible across all versions without keeping ubuntu version vs package repo
lookup tables/if-else statements. However, for my simple purposes, it is enough to know if any given url/ppa is valid or not.
So, is there a way to test this without adding the url/ppa and seeing it to fail on next apt update
?
Update:
As pointed out, this question is closely related to mine but my aim is to check the validity before adding to the sources list, also, both for PPAs and repo urls. So, I've come up with the scripts below which possibly can be improved but also seem to do the job.
function add_ppa() {
local __ppa_name
for __ppa_name in "$@"; do
grep -h "^deb.*$__ppa_name" /etc/apt/sources.list.d/* > /dev/null 2>&1
if [ "$?" != "0" ]
then
local __ppa_prefix=$(echo $__ppa_name | cut -d"/" -f1)
local __ppa_suffix=${__ppa_name##*/}
curl -fsSL https://launchpad.net/~"$__ppa_prefix"/+archive/ubuntu/"$__ppa_suffix" &>/dev/stdout | grep "\"`lsb_release -sc`\"" -m1 >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo "Adding ppa:$__ppa_name"
sudo add-apt-repository -y ppa:$__ppa_name
fi
fi
done
}
function add_to_sources_list() {
local __repo_base_link="$1"
local __flavour="$2"
local __version_name=`lsb_release -sc`
local __repo_link="deb $__repo_base_link $__version_name $__flavour"
grep -h "^$__repo_link" /etc/apt/sources.list > /dev/null 2>&1
if [ "$?" != "0" ]
then
echo "Adding $__repo_link"
curl -fsSL "${__repo_base_link}/dists/${__version_name}/Release" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo "Adding repo:$__repo_link"
sudo add-apt-repository -y "$__repo_link"
fi
fi
}
# example use
add_ppa "wireshark-dev/stable"
add_to_sources_list "http://gb.archive.ubuntu.com/ubuntu/" "multiverse"