AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 110503
In Process
su
su
Asked: 2010-02-08 17:46:21 +0800 CST2010-02-08 17:46:21 +0800 CST 2010-02-08 17:46:21 +0800 CST

使用 find 和 diff 命令帮助 bash 脚本

  • 772

我有一个需要帮助的 bash 脚本:

#!/bin/bash
if [ -f "/suid.old" ]
then
find / -perm -4000 -o -perm -2000 ls > suid.old
else
find / -perm 4000 -o -perm -2000 ls > suid.new

diff suid.old suid.new > newchanges.list
fi

当我运行它时,它给了我一个错误消息:diff:suid.old:没有这样的文件或目录。

我的脚本应该说,如果 suid.old 不存在,则使用 find 命令创建一个,或者使用 find 命令对 suid.new 执行所需的任何操作。找到它所做的任何更改并将其重定向到 newchanges.list

请帮忙,

bash diff find
  • 3 3 个回答
  • 1249 Views

3 个回答

  • Voted
  1. Dennis Williamson
    2010-02-08T18:36:44+08:002010-02-08T18:36:44+08:00

    if从语句中的文件名中删除斜杠。您拥有它的方式是检查根目录中的文件,但稍后它会在当前目录中创建。

    此外,您的脚本基本上说“如果 suid.old不存在,那么做一个差异”。

    你可能想要这样的东西:

    #!/bin/bash
    if [ ! -f "suid.old" ]
    then
        find / -perm -4000 -o -perm -2000 ls > suid.old
    fi
    
    if [ ! -f "suid.new" ]
    then
        find / -perm 4000 -o -perm -2000 ls > suid.new
    fi
    
    diff suid.old suid.new > newchanges.list
    mv suid.new suid.old
    

    这说:“如果 suid.old 不存在,创建它。如果 suid.new 不存在,创建它。现在它们已经创建(或已经存在)做差异。”

    • 1
  2. Zypher
    2010-02-08T18:14:01+08:002010-02-08T18:14:01+08:00

    从查找行中删除“ls”。他们应该看起来像find / -perm -4000 -0 -perm -2000 > suid.old

    你设置它的方式 find 认为 ls 是一个路径参数。由于 find 将结果打印到 STDOUT,因此只需执行正常重定向即可获得所需的输出。

    • 0
  3. kubanczyk
    2010-02-09T04:57:21+08:002010-02-09T04:57:21+08:00

    我的版本:

    #!/bin/bash
    
    if [ ! -f "/suid.old" ]; then                          # note: !
       find / -perm -4000 -o -perm -2000 -ls > /suid.old   # note: dash before ls; slash before file
    else
       find / -perm 4000 -o -perm -2000 -ls > /suid.new
       diff /suid.old /suid.new > /newchanges.list
       mv /suid.new /suid.old                              # could be handy
    fi
    
    • 0

相关问题

  • Mac OS X:从 python 脚本中更改 $PATH

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • crontab ifconfig 什么都不输出

  • 使用命令行工具按排序顺序计算重复项

  • 是否有 bash 等效于 ruby​​ 的“一些内容#{foo}”?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve