基础知识:我有一个 .crt 文件(认证机构文件),他由许多字段组成,但在恢复这个问题的一行中,我有这个:
Certificate:
...(alot of stuff before)...
Subject: C=US, ST=Maryland, L=Pasadena, O=Brent Baccala,
OU=FreeSoft, CN=www.freesoft.org/emailAddress=baccala@freesoft.org
Subject Public Key Info:
...(alot of stuff after)
我需要解析文件以填充 .csv 文件,我已经完成了我需要帮助的问题是,我需要获取该字段:
CN=www.fresoft.org
但是当我得到这种带有很多斜杠的 CN=...(Value 而不是 ...) 时,我在解析中遇到错误,就像原始字符串一样:
CN=foo/bar/the/hell/emailAddress=blablabla
我只需要:
foo/bar/the/hell
有那么一会儿,我在正确的列中得到了它,但是当我没有 emailAddress 时,我的解析失败了,然后我在我的 CN .csv 列中得到了错误的信息而不是
|CN|
foo/bar/the/hell
我得到:
|CN|
OU=FreeSoft, foo/bar/the/hell.
我有这个代码做CN解析:
#!/bin/bash
subject_line=$(echo $cert | grep -o "Subject:.*Subject Public Key Info")
cn=$(echo $subject_line | grep -o "CN=.*" )
if [ $(echo $cn | grep -c ".*email.*") -gt 0 ]; then
end_cn=$(echo $cn | grep -b -o emailAddress)
end_cn_idx=$(echo $end_cn | grep -o .*:)
final_end_cn=${end_cn_idx:0:-1}
common_name=${cn:3:$final_end_cn-4}
echo $common_name
else
end_cn=$(echo $cn | grep -b -o "Subject Public Key Info")
end_cn_idx=$(echo $end_cn | grep -o .*:)
final_end_cn=${end_cn_idx:0:-1}
common_name=${cn:3:$final_end_cn-5}
echo $common_name
fi