I have a variable $line, which can contain any of the following strings:
line="READ CACHE IS: ENABLED"line="BLOCKS READ CACHE AND SENT TO INITIATOR = 2489338280"line="ECC REREADS/ ERRORS ALGORITHM PROCESSED UNCORRECTED"line="READ: 2513550726 22 0 2513550748 2513550748 27768.965 0"line="1 RAW_READ_ERROR_RATE PO-R-- 100 100 016 - 0"line="0x22 GPL R/O 1 READ STREAM ERROR LOG"line="READ: DISABLED"
I have a script that compares the $line variable against some patterns:
if [[ ${line} == *"RAW_READ_ERROR_RATE"* ]] ||
[[ ${line} == "READ\:"* ]] &&
[[ ${line} != *"READ: DISABLED"* ]]; then
devReadErr=$(echo "$line" | awk '{print $8}')
Herein lies the problem. The colon is screwing everything up. I've tried every possible way of formatting the pattern to satisfy both possibilities of line="1 RAW_READ_ERROR_RATE PO-R-- 100 100 016 - 0" or line="READ: 2513550726 22 0 2513550748 2513550748 27768.965 0" When I escape the : as shown above. I can satisfy line="1 RAW_READ_ERROR_RATE PO-R-- 100 100 016 - 0" but not line="READ: 2513550726 22 0 2513550748 2513550748 27768.965 0". If I take away the escape, then I satisfy line="READ: 2513550726 22 0 2513550748 2513550748 27768.965 0"not line="1 RAW_READ_ERROR_RATE PO-R-- 100 100 016 - 0"
Sample Run 1:
line="1 RAW_READ_ERROR_RATE PO-R-- 100 100 016 - 0"
if [[ ${line} == *"RAW_READ_ERROR_RATE"* ]] ||
[[ ${line} == "READ\:"* ]] &&
[[ ${line} != *"READ: DISABLED"* ]]; then
devReadErr=$(echo "$line" | awk '{print $8}')
fi
echo $devReadErr
Output of Run 1:
0
Sample Run 2:
line="READ: 2513550726 22 0 2513550748 2513550748 27768.965 0"
if [[ ${line} == *"RAW_READ_ERROR_RATE"* ]] ||
[[ ${line} == "READ\:"* ]] &&
[[ ${line} != *"READ: DISABLED"* ]]; then
devReadErr=$(echo "$line" | awk '{print $8}')
fi
echo $devReadErr
Output of Run 2:
<null>
Sample Run 3:
line="READ: 2513550726 22 0 2513550748 2513550748 27768.965 0"
if [[ ${line} == *"RAW_READ_ERROR_RATE"* ]] ||
[[ ${line} == "READ:"* ]] &&
[[ ${line} != *"READ: DISABLED"* ]]; then
devReadErr=$(echo "$line" | awk '{print $8}')
fi
echo $devReadErr
Output of Run 3:
0
How do I get the best of both worlds?
=~operator, not==(which matches against file expansion), – user1934428 Sep 19 '18 at 06:35READ:*, and prints the eighth field, which is0. – ilkkachu Sep 19 '18 at 13:18$line(indicated above) and writes$devReadErras<null>upon encounteringREAD: 2513550726 22 0 2513550748 2513550748 27768.965 0when the colon is escaped. – AfroJoe Sep 19 '18 at 13:47