简体   繁体   English

除了if语句中的echo之外还有Bash grep输出

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM