2019/12/30

yum 套件管理

我在這篇文章寫過 yum 跟 apt 的使用差異比較,這篇來討論一下 yum 對於 repo 的管理方式,雖然很多方面 centos 都比 ubuntu 麻煩許多,但 centos 的 yum 在某些方面對於 repo 的管理更加的方便。

首先,如果你沒有這個指令,請先透過 yum 安裝,yum -y install yum-utils,一般我們會這樣操作。

$ yum repolist all # 查詢有哪些 repo
$ yum repolist enabled # 哪些 repo 有啟用
$ yum repolist disabled # 哪些 repo 沒有啟用

今天假設我的本機電腦 PHP 版本是 7.1,我有安裝 7.3 的 repo,我要更換版本到 7.3 的話指令如下:

$ sudo yum-config-manager --disable remi-php71
$ sudo yum-config-manager --enable remi-php73
$ sudu yum install php
$ php -v
PHP 7.3.13 (cli) (built: Dec 17 2019 10:29:15) ( NTS )

打完收工,至於 repo 怎麼安裝只要打上要裝的 package 加上 yum repo 等關鍵字幾乎都找的到。

如果你不想透過 config 設定,想在 runtime 的時候指定 repo,大概流程如下:

$ sudo yum repolist all | grep php

remi-php54                          Remi's PHP 5.4 RPM repositor disabled
remi-php55                          Remi's PHP 5.5 RPM repositor disabled
remi-php55-debuginfo/x86_64         Remi's PHP 5.5 RPM repositor disabled
remi-php56                          Remi's PHP 5.6 RPM repositor disabled
remi-php56-debuginfo/x86_64         Remi's PHP 5.6 RPM repositor disabled
remi-php70                          Remi's PHP 7.0 RPM repositor disabled
remi-php70-debuginfo/x86_64         Remi's PHP 7.0 RPM repositor disabled
remi-php70-test                     Remi's PHP 7.0 test RPM repo disabled
remi-php70-test-debuginfo/x86_64    Remi's PHP 7.0 test RPM repo disabled
remi-php71                          Remi's PHP 7.1 RPM repositor disabled
remi-php71-debuginfo/x86_64         Remi's PHP 7.1 RPM repositor disabled
remi-php71-test                     Remi's PHP 7.1 test RPM repo disabled
remi-php71-test-debuginfo/x86_64    Remi's PHP 7.1 test RPM repo disabled
remi-php72                          Remi's PHP 7.2 RPM repositor disabled
remi-php72-debuginfo/x86_64         Remi's PHP 7.2 RPM repositor disabled
remi-php72-test                     Remi's PHP 7.2 test RPM repo disabled
remi-php72-test-debuginfo/x86_64    Remi's PHP 7.2 test RPM repo disabled
remi-php73                          Remi's PHP 7.3 RPM repositor disabled
remi-php73-debuginfo/x86_64         Remi's PHP 7.3 RPM repositor disabled
remi-php73-test                     Remi's PHP 7.3 test RPM repo disabled
remi-php73-test-debuginfo/x86_64    Remi's PHP 7.3 test RPM repo disabled
remi-php74                          Remi's PHP 7.4 RPM repositor disabled
remi-php74-debuginfo/x86_64         Remi's PHP 7.4 RPM repositor disabled
remi-php74-test                     Remi's PHP 7.4 test RPM repo disabled
remi-php74-test-debuginfo/x86_64    Remi's PHP 7.4 test RPM repo disabled
remi-php80                          Remi's PHP 8.0 RPM repositor disabled
remi-php80-debuginfo/x86_64         Remi's PHP 8.0 RPM repositor disabled
remi-php80-test                     Remi's PHP 8.0 test RPM repo disabled
remi-php80-test-debuginfo/x86_64    Remi's PHP 8.0 test RPM repo disabled

# 透過上方找到了你想要裝的版本,獨立 enable repo 他
$ sudo yum install --enablerepo=remi-php74 php

2019/12/20

merge object

jQuery 有一個 $.extend 功能我覺得相當好用,假設你有兩個 object,{'name': 'chan', 'sport': 'basketball'},新的屬性是 {'sort': 'baseball'}, 透過 $.extend(object1, object2) 可以得到 {'name': 'chan', 'sport': 'baseball'},今天要在三個語言實現這個功能,但我要另外加一個內容是,如果新的內容是空字串就不覆蓋。

PHP
<?php  
  
$data = [  
    'name' => 'chan',  
    'sport' => 'basketball'  
];  
$newData = [  
    'name' => '',  
    'sport' => 'baseball'  
];  
$mergeObject = function($object1, $object2) {  
    return json_encode(  
        array_merge(  
            $object1,  
            array_filter(  
                $object2,  
                function($item) {  
                    return $item != '';  
                } 
            )  
        )  
    );  
}; 
  
echo $mergeObject($data, $newData); // {"name":"chan","sport":"baseball"}  
python
# coding=utf-8  
  
import json  
  
data = {  
    'name': 'chan',  
    'sport': 'basketball'  
}  
new_data = {  
    'name': '',  
    'sport': 'baseball'  
}  
  
  
def merge_object(object1, object2):  
    x = object1.copy()  
    n = {}
  
    for key, value in object2.items():  
        if value != '':  
            n[key] = value  
  
    x.update(n)  
  
    return json.dumps(x)  
  
  
print(merge_object(data, new_data)) # {"sport": "baseball", "name": "chan"}
node.js
let data = {  
    'name': 'chan',  
    'sport': 'basketball'  
};  
let newData = {  
    'name': '',  
    'sport': 'baseball'  
};  
let objectFilter = (object1, object2) => {  
    let n = {};  
  
    for (let i in object2) {  
        let value = object2[i];  
  
        if (value !== '') {  
            n[i] = value;  
        }  
    }  
  
    return JSON.stringify(Object.assign(object1, n));  
};  
  
console.log(objectFilter(data, newData)); // {"name":"chan","sport":"baseball"}