the*_*ian 2 grep bash shell-script
我想要一个 bash 脚本,它:
我有以下内容,但脚本输出没有显示任何匹配项:
#!/bin/bash
echo "Searching files in directory for secrets and urls"
for file in ./*
do
echo "=====$file====="
strings ${file} | egrep -wi --color 'secret\|password\|key\|credential|\http'
done
Run Code Online (Sandbox Code Playgroud)
我也试过strings $file | egrep -wi --color 'secret\|password\|key\|credential|\http'
,eval "strings ${file} | egrep -wi --color 'secret\|password\|key\|credential|\http'"
但这些似乎不起作用。该脚本输出文件名,但不输出匹配项。
Kus*_*nda 10
您正在使用egrep
which 与 相同grep -E
,即它允许使用扩展的正则表达式。
在扩展的正则表达式中,|
是一个交替(这是你想要使用的),并\|
匹配一个文字|
字符。
你因此想要
grep -w -i -E 'secret|password|key|credential|http'
Run Code Online (Sandbox Code Playgroud)
或者
grep -i -E '\<(secret|password|key|credential|http)\>'
Run Code Online (Sandbox Code Playgroud)
where\<
和\>
匹配单词边界。
或者
grep -w -i -F \
-e secret \
-e password \
-e key \
-e credential \
-e http
Run Code Online (Sandbox Code Playgroud)
...如果你想做字符串比较而不是正则表达式匹配。
此外,您将希望始终双引号变量扩展。这将允许您还可以正确处理名称包含空格字符(空格、制表符、换行符)和名称包含文件名通配符 ( *
, ?
, [...]
) 的文件:
grep -w -i -E 'secret|password|key|credential|http'
Run Code Online (Sandbox Code Playgroud)
也可以看看