6

I am running Debian 8 Jessie, trying to install PHP command line intepreter from a shell script.

Here is what I do:

sudo apt-get install php-cli

it tells me there is no php-cli. I do find there is php5-cli. However, to make my script more robust, e.g. when running on a recent ubuntu server offering php7, I do not want to specify php5-cli.

I googled for an answer but what I get only confused me:

xrfang@P-qapub:~$ sudo apt-get install --names-only "php*-cli"
E: Command line option --names-only is not understood
xrfang@P-qapub:~$ sudo apt-cache search --names-only php-cli
php-google-api-php-client - Google APIs client library for PHP
xrfang@P-qapub:~$ apt-cache search --names-only "php\d+-cli$"
xrfang@P-qapub:~$ apt-cache search --names-only "php\d-cli$"
xrfang@P-qapub:~$ apt-cache search --names-only "php\\d-cli$"
xrfang@P-qapub:~$ apt-cache search --names-only "php5-cli$"
php5-cli - command-line interpreter for the php5 scripting language

My questions are:

  1. I do not want the Google API for PHP, so I try to use regex, but as shown above it just does not accept \d.

  2. Is it possible to use --name-only somehow with apt-get install, not apt-cache search.

Thanks!

alexander.polomodov
  • 1,068
  • 3
  • 10
  • 14
xrfang
  • 185
  • 1
  • 1
  • 10

1 Answers1

5

Is it possible to use --name-only somehow with apt-get install, not apt-cache search.

By default apt-cache search searches informations from the available package names and their long description. --names-only restricts this search only to the package names.

Now apt-get doesn't care about description. It will try a package name verbatim, and if not found, will consider the parameter as a regex (while not really documented, actually an extended regular expression). This regex will be used only to match package name(s) from the list of known packages. So there's no --names-only option available: consider apt-get is under permanent effect of this option.

I do not want the Google API for PHP, so I try to use regex, but as shown above it just does not accept \d.

apt-get accepts (extended) regular expressions, not Perl Compatible Regular Expression. \d means a digit only in a PCRE and has no special meaning in BRE or ERE. A digit in a regular expression can be represended by [0-9] (or for fun [[:digit:]] as an extended regular expression syntax).

Your search would be:

$ apt-cache search --names-only 'php[0-9]+-cli'
php5-cli - command-line interpreter for the php5 scripting language

To install any php cli numbered version, that would be:

# apt-get install '^php[0-9]+-cli$'
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'php4-cli' for regex '^php[0-9]+-cli$'
Note, selecting 'php5-cli' for regex '^php[0-9]+-cli$'
The following extra packages will be installed:

[...]

This will install php5-cli and its dependencies.

Please note the usage of ^ and $ to delimit start and end of the package name selected: once the expression is used as a regex, it can be any part of a package's name, so better be careful. Eg trying apt-get install . will attempt to install any package having at least one character: all of them, and will of course fail for conflicts.

For the small details: php4-cli doesn't exist in Jessie, but is referenced by an other package (php-xajax) that's why it's selected as candidate since it matched the regex. Of course it's not installable so won't be listed in the end.

A.B
  • 11,090
  • 2
  • 24
  • 45
  • cool. although I used regex for over a decade, didn't even noticed how *non* perl compatible regex looks like ! thanks a lot. – xrfang Aug 14 '18 at 00:55