簡體   English   中英

除了if語句中的echo之外還有Bash grep輸出

[英]Bash grep output in addition to echo within if statement

我有一個簡單的腳本,檢查我們的html目錄中是否正在使用圖像。 我有一個名為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
.....  

這是我的簡單腳本

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

現在,這很有效。 我想再向if語句添加一個元素。 我希望echo >>不僅可以將$var打印到doesExist.txt - 但是它找到了什么文件 ...我想我可能需要在if里面有一個二級grep

我對doesExist.txt所需輸入如下所示:

imageserver/icons/socialmedia/sqcolor_tumblr.png  |  /var/www/html/file1.html
imageserver/icons/socialmedia/sqcolor_tumblr.png  |  /var/www/html/file2.html

我卡住了,不太確定我是否需要輔助grep或 - 我是否有可能從原始if語句中獲取原始grep的文件名?

如何在doesExist.txt完成所需的輸入

更改:

if grep -rq $var /var/www/html; then

    echo "Image exists ...\n"
    echo $var >> doesExist.txt

對某些變化:

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