获得Centos版本的价值

时间:2018-04-12 06:47:34

标签: linux bash shell awk sed

当我运行cat /etc/redhat-release时,我得到输出

CentOS Linux release 7.5.1708

我只想从中获取值7

我写了以下内容,但它无效

cat /etc/redhat-release | awk '{print $7}' | awk -F. '{print $1}

4 个答案:

答案 0 :(得分:3)

如果你只是想要数字,而不是awk的东西,这里更容易:

. /etc/os-release
echo $VERSION_ID

<强>为什么吗

查看手册页man os-release

  

os-release的基本文件格式是以换行符分隔的列表   类似环境的shell兼容变量赋值。有可能的   从shell脚本中获取配置...

答案 1 :(得分:0)

它可能不是最好的解决方案,但由于您知道该文件将以&#34; CentOS Linux发行版&#34;开头,您可以使用cut仅在该前缀之后使用22d字符:

$ cut -c22 /etc/redhat-release

答案 2 :(得分:0)

awk -F'.' '{sub(/.*[^0-9]/,"",$1); print $1}' <<< $(< /etc/redhat-release)

答案 3 :(得分:0)

如果你需要awk并且你可以使用awk -F '=' '/^VERSION_ID/ {print $2}' /etc/os-release 尝试:

| tr -d '"'

但也许需要使用grep -oP 'VERSION_ID="\K[^"]+' /etc/os-release

取消引用

使用grep:

\K

VERSION_ID="排除了[^"]+"意味着保持匹配,直到找到vid=$(. /etc/os-release; echo $VERSION_ID)

在这种情况下只需要使用int:

设置var
body {
    background-color: #ffffff;
    margin: 0px;
    display: grid;
    
}

#logo-holder { 
    float: left; 
    display: inline;
 }

    #logo-holder h1#logo a { 
        font-size: 22px; 
        font-weight: normal; 
        color: #000; 
        line-height: 1px; 
        font-style: italic;
        text-decoration: none;
        padding-top: 200px;

     }

        #logo-holder p#quote { 
            font-size: 10px; 
            color: #777; 
            line-height: 3px; 
            padding-left: 3px; 
            letter-spacing: 0.12em;
         }

ul {
    list-style-type: none;
    margin: 0;
    padding: 20px;
    padding-bottom: 40px;
    overflow: hidden;
    background-color: #e6e6e6;
    text-align: right;
    color: #dfbf9f;
}

li {
    display: inline-block;
}

    li a {
        display: block;
        color: #999999;
        text-align: center;
        padding: 14px 30px;
        text-decoration: none;
        font-weight: bold;
        font-size: 24px;
    }

        li a:hover {
            border-radius: 10px;
            background-color: #d9d9d9;
        }

        li a:visited {
            background-color: #d9d9d9 !important;
        }

.welcome {
    background: linear-gradient(to bottom, #01aef0 45%, #0090d7 100%);
    padding: 50px;
    margin: 0px;
    padding-top: 20px;
}

h1 {
    color: #ffffff;
    -webkit-margin-before: 0;
    -webkit-margin-after: 0;
    font-size: 5em;
    -webkit-margin-start: 15px;
    -webkit-margin-end: 15px;
    font-family: 'Crimson Text', serif;
    letter-spacing: 5px;
    margin-bottom: 10px;
}

h2 {
    margin: 0;
    color: #000000;
    font-style: italic;
    font-size: 2em;
    font-family: 'Ramajana', serif;
}

#lorem {
    color: #80dfff !important;
    -webkit-margin-start: 15px;
    -webkit-margin-end: 15px;
    -webkit-margin-before: -15px;
    -webkit-margin-after: 10px;
    font-family: 'Crimson Text', serif;
    font-weight:300;
}

.grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-row-gap: 50px;
    grid-column-gap: 30px;
    padding: 0px;
    margin-left: 100px;
    margin-right: 100px;
    margin-top: 75px;
    font-family: 'Ramajana', serif;
}

.description {
    font-size: 19px;
    color: #808080;
}

#title {
    font-size: 38px;
    font-weight: 700;
    font-family: 'Ramajana', serif;
}

.ostatnie {
    display: grid;
    grid-template-columns: 2fr 1fr;
    grid-gap: 20px;
    grid-column-gap: 30px;
    padding: 0px;
    margin-left: 100px;
    margin-right: 100px;
    margin-top: 30px;
    margin-bottom: 50px;
    }

footer {
    background-color: #f2f2f2;
    text-align: left;
    padding: 10px 0 2px 0;
    margin-bottom: 50px;
    display: inline-block;
    width: 100%;
    height: 75%;
    color: #999999;
    font-size: 17px;
    font-family: Verdana, sans-serif;
    float: left;

}
#footer-links { 
    padding: 0px 0px 0px 0; 
    text-decoration: none;
    color: #dfbf9f;
}
#footer-links p a { 
    padding-right: 10px; 
    padding-left: 30px;
    text-decoration: none;
    color: #808080;
    margin-left: 50px;
}
    
.cl {
margin: 5px 0 0 80px;
    }