使用带有通配符的“apt-get remove”时如何排除一组软件包?

使用带有通配符的“apt-get remove”时如何排除一组软件包?

我正在尝试使用sudo apt-get remove --purge删除所有“php7.” 软件包,但我想排除“php7.0“包。我已经尝试过; sudo apt-get -y remove --purge php7.[^php7.0]*但没有成功。我尝试了很多建议的其他类似解决方案,但没有成功。事实上,它专门删除了我想要排除的包。如果有人解决了这个难题,我将非常感激我。

“Ubuntu 16.04.4 LTS”

~$ dpkg -l | grep php7
ii  php7.0-cli                          7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        command-line interpreter for the PHP scripting language
ii  php7.0-common                       7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        documentation, examples and common module for PHP
ii  php7.0-curl                         7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        CURL module for PHP
ii  php7.0-fpm                          7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php7.0-gd                           7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        GD module for PHP
ii  php7.0-imap                         7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        IMAP module for PHP
ii  php7.0-intl                         7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        Internationalisation module for PHP
ii  php7.0-json                         7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        JSON module for PHP
ii  php7.0-ldap                         7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        LDAP module for PHP
ii  php7.0-mbstring                     7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        MBSTRING module for PHP
ii  php7.0-mcrypt                       7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        libmcrypt module for PHP
ii  php7.0-mysql                        7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        MySQL module for PHP
ii  php7.0-opcache                      7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        Zend OpCache module for PHP
ii  php7.0-readline                     7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        readline module for PHP
ii  php7.0-xml                          7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP
ii  php7.0-zip                          7.0.33-29+ubuntu16.04.1+deb.sury.org+1     amd64        Zip module for PHP
ii  php7.1                              7.1.33-16+ubuntu16.04.1+deb.sury.org+1     all          server-side, HTML-embedded scripting language (metapackage)
ii  php7.1-bcmath                       7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        Bcmath module for PHP
ii  php7.1-bz2                          7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        bzip2 module for PHP
ii  php7.1-cli                          7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        command-line interpreter for the PHP scripting language
ii  php7.1-common                       7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        documentation, examples and common module for PHP
ii  php7.1-curl                         7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        CURL module for PHP
ii  php7.1-fpm                          7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php7.1-gd                           7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        GD module for PHP
ii  php7.1-intl                         7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        Internationalisation module for PHP
ii  php7.1-json                         7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        JSON module for PHP
ii  php7.1-mbstring                     7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        MBSTRING module for PHP
ii  php7.1-opcache                      7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        Zend OpCache module for PHP
ii  php7.1-readline                     7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        readline module for PHP
ii  php7.1-xml                          7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP
ii  php7.1-zip                          7.1.33-16+ubuntu16.04.1+deb.sury.org+1     amd64        Zip module for PHP
ii  php7.2                              7.2.31-1+ubuntu16.04.1+deb.sury.org+1      all          server-side, HTML-embedded scripting language (metapackage)
ii  php7.2-bcmath                       7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        Bcmath module for PHP
ii  php7.2-bz2                          7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        bzip2 module for PHP
ii  php7.2-cli                          7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        command-line interpreter for the PHP scripting language
ii  php7.2-common                       7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        documentation, examples and common module for PHP
ii  php7.2-curl                         7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        CURL module for PHP
ii  php7.2-fpm                          7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php7.2-gd                           7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        GD module for PHP
ii  php7.2-intl                         7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        Internationalisation module for PHP
ii  php7.2-json                         7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        JSON module for PHP
ii  php7.2-mbstring                     7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        MBSTRING module for PHP
ii  php7.2-opcache                      7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        Zend OpCache module for PHP
ii  php7.2-readline                     7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        readline module for PHP
ii  php7.2-xml                          7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP
ii  php7.2-zip                          7.2.31-1+ubuntu16.04.1+deb.sury.org+1      amd64        Zip module for PHP
ii  php7.3                              7.3.19-1+ubuntu16.04.1+deb.sury.org+1      all          server-side, HTML-embedded scripting language (metapackage)
ii  php7.3-bcmath                       7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        Bcmath module for PHP
ii  php7.3-bz2                          7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        bzip2 module for PHP
ii  php7.3-cli                          7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        command-line interpreter for the PHP scripting language
ii  php7.3-common                       7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        documentation, examples and common module for PHP
ii  php7.3-curl                         7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        CURL module for PHP
ii  php7.3-fpm                          7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php7.3-gd                           7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        GD module for PHP
ii  php7.3-intl                         7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        Internationalisation module for PHP
ii  php7.3-json                         7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        JSON module for PHP
ii  php7.3-mbstring                     7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        MBSTRING module for PHP
ii  php7.3-opcache                      7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        Zend OpCache module for PHP
ii  php7.3-readline                     7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        readline module for PHP
ii  php7.3-xml                          7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP
ii  php7.3-zip                          7.3.19-1+ubuntu16.04.1+deb.sury.org+1      amd64        Zip module for PHP

“Ubuntu 16.04.6 LTS”

~$ dpkg -l | grep php7
ii  php-common                            2:69+ubuntu16.04.1+deb.sury.org+2+php7.3                 all          Common files for PHP packages
ii  php7.0                                7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   all          server-side, HTML-embedded scripting language (metapackage)
ii  php7.0-bcmath                         7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        Bcmath module for PHP
ii  php7.0-bz2                            7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        bzip2 module for PHP
ii  php7.0-cli                            7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        command-line interpreter for the PHP scripting language
ii  php7.0-common                         7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        documentation, examples and common module for PHP
ii  php7.0-curl                           7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        CURL module for PHP
ii  php7.0-fpm                            7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php7.0-gd                             7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        GD module for PHP
ii  php7.0-intl                           7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        Internationalisation module for PHP
ii  php7.0-json                           7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        JSON module for PHP
ii  php7.0-mbstring                       7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        MBSTRING module for PHP
ii  php7.0-opcache                        7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        Zend OpCache module for PHP
ii  php7.0-readline                       7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        readline module for PHP
ii  php7.0-xml                            7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP
ii  php7.0-zip                            7.0.33-29+ubuntu16.04.1+deb.sury.org+1                   amd64        Zip module for PHP
ii  php7.1-cli                            7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        command-line interpreter for the PHP scripting language
ii  php7.1-common                         7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        documentation, examples and common module for PHP
ii  php7.1-curl                           7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        CURL module for PHP
ii  php7.1-fpm                            7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php7.1-gd                             7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        GD module for PHP
ii  php7.1-imap                           7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        IMAP module for PHP
ii  php7.1-intl                           7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Internationalisation module for PHP
ii  php7.1-json                           7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        JSON module for PHP
ii  php7.1-ldap                           7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        LDAP module for PHP
ii  php7.1-mbstring                       7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        MBSTRING module for PHP
ii  php7.1-mcrypt                         7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        libmcrypt module for PHP
ii  php7.1-mysql                          7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        MySQL module for PHP
ii  php7.1-opcache                        7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Zend OpCache module for PHP
ii  php7.1-readline                       7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        readline module for PHP
ii  php7.1-xml                            7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP
ii  php7.1-zip                            7.1.30-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Zip module for PHP
ii  php7.2                                7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    all          server-side, HTML-embedded scripting language (metapackage)
ii  php7.2-bcmath                         7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Bcmath module for PHP
ii  php7.2-bz2                            7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        bzip2 module for PHP
ii  php7.2-cli                            7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        command-line interpreter for the PHP scripting language
ii  php7.2-common                         7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        documentation, examples and common module for PHP
ii  php7.2-curl                           7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        CURL module for PHP
ii  php7.2-fpm                            7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php7.2-gd                             7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        GD module for PHP
ii  php7.2-intl                           7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Internationalisation module for PHP
ii  php7.2-json                           7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        JSON module for PHP
ii  php7.2-mbstring                       7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        MBSTRING module for PHP
ii  php7.2-opcache                        7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Zend OpCache module for PHP
ii  php7.2-readline                       7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        readline module for PHP
ii  php7.2-xml                            7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP
ii  php7.2-zip                            7.2.31-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Zip module for PHP
ii  php7.3                                7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    all          server-side, HTML-embedded scripting language (metapackage)
ii  php7.3-bcmath                         7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Bcmath module for PHP
ii  php7.3-bz2                            7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        bzip2 module for PHP
ii  php7.3-cli                            7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        command-line interpreter for the PHP scripting language
ii  php7.3-common                         7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        documentation, examples and common module for PHP
ii  php7.3-curl                           7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        CURL module for PHP
ii  php7.3-fpm                            7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php7.3-gd                             7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        GD module for PHP
ii  php7.3-intl                           7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Internationalisation module for PHP
ii  php7.3-json                           7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        JSON module for PHP
ii  php7.3-mbstring                       7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        MBSTRING module for PHP
ii  php7.3-opcache                        7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Zend OpCache module for PHP
ii  php7.3-readline                       7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        readline module for PHP
ii  php7.3-xml                            7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP
ii  php7.3-zip                            7.3.19-1+ubuntu16.04.1+deb.sury.org+1                    amd64        Zip module for PHP

答案1

您的正则表达式看起来不正确。它匹配包含(不一定以)“php7”加上一个任意字符(“.”)的包名称,后跟任意数量的字符,这些字符都不是以下字符:p、h、7、.、0。

您可能想要的是以下正则表达式:

sudo apt-get -y remove --purge '^php7\.[^0]'

这与“php7”匹配。在包名称的开头 (^) 加上除 0 之外的任何字符。后面可能还会有其他字符,但我们不需要考虑它们。

答案2

我会做什么:

sudo apt-get remove --purge $(
    dpkg -l | awk '$1 == "ii" && /php7/ && !/php7\.0/{print $2}'
)

将变量传递给,使用-v开关:

EXCLUDE_VERSION='php7.0'
awk -v exv="$EXCLUDE_VERSION" '$1 == "ii" && /php7/ && $2 !~ exv{print $2}'

相关内容