linux - 执行脚本时没有那个文件或目录

标签 linux bash

我有一个文件夹 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/

相关文章:

linux - 如何检查变量是否等于 C 中的文本?

c - 选择return 0 but data can be read

Linux 按列合并两个文件

c - 在 Linux : unconsumed input is sent to bash 中使用系统调用 READ 读取 STDIN

bash - bash 的开始结束还是括号?

linux - Guess_number 脚本将 WHILE 添加到 guess_number 中并运行它

c - dma_mmap_coherent 和 remap_pfn_range 有什么区别?

regex - 如何在 bash 中用单个空行替换多个空行?

c++ - C++ 中的路径清理

bash - 获取-bash : mvn: command not found