我需要帮助创建一个用于查找Docker中的错误日志的脚本。

huangapple go评论89阅读模式
英文:

I need help creating a script to find error logs in Docker

问题

I understand your request. Here's the translated portion of your text:

我正在尝试创建一个Bash脚本,可以从Docker容器中查找错误日志。基本上,我们会有一个像ABC1234567这样的ID,我正在尝试创建一个Bash脚本,该脚本会询问ID,并告诉您哪个容器有问题。到目前为止,我有以下代码:

#!/bin/bash

# 询问案例ID
echo '输入案例ID'
read caseID

# 运行容器检查
docker logs con1 2>&1 | grep -B 4 $caseID
docker logs con2 2>&1 | grep -B 4 $caseID
docker logs con3 2>&1 | grep -B 4 $caseID
docker logs con4 2>&1 | grep -B 4 $caseID
docker logs con5 2>&1 | grep -B 4 $caseID
docker logs con6 2>&1 | grep -B 4 $caseID

请注意,这段翻译的内容与您的代码相关,但不包括回答翻译问题的部分。如果您需要进一步的帮助或有其他问题,请随时提问。

英文:

I'm trying to create a bash script that can find error logs from a docker container. Essentially we will have an ID like ABC1234567 and I am I'm trying to create a script in Bash that asks for the ID and tells you which container has the error of the case. The code I have so far is:

#!/bin/bash

#ask for case ID
echo 'Enter Case ID'
read caseID

#run container check
docker logs con1 2>&1 | grep -B 4 $caseID
docker logs con2 2>&1 | grep -B 4 $caseID
docker logs con3 2>&1 | grep -B 4 $caseID
docker logs con4 2>&1 | grep -B 4 $caseID
docker logs con5 2>&1 | grep -B 4 $caseID
docker logs con6 2>&1 | grep -B 4 $caseID

Now, this does work, because once the ID is found in one of the containers, it does throw back a bunch of dialogue about that container, including the case ID, but it's mostly useless info and really it's just an indicator that we found the right container. The problem is that it doesn't tell me which container had the case ID. What would I need to add so that it can echo the Container name back at us? For example if I run the code I am expecting it to just say something like
Error in Con3

I tried to make an ifelse statements but I don't necessarily know the argument I am looking for. If it helps the output that comes out when the error ID is found in one of the containers actually contains that ID as a casename. This is my first time actually working with Docker so I am a bit limited in my knowledge for it.

答案1

得分: 0

一种可能性是重构您的脚本以利用循环,并输出当前容器名称。

#!/bin/bash

# 询问案例 ID
echo '输入案例 ID'
read -r caseID

for i in 1 2 3 4 5 6 ; do 
    echo "检查 con$i"
    docker logs "con$i" 2>&1 | grep -B 4 "$caseID"
done
英文:

One possibility might be to refactor your script to utilize a loop and output the current container name.

#!/bin/bash

#ask for case ID
echo 'Enter Case ID'
read -r caseID

for i in 1 2 3 4 5 6 ; do 
    echo "checking con$i"
    docker logs "con$i" 2>&1 | grep -B 4 "$caseID"
done

huangapple
  • 本文由 发表于 2023年6月29日 05:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定