I want to get a list of all versions of PHP that have the apache module installed.
This is my function :
availableVersions () {
available_versions="";
for php_version in `ls -d /etc/php/*/apache2`
do
available_versions="${available_versions}| ${php_version} ";
done
echo $available_versions;
}
The return of function is :
| /etc/php/7.0/apache2 | /etc/php/7.1/apache2 | /etc/php/7.2/apache2 | /etc/php/7.3/apache2 | /etc/php/7.4/apache2 | /etc/php/8.0/apache2
But what i want is :
| 7.0 | 7.1 | 7.2 | 7.3 | 7.4 | 8.0
Here is a solution that I have just found, but it will have to be improved.
availablesVersions () {
available_versions="";
for php_version in `ls -d /etc/php/*/apache2`
do
version="${php_version///etc/''}";
version="${version///php/''}";
version="${version///apache2/''}";
version="${version////''}";
available_versions="${available_versions}| ${version} ";
done
echo $available_versions;
}
Can someone help me?
2
Answers
Something like this maybe.
Using
extglob
you can do this:Output:
PS: Using
( ... )
to run these commands in a sub-shell to avoid changingIFS
for parent shell.