要编写一个 Bash 脚本来检查 MTR(Matt's traceroute) 的输出是否符合某个正则表达式,可以使用 grep 命令来匹配正则表达式并检查输出。
下面是一个示例脚本,它会运行 MTR 命令,并使用 grep 来匹配正则表达式:
#!/bin/bash
# 运行 MTR 命令,并将输出保存到变量中
mtr_output=$(mtr google.com)
# 定义要匹配的正则表达式
regex="Loss% Snt Last Avg Best Wrst StDev"
# 使用 grep 命令匹配正则表达式,并检查输出
if echo "$mtr_output" | grep -q "$regex"; then
echo "MTR output matches regex"
else
echo "MTR output does not match regex"
fi
在这个示例中,我们首先运行 MTR 命令并将输出保存到一个变量中。然后,我们定义要匹配的正则表达式,并使用 grep 命令来匹配输出。如果匹配成功,则输出 "MTR output matches regex",否则输出 "MTR output does not match regex"。
你可以根据自己的需求修改正则表达式和 MTR 命令的参数。
上一篇:Bash脚本检查命令行参数