代码之家  ›  专栏  ›  技术社区  ›  Jim Hunziker

为Unix组的成员设置mercurial

  •  1
  • Jim Hunziker  · 技术社区  · 15 年前

    我在Linux服务器上设置了一个mercurial存储库,一些(但不是全部)用户有权推送它。它们通过ssh连接到存储库。

    这些用户是一个Unix组的成员。下面是我用来修改存储库的脚本,允许它接收来自它们的推送。

    这个可以改进吗?这里有没有不必要的操作?有什么不好的款式吗 bash 脚本?

    #!/bin/bash                                                                     
    
    if [[ $# -lt 2 ]]; then
        echo Usage: $0 directory groupname
        exit 1
    fi
    
    if ! chown -R :$2 $1; then
        echo chown failure
        exit 2
    fi
    
    if ! find $1/.hg -type d -exec chmod g+s {} \;; then
        echo chmod failure
        exit 3
    fi
    
    if ! find $1 -perm -u+r -exec chmod g+r {} \;; then
        echo chmod failure 2
        exit 4
    fi
    
    if ! find $1 -perm -u+w -exec chmod g+w {} \;; then
        echo chmod failure 3
        exit 5
    fi
    
    if ! find $1 -perm -u+x -exec chmod g+x {} \;; then
        echo chmod failure 4
        exit 6
    fi
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Ry4an Brase    15 年前

    运行find with-exec为每个文件启动一个单独的chown进程。如果你这样做的话,你会得到更少的过程冲击(和更快的速度):

    find "$1"/.hg -type d -print0 | xargs chmod g+s
    find "$1" -perm -u+r -print0 | xargs chmod g+r
    find "$1" -perm -u+w -print0 | xargs chmod g+w
    find "$1" -perm -u+x -print0 | xargs chmod g+x
    

    作为旁白,你看过Mercurial的吗? ACL Extension hg-ssh ?只要唯一的访问权是ssh,它就做同样的事情。

        2
  •  1
  •   John Kugelman Michael Hodel    15 年前

    有几个小问题:通过重定向来将错误消息回送到stderr是个好主意。 >&2 .并且应该在变量周围添加双引号,这样脚本就可以处理具有空格的文件名。

    您可以将初始行更改为 #!/bin/bash -e 如果出现错误,立即退出脚本。这样你就可以把所有的 if 声明。或者如果你想要更多的控制,你可以使用 trap ERR 调用自定义错误处理代码的命令:

    #!/bin/bash
    
    function uhoh() {
        echo "error in script!" >&2
        exit 1
    }
    
    trap uhoh ERR
    
    if [[ $# -lt 2 ]]; then
        echo "Usage: $0 directory groupname" >&2
        exit 1
    fi
    
    chown -R :"$2" "$1"
    find "$1"/.hg -type d -exec chmod g+s {} \;
    find "$1" -perm -u+r -exec chmod g+r {} \;
    find "$1" -perm -u+w -exec chmod g+w {} \;
    find "$1" -perm -u+x -exec chmod g+x {} \;
    

    就我个人而言,我只想 /bin/bash -e 如果您希望在出现故障时立即停止脚本,则选择此选项。我认为检查每个命令的结果,并为每个命令使用不同的退出代码是过分的。你不需要那么健壮,没有人会对退出代码3和退出代码4做任何不同的事情…