通过脚本启用 epel repo?

通过脚本启用 epel repo?

我正在通过 shell 脚本安装 Epel repo,该 repo 默认处于禁用状态,我想启用它以便脚本可以安装更多软件包。

以下安装 Epel repo:

yum install -y epel-release

这将创建 /etc/yum.repos.d/epel.repo,其中包含:

[epel]
name=Extra Packages for Enterprise Linux 5 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-5&arch=$basearch
failovermethod=priority
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 5 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch/debug
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-debug-5&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 5 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/5/SRPMS
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-source-5&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
gpgcheck=1

现在,我只想启用第一个 repo '[epel]。我尝试使用 sed,但到目前为止只能启用所有 repo(我是 sed 初学者):

sh-3.2$ sed 's,enabled=0,enabled=1,g;' ./epel.repo
[epel]
name=Extra Packages for Enterprise Linux 5 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-5&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 5 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch/debug
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-debug-5&arch=$basearch
failovermethod=priority
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 5 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/5/SRPMS
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-source-5&arch=$basearch
failovermethod=priority
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
gpgcheck=1

此版本的命令不会改变任何内容:

sed 's,\(\[epel\].*\)enabled=0,\1enabled=1,;' ./epel.repo

哪个正则表达式对我有用?或者有更好的方法吗?

答案1

sed(1)不会就地编辑文件,而是将编辑内容发送到其输出。要就地编辑,您需要给出-i(或--in-place) 标志。此外,您正在做的是编辑包含 的行[epel].*enabled,但您找不到这些行。您希望将替换限制在以 开头的部分[epel],该部分以下一个 结尾[

sed -i -e '/\[epel\]/,/^\[/s/enabled=0/enabled=1/' ./epel.repo

答案2

您的问题似乎没有定义命令行工具的任何特定参数,但我发现该yum-config-manager命令很有用:

yum-config-manager --enable epel

例子:

> cat /etc/yum.repos.d/epel.repo | grep '\[.*\]\|enabled.*'
[epel]
enabled=0
[epel-debuginfo]
enabled=0
[epel-source]
enabled=0
> yum-config-manager --enable epel
[ ... REMOVE LONG OUTPUT ... ]
> cat /etc/yum.repos.d/epel.repo | grep '\[.*\]\|enabled.*'
[epel]
enabled=1
[epel-debuginfo]
enabled=0
[epel-source]
enabled=0
> yum-config-manager --disable epel
[ ... REMOVE LONG OUTPUT ... ]
> cat /etc/yum.repos.d/epel.repo | grep '\[.*\]\|enabled.*'
[epel]
enabled=0
[epel-debuginfo]
enabled=0
[epel-source]
enabled=0

相关内容