我如何以编程方式检查某个源是否在 sources.list 中?

我如何以编程方式检查某个源是否在 sources.list 中?

对于 10.04 LTS 和 12.04 LTS,除了使用 grep 之外,还有其他方法可以从 bash 脚本中检查源是否在 /etc/apt/sources.list 中吗?

答案1

apt-cache policy显示所有可用源。示例输出(已修剪):

Package files:
 100 /var/lib/dpkg/status
     release a=now
 500 http://ppa.launchpad.net/lekensteyn/wacom-tablet/ubuntu/ precise/main i386 Packages
     release v=12.04,o=LP-PPA-lekensteyn-wacom-tablet,a=precise,n=precise,l=Wacom Tablet Drivers,c=main
     origin ppa.launchpad.net
 500 http://ppa.launchpad.net/lekensteyn/wacom-tablet/ubuntu/ precise/main amd64 Packages
     release v=12.04,o=LP-PPA-lekensteyn-wacom-tablet,a=precise,n=precise,l=Wacom Tablet Drivers,c=main
     origin ppa.launchpad.net
 500 http://ppa.launchpad.net/bumblebee/testing/ubuntu/ precise/main i386 Packages
     release v=12.04,o=LP-PPA-bumblebee-testing,a=precise,n=precise,l=Bumblebee Developer releases,c=main
     origin ppa.launchpad.net
 500 http://ppa.launchpad.net/bumblebee/testing/ubuntu/ precise/main amd64 Packages
     release v=12.04,o=LP-PPA-bumblebee-testing,a=precise,n=precise,l=Bumblebee Developer releases,c=main
     origin ppa.launchpad.net
 500 http://archive.canonical.com/ubuntu/ precise/partner i386 Packages
     release v=12.04,o=Canonical,a=precise,n=precise,l=Partner archive,c=partner
     origin archive.canonical.com
...
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/main Translation-nl
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/main Translation-en
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/multiverse i386 Packages
     release v=12.04,o=Ubuntu,a=precise,n=precise,l=Ubuntu,c=multiverse
     origin nl.archive.ubuntu.com
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/universe i386 Packages
     release v=12.04,o=Ubuntu,a=precise,n=precise,l=Ubuntu,c=universe
     origin nl.archive.ubuntu.com
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/restricted i386 Packages
     release v=12.04,o=Ubuntu,a=precise,n=precise,l=Ubuntu,c=restricted
     origin nl.archive.ubuntu.com
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/main i386 Packages
     release v=12.04,o=Ubuntu,a=precise,n=precise,l=Ubuntu,c=main
     origin nl.archive.ubuntu.com
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/multiverse amd64 Packages
     release v=12.04,o=Ubuntu,a=precise,n=precise,l=Ubuntu,c=multiverse
     origin nl.archive.ubuntu.com
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/universe amd64 Packages
     release v=12.04,o=Ubuntu,a=precise,n=precise,l=Ubuntu,c=universe
     origin nl.archive.ubuntu.com
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/restricted amd64 Packages
     release v=12.04,o=Ubuntu,a=precise,n=precise,l=Ubuntu,c=restricted
     origin nl.archive.ubuntu.com
 500 http://nl.archive.ubuntu.com/ubuntu/ precise/main amd64 Packages
     release v=12.04,o=Ubuntu,a=precise,n=precise,l=Ubuntu,c=main
     origin nl.archive.ubuntu.com
Pinned packages

用于awk检索实际源(URL 或文件):

$ apt-cache policy | awk '/^ *[0-9]/{print $2 " " $3}'
/var/lib/dpkg/status 
http://ppa.launchpad.net/lekensteyn/wacom-tablet/ubuntu/ precise/main
http://ppa.launchpad.net/lekensteyn/wacom-tablet/ubuntu/ precise/main
http://ppa.launchpad.net/bumblebee/testing/ubuntu/ precise/main
http://ppa.launchpad.net/bumblebee/testing/ubuntu/ precise/main
...
http://nl.archive.ubuntu.com/ubuntu/ precise/universe
http://nl.archive.ubuntu.com/ubuntu/ precise/restricted
http://nl.archive.ubuntu.com/ubuntu/ precise/main

答案2

我最终使用grep它来检查sources.list并检查sources.list.d是否存在foo.list

虽然它没有我所希望的那样干净,但它足以满足我的需要。

相关内容