【问题标题】:Checking for string value in the server hostname检查服务器主机名中的字符串值
【发布时间】:2018-09-09 21:13:45
【问题描述】:

我的 RHEL 服务器的主机名格式为 la45serapp090.lba.com

如果hostname 有字符串serapp,我想返回true

到目前为止,我已经编写了以下命令,但它只是返回值45

uname -n | cut -c3,4

【问题讨论】:

  • “返回真”是什么意思?你写的是函数吗?函数和脚本只能返回 0-255 之间的整数。还是您的意思是将变量设置为True 或者可能回显文本值"true"

标签: bash sed sh cut rhel


【解决方案1】:

您可以在if 中进行模式匹配:

if [[ `uname -a` == *serapp* ]];
then echo serapp server
else echo non-serapp server
fi

【讨论】:

    【解决方案2】:

    使用正则表达式:

    #!/bin/bash
    
    if [[ $( uname -a ) =~ serapp ]]; then
        return 0
    else
        return 1
    fi
    

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 2014-01-14
      • 2017-05-22
      • 2012-04-14
      • 2016-04-16
      • 2012-09-15
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多