[英]Bash grep output in addition to echo within if statement
I have a simple script that checks if images are being used within our html
directory. 我有一个简单的脚本,检查我们的
html
目录中是否正在使用图像。 I have a file called imageserver.txt which looks like: 我有一个名为imageserver.txt的文件,它看起来像:
imageserver/icons/socialmedia/sqcolor_tumblr.png
imageserver/icons/socialmedia/sqcolor_gaf.png
imageserver/icons/socialmedia/sqcolor_yelp.png
imageserver/icons/socialmedia/sqcolor_linkedin.png
.....
This is my simple script 这是我的简单脚本
imageSearch.sh imageSearch.sh
#!/bin/bash
while IFS= read -r var
do
echo "\n ... Searching for $var \n"
if grep -rq $var /var/www/html; then
echo "Image exists ...\n"
echo $var >> doesExist.txt
else
echo "Image does not exist ...\n"
echo $var >> nonExists.txt
fi
done < ./imageserver.txt
Now, this works GREAT. 现在,这很有效。 I am wanting to add one more element only to the
if
statement. 我想再向
if
语句添加一个元素。 I want the echo >>
to not only print $var
to the doesExist.txt
-- But what file it was found in .. I am thinking I may need a secondary grep
inside the if
? 我希望
echo >>
不仅可以将$var
打印到doesExist.txt
- 但是它找到了什么文件 ...我想我可能需要在if
里面有一个二级grep
?
My desired input into doesExist.txt
would look like: 我对
doesExist.txt
所需输入如下所示:
imageserver/icons/socialmedia/sqcolor_tumblr.png | /var/www/html/file1.html
imageserver/icons/socialmedia/sqcolor_tumblr.png | /var/www/html/file2.html
I am stuck and not quite sure if I need a secondary grep
or -- Is it possible I can get the file names from the original grep
within the original if
statement? 我卡住了,不太确定我是否需要辅助
grep
或 - 我是否有可能从原始if
语句中获取原始grep
的文件名?
How do I accomplish my desired input into doesExist.txt
如何在
doesExist.txt
完成所需的输入
Change: 更改:
if grep -rq $var /var/www/html; then
echo "Image exists ...\n"
echo $var >> doesExist.txt
to some variation of: 对某些变化:
results=$(grep -rH "$var" /var/www/html)
if [ $? -eq 0 ]; then
echo "Image exists ...\n"
echo "$results" >> doesExist.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.