如何从 grep 获得有意义的输出?
How to get a meaningful output from grep?
我正在编写一个 bash 脚本,您可以 运行 在部署了游戏 mediawiki 的服务器上。我想检查维基是否使用 SMTP。因此,我的脚本的一部分会搜索配置文件中证明他们使用 SMTP 的内容。
问题是,我在多台服务器上这样做,我想在一台服务器上循环遍历多个市场。并非所有服务器都共享相同的市场名称。在我的脚本中,我有包含所有市场名称的数组。如果使用 SMTP,我希望我的脚本能够解决 grep 无法找到要在其中查找的文件的情况。我该怎么做?
我正在考虑一个额外的命令来在 grepping 之前询问文件是否存在。但这并没有如你所愿
for i in "${markets[@]}"; do
myPATH="/www/${game}_$i/${game}_$i/LocalSettings.php"
grepOut="grep -q 'wgSMTP = array(' "$myPATH""
if grep -q "wgSMTP = array(" "$myPATH"; then
echo -e "The Market ${BLUE}$i${NC} ${GREEN}uses${NC} SMTP."
else
if [[ "$grepOut" == *"No such file or directory"* ]]; then
if [[ "$market" == "all" ]]; then
echo -e "All markets:"
else
echo -e "The Market ${BLUE}$i doesn't${NC} exist on this server."
fi
else
echo -e "The Market ${BLUE}$i${NC} ${RED}doesn't${NC} use SMTP."
fi
fi
done
for i in "${markets[@]}"; do
path="/www/${game}_$i/${game}_$i/LocalSettings.php"
if ! [[ -f "$path" ]]; then
if [[ "$market" == "all" ]]; then
echo -e "All markets:"
else
echo -e "The Market ${BLUE}$i doesn't${NC} exist on this server."
fi
continue
fi
if grep -q 'wgSMTP = array(' "$path"; then
echo -e "The Market ${BLUE}$i${NC} ${GREEN}uses${NC} SMTP."
else
echo -e "The Market ${BLUE}$i${NC} ${RED}doesn't${NC} use SMTP."
fi
done
不确定 $market
是否应该是 $i
(反之亦然)。
我正在编写一个 bash 脚本,您可以 运行 在部署了游戏 mediawiki 的服务器上。我想检查维基是否使用 SMTP。因此,我的脚本的一部分会搜索配置文件中证明他们使用 SMTP 的内容。 问题是,我在多台服务器上这样做,我想在一台服务器上循环遍历多个市场。并非所有服务器都共享相同的市场名称。在我的脚本中,我有包含所有市场名称的数组。如果使用 SMTP,我希望我的脚本能够解决 grep 无法找到要在其中查找的文件的情况。我该怎么做? 我正在考虑一个额外的命令来在 grepping 之前询问文件是否存在。但这并没有如你所愿
for i in "${markets[@]}"; do
myPATH="/www/${game}_$i/${game}_$i/LocalSettings.php"
grepOut="grep -q 'wgSMTP = array(' "$myPATH""
if grep -q "wgSMTP = array(" "$myPATH"; then
echo -e "The Market ${BLUE}$i${NC} ${GREEN}uses${NC} SMTP."
else
if [[ "$grepOut" == *"No such file or directory"* ]]; then
if [[ "$market" == "all" ]]; then
echo -e "All markets:"
else
echo -e "The Market ${BLUE}$i doesn't${NC} exist on this server."
fi
else
echo -e "The Market ${BLUE}$i${NC} ${RED}doesn't${NC} use SMTP."
fi
fi
done
for i in "${markets[@]}"; do
path="/www/${game}_$i/${game}_$i/LocalSettings.php"
if ! [[ -f "$path" ]]; then
if [[ "$market" == "all" ]]; then
echo -e "All markets:"
else
echo -e "The Market ${BLUE}$i doesn't${NC} exist on this server."
fi
continue
fi
if grep -q 'wgSMTP = array(' "$path"; then
echo -e "The Market ${BLUE}$i${NC} ${GREEN}uses${NC} SMTP."
else
echo -e "The Market ${BLUE}$i${NC} ${RED}doesn't${NC} use SMTP."
fi
done
不确定 $market
是否应该是 $i
(反之亦然)。