【问题标题】:writing a shell script if statement to check for directory编写一个 shell 脚本 if 语句来检查目录
【发布时间】:2012-08-22 09:50:41
【问题描述】:

我需要编写一个脚本来重新创建我的 opt 文件夹,如果我从中删除一个包时它被删除了。这是我之前帖子的链接:dpkg remove to stop processes

现在,我遇到的问题可以在这里更好地描述:http://lists.debian.org/debian-devel/2006/03/msg00242.html

我正在考虑添加一个 postrem 脚本来检查 opt 目录是否存在,如果不存在,则创建一个。不过,我对 shell 脚本的经验非常有限..

【问题讨论】:

    标签: shell package uninstallation dpkg


    【解决方案1】:
    [ -d "$dir" ] || mkdir -p "$dir"
    

    这可以写得更详细/更清楚:

    if ! test -d "$dir"; then
        mkdir -p "$dir"
    fi
    

    更多信息请参见help test。

    【讨论】:

    • 由于mkdir -p $dir 是幂等的,所以不需要先测试目录。只需mkdir -p "$dir" 就足够了。
    • 你是对的。我将保留我的答案,因为它说明了一个有用的结构。
    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2012-09-23
    相关资源
    最近更新 更多