使用linux shell读取yaml文件

你说的曾经没有我的故事 提交于 2020-01-16 14:40:53

一. Linux 安装python3.7.0

centos6/7,centos系统本身默认安装有python2.x(其实我是centos6.5,centos默认安装python2.6.6),版本x根据不同版本系统有所不同,可通过 python --V 或 python --version 查看系统自带的python版本有一些系统命令时需要用到python2,不能卸载

1. 安装依赖包

1)首先安装gcc编译器,gcc有些系统版本已经默认安装,通过 gcc --version 查看,没安装的先安装gcc,yum -y install gcc

2)安装其它依赖包,(注:不要缺少,否则有可能安装python出错,python3.7.0以下的版本可不装 libffi-devel )

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel

2. 下载python3.7.0源码,根据需求下载

1)在https://www.python.org/ftp/python/中选择自己需要的python源码包,我下载的是python3.7.0

(大家也可以去https://www.python.org官网首页下载,找到downloads,就可以选择想要的版本下载了)
img

2)下载

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz

3. 解压Python-3.7.0.tgz

cd /usr/local
tar -zxvf /opt/software/Python-3.7.0.tgz

4. 建立一个空文件夹,用于存放python3程序

mkdir /usr/local/python3 

5. 执行配置文件,编译,编译安装

cd Python-3.7.0
./configure --prefix=/usr/local/python3
make && make install

安装完成没有提示错误便安装成功了

6. 建立软连接

ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3

7. 测试一下python3是否可以用

[root@mini Python-3.7.0]# python3
Python 3.7.0 (default, Jul 28 2018, 22:47:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world!")
hello world!
>>> exit()
[root@mini Python-3.7.0]# pip3 --version
pip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)

可以看到python3.7.0可以正常使用

二. bug: 使用pip 命令失败

1. 错误信息

1.1信息

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting virtualenv
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Could not fetch URL https://pypi.org/simple/virtualenv/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/virtualenv/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Could not find a version that satisfies the requirement virtualenv (from versions: )
No matching distribution found for virtualenv
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

1.2 错误原因

系统版本centos6.5,其中openssl的版本为OpenSSL 1.0.1e-fips 11 Feb 2013,而python3.7需要的openssl的版本为1.0.2或者1.1.x,需要对openssl进行升级,并重新编译python3.7.0。yum 安装的openssl 版本都比较低。

2. 解决方案–升级openssl

2.1 下载openssl

wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
cd /usr/local
tar -zxvf openssl-1.1.1a.tar.gz
cd openssl-1.1.1amv openssl-1.1.1a openssl

2.2 编译安装

./config --prefix=/usr/local/openssl no-zlib #不需要zlib
make
make install

2.3 备份原配置

mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl/ /usr/include/openssl.bak
新版配置
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
修改系统配置
\## 写入openssl库文件的搜索路径
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
\## 使修改后的/etc/ld.so.conf生效
ldconfig -v

2.4 查看openssl版本

openssl version
openssl version 提示:
 /usr/local/openssl/bin/openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

假如你的libssl.so.1.1 文件在/usr/local/openssl/lib/下面,可以这样做
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

2.5 重新安装python

./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
make
make install

三. shell 读取yaml 之 shyaml

1. 安装shyaml

pip3 install shyaml

2. 案例分析

2.1 file.yaml文件内容

创建文件
cd /usr/local
vim file.yaml
---
idc_group:
    name: bx
    bx:
        news_bx: news_bx
        web3_bx: web3_php-fpm_bx
        task: [1,2,3,4,5,6]

2.2 shell操作

(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml get-value idc_group.name
bx
(testenv3.7) localhost:testenv3.7 macname$ 
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml key-values idc_group.bx
news_bx
news_bx
web3_bx
web3_php-fpm_bx
(testenv3.7) localhost:testenv3.7 macname$ 


get-value:获取值
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml get-value idc_group.bx.news_bx
news_bx
(testenv3.7) localhost:testenv3.7 macname$ 


get-type:获取相应的类型
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml get-type idc_group.bx.news_bx
str
(testenv3.7) localhost:testenv3.7 macname$ 


get-values{,-0}:对序列类型来说,获取值列表
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml get-values idc_group.bx.task
1
2
3
4
5
6
(testenv3.7) localhost:testenv3.7 macname$ 


keys{,-0}:返回键列表
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml keys idc_group
name
bx
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml keys idc_group.bx
news_bx
web3_bx
task
(testenv3.7) localhost:testenv3.7 macname$ 


values{,-0}:返回值列表
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml values idc_group.bx
news_bx
web3_php-fpm_bx
- 1
- 2
- 3
- 4
- 5
- 6
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml values idc_group
bx
news_bx: news_bx
web3_bx: web3_php-fpm_bx
task:
- 1
- 2
- 3
- 4
- 5
- 6
(testenv3.7) localhost:testenv3.7 macname$ 


key-values,{,-0}:返回键值对
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml key-values idc_group.bx
news_bx
news_bx
web3_bx
web3_php-fpm_bx
task
- 1
- 2
- 3
- 4
- 5
- 6
(testenv3.7) localhost:testenv3.7 macname$ cat file.yaml | shyaml key-values idc_group
name
bx
bx
news_bx: news_bx
web3_bx: web3_php-fpm_bx
task:
- 1
- 2
- 3
- 4
- 5
- 6
(testenv3.7) localhost:testenv3.7 macname$ 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!