gpt4 book ai didi

regex - 需要使用grep在文件中查找字符串

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:13 26 4
gpt4 key购买 nike

我正在使用 shell 脚本通过 wget 获取文件并搜索它的模式。我的shell脚本如下:

#Execute commands one by one
while read line
do
STARTTIME=$(($(date +%s%N)/1000000))
line2=$(wget -q --post-data "$line" -O PHPFiles/test.php http://localhost:1234/XSS/XSS2/test.php)
ENDTIME=$(($(date +%s%N)/1000000))
GAP=$(($ENDTIME-$STARTTIME))
DIFF=$(($DIFF+($ENDTIME-$STARTTIME)))
echo "Time Taken "$GAP

finalSearchLine1="${line/&name2=/ }"
finalSearchLine2="${finalSearchLine1/name=/}"

echo "$finalSearchLine2"

if grep -q -F "$finalSeachLine2" -a PHPFiles/test.php;
then
echo found
success=$((success+1))
else
echo not found
failure=$((failure+1))
fi
rm PHPFiles/test.php
done < $1
echo "***************"
echo "Success "$success
echo "Failure "$failure
echo "Total Time "$DIFF
echo "Average Time "$((DIFF/(success+failure)))

但是,我在使用 grep 命令时遇到了问题。有时,数据 $finalSearchLine2 包含引号,例如:

<script >alert("XSS"); </script>

这似乎会导致 grep 命令出现问题。对于 if 语句,即使 $finalSearchLine2 变量中没有匹配模式,我似乎总是得到 found 结果。我不知道是否可以在 grep 变量中使用转义字符串。谁能提出一个可能的解决方案?

最佳答案

Grep 需要像这样的双引号进行转义 \"因此,作为第一个解决方案,您可以尝试:

temp_variable=$(sed 's/"/\\"/g' <<< $temp)
if grep -q -F "$temp_variable" -a /PHPFiles/test.php;

因此,您首先使用 sed 转义双引号,并将结果存储在 temp_variable 中。然后在 grep 中使用 temp_variable。

关于regex - 需要使用grep在文件中查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24605139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com