如何在复杂的线条上进行非常精确的匹配

如何在复杂的线条上进行非常精确的匹配

我们有以下文件:

more test.txt

export KAFKA_HEAP_OPTS="-Xmx8g -Xms8g"
export KAFKA_JVM_PERFORMANCE_OPTS="-XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX
:MaxMetaspaceFreeRatio=80"

如何验证线路:

export KAFKA_HEAP_OPTS="-Xmx8g -Xms8g"

export KAFKA_JVM_PERFORMANCE_OPTS="-XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX
    :MaxMetaspaceFreeRatio=80"

存在于文件 - test.txt 中

当然,我们可以做

grep -q 'export KAFKA_HEAP_OPTS="-Xmx8g -Xms8g"' test.txt 
[[ $? -eq 0 ]] && echo "line in file"


grep -q 'export KAFKA_HEAP_OPTS="export KAFKA_JVM_PERFORMANCE_OPTS="-XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX
        :MaxMetaspaceFreeRatio=80"' test.txt 
[[ $? -eq 0 ]] && echo "line in file"

但这种方法非常笨拙

答案1

#! /bin/bash

env - bash -c '. test.txt; [ -n "$KAFKA_HEAP_OPTS" ] && [ -n "$KAFKA_JVM_PERFORMANCE_OPTS" ]'
if [ $? -eq 0 ]; then
     echo 'OK'
else
    echo 'ERROR'
fi

相关内容