我想在 bash 脚本的输出中添加错误的含义

I want to add the meaning of an error to the output of a bash script

我有一个源文件,其中填充了这些行。

e9999=$"First named error has occurred"
e1000=$"Second named error has occurred"
e0000=$"Another named error has occurred"

我有一个脚本,它以这样的行输出结束(脚本是多次 greps 进入一些 sed,然后进一步 greps)。

Dec 03 22:15:49      uniqid c5bb3ed1d2110203e2850998f1008e92   IP 0.0.0.0   email@email.fqdn StatusCode: 1000
Dec 04 07:53:12      uniqid 476ceb443a552012a1dd1e754d279018   IP 255.255.255.255   email@email.fqdn StatusCode: 9999
Dec 04 10:51:08      uniqid 4c27a6e016e8b5c0a659274d20c86e0c   IP 192.168.0.1   email@email.fqdn StatusCode: 0000

我希望在输出的末尾添加每行'statuscode'的含义。

示例:

Dec 03 22:15:49      uniqid c5bb3ed1d2110203e2850998f1008e92   IP 0.0.0.0   email@email.fqdn StatusCode: 1000 Second named error has occurred
Dec 04 07:53:12      uniqid 476ceb443a552012a1dd1e754d279018   IP 255.255.255.255   email@email.fqdn StatusCode: 9999 First named error has occurred
Dec 04 10:51:08 uniqid 4c27a6e016e8b5c0a659274d20c86e0c IP 192.168.0.1 email@email.fqdn StatusCode: 0000 Another named error has occured

原始结果中的行数可能相差很大

错误代码可能有很大差异(但始终为 4 位数字),所有错误代码都在源文件中

错误代码的顺序可能会有所不同(除非对行进行排序会更容易)。

这一切都是通过 RHEL 完成的 bash

** 示例输入:**

12 月 05 19:20:46 应用程序 [调试] [会话 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] 通信:消息:company_PassDelivery_Test_app18b4e79f-ee3c-4a15-91f0-f97edb15478cPassDelivery_app4d82fb84-bffb-4b48-aea1-98a60236fd48https://transp1..net/s2s/default.asp?id=3578ef0a3bbf105c08d533699efb9f2c891fc4ea2019-12-05T19:20:45ZCallPlaced7000 12 月 05 19:20:46 app [debug] [session 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] reg_phase1_info_request.p:正在调用电子邮件@e.com StatusCode:7000 12 月 5 日 19:20:56 应用 [调试] [会话 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] 通信:消息:4d82fb84-bffb-4b48-aea1-98a60236fd48https://transp1..net/s2s/default.asp?id=3578ef0a3bbf105c08d533699efb9f2c891fc4ea2019T-=705] 12 月 5 日 19:20:56 app [debug] [session 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] reg_phase1_info_request.p:正在调用电子邮件@e.com StatusCode:3340 12 月 05 19:21:06 应用程序 [调试] [会话 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] 通信:消息:4d82fb84-bffb-4b48-aea1-98a60236fd48https://transp1..net/s2s/default.asp?id=3578ef0a3bbf105c08d533699efb9f2c891fc4ea2019T-=7005] 12 月 5 日 19:21:06 app [debug] [session 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] reg_phase1_info_request.p:正在调用电子邮件@e.com StatusCode:3130 12 月 5 日 19:26:28 应用程序 [调试] [会话 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] 通信:消息:company_PassDelivery_Test_app18b4e79f-ee3c-4a15-91f0-f97edb15478cPassDelivery_app4d82fb84-bffb-4b48-aea1-98a60236fd48https://transp1..net/s2s/default.asp?id=3578ef0a3bbf105c08d533699efb9f2c891fc4ea2019-12-05T19:26:28ZPhoneSessionDeliver3370 12 月 5 日 19:26:28 app [debug] [session 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] reg_phase1_info_request.p:电子邮件状态未知@e.com StatusCode:3370 12 月 05 日 19:26:28 app [debug] [session 75b49d52fdd572752c2848f9992cb838] [IP 255.255.255.255] reg_phase1_info_request.p:呼叫结束,消息 type:result。状态码:3013

** 代码示例:**

#!/bin/bash
source ~/ondemandscripts/errorcodes
today=$(date "+%b %d")
yesterday=$(date -d "1 day ago" "+%b %d")
echo -e "\nChecking "/opt/app/app/logs/PHP_Debug_Log" for  errors and users email address"
echo -e "\n"


if [ $# -lt 1 ]
then
        echo -e "Designed for app\nUsage options:\n[=14=] all\n[=14=] today\n[=14=] yesterday\n[=14=] alluniq\n[=14=] todayuniq\n[=14=] yesterdayuniq\n"
                        exit 1;
                        fi
                        if [  = "all" ]; then
                        grep -i 'statusCode' /opt/app/app/logs/PHP_Debug_Log | grep '1010\|1020\|2120\|2140\|2142\|2160\|2510\|2520\|2530\|2540\|2560\|3101\|3102\|3103\|3104\|3110\|3111\|3118\|3120\|3130\|3150\|3180\|3210\|3220\|3230\|3240\|3250\|3320\|3325\|3330\|3340\|3350\|3360\|3420\|3450\|3510\|3520\|3610\|3612\|3614\|3620\|3622\|3630\|3650\|3657\|3660\|3662' | sed 's/Comms.*<status>/  /' | sed 's/<\/statusCode>.*/ /' | sed 's/debug//'| sed 's/app//'| sed 's/>/ /' | sed 's/</ /' | sed 's/]/ /g' | sed 's/\[/ /g' | sed 's/reg_phase1.*result./ /' | sed 's/reg_phase1.*for / /' | grep -v "StatusCode: 7000" | grep 'email.com'

else
                               echo -e "Designed for app\nUsage options:\n[=14=] all\n[=14=] today\n[=14=] yesterday\n[=14=] alluniq\n[=14=] todayuniq\n[=14=] yesterdayuniq\n"

                                        fi

根据问题,体积(非常)大,可能是行等。这意味着这里需要脚本引擎(awk、Perl、Python、...)。两者的逻辑都是一样的

  • 预加载查找 table 错误代码
  • 对于每一行:
    • 从第 10 列查找错误代码
    • 将错误测试附加到行

使用 awk,假设查找 table 在 errors.txt

awk -v LOOKUP=lookup.txt '
BEGIN {
   # Read the errors.txt file into array x, using custom  FS
   FS="=\$"
   while ( (getline < LOOKUP) > 0 ) { x[] = substr(, 2, length()-2) }
   # Restore FS
   FS=" "
}
{
   print [=10=], x["e" ] ;
} 
' data.txt