我有一个文件夹 bin/bash,我在其中放置了这个脚本:mal_test.sh
if grep 'php \$[a-zA-Z]*=.as.;' "$1" >/dev/null
then
"!!! $1 is malware!!!"
else
" $1 is fine."
fi
我从不同的目录运行以下命令:
find -type f -exec ./mal_test.sh '{}' ';'
但我发现错误:./mal_test
- No such file or directory
我猜脚本被解释为一个文件...我该如何解决?
谢谢!
最佳答案
有不少异常……
1) 您在 grep 中使用了扩展的正则表达式。所以你应该使用 -E 选项
。
2) 在您的查找命令中而不是引用 ';'
做 \;
3) 由于您将脚本放在 bin/bash 目录中,因此请确保该路径已添加到您的 PATH 环境变量中。另一种方法是提供脚本所在的完整路径。
4) if then else
循环中没有echo
语句。
5) 在您的查找命令中,您需要提供要查找文件的路径。使用 .
作为当前目录。例如 查找。 -type f -exec ./script.sh {}\;
我对您的脚本进行了以下更改(您可以添加自己的 RegEx)。这就是我得到的 -
[jaypal~/Temp]$ cat script.sh
#!/bin/bash
if grep -E '[a-zA-Z]*' ${1} >/dev/null
then
echo "!!! $1 is malware!!!"
else
echo " $1 is fine."
fi
输出:
[jaypal~/Temp]$ find . -type f -exec ./script.sh {} \;
./.mp3 is fine.
./1 is fine.
!!! ./a.sh is malware!!!
!!! ./ab.sed is malware!!!
!!! ./b.sh is malware!!!
!!! ./backup/GTP_Identifier.sh is malware!!!
!!! ./backup/GTP_Parser.sh is malware!!!
!!! ./backup/split.pl is malware!!!
!!! ./backup/T20111017153052.NEW is malware!!!
!!! ./backup/T20111025172411.NEW is malware!!!
!!! ./backup/T20111107193400.NEW is malware!!!
关于linux - 执行脚本时没有那个文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8250976/