代码之家  ›  专栏  ›  技术社区  ›  Nate

更改脚本中的默认组

  •  15
  • Nate  · 技术社区  · 14 年前

    在脚本执行期间,是否可以更改脚本中用户的默认组?

    我需要在一个脚本中生成具有适当用户和组的文件,但我的用户的主要组不是应该拥有结果输出的人。

    $ groups
    groupa groupb
    
    $ ./myscript.sh
    
    $ ls -l
    -rw-r--r-- 1 me groupa     0 Sep 17 09:42 myscript_output.txt
    

    但我 希望 “B组”。

    myscript.sh:

    #!/bin/bash
    
    touch "myscript_output.txt"
    
    4 回复  |  直到 14 年前
        1
  •  19
  •   dogbane    14 年前

    尝试 newgrp 命令,将用户的主要组更改为该用户所属的另一组:

    #!/bin/bash
    
    newgrp groupb << END
        touch "myscript_output.txt"
    END
    
        2
  •  4
  •   frayser    14 年前

    可以从脚本设置组。它只需要“如果” 陈述如下。检查组,如果不正确,则 使用上述sg命令nate重新启动脚本。
    使用循环检查(以防不可预见的情况发生)。

    要使用,只需将组从“wheel”更改为所需。用常规代码替换“demo”部分。

    继续阅读下面的内容(在脚本之后)。

    #! /bin/sh
    #    
    # If the group(set with NEEDGRP) is already correct, or this code has already
    # run, then this section is skipped and the rest of the 
    # script is run; otherwise sg is called to restart the script with the
    # desired group.  Assumes the command "id -ng" returns the group.
    
    if ! [ "${SBREADY:=false}" = true -o $(id -ng) = ${NEEDGRP:=wheel} ] ; then
        export SBREADY=true
        exec sg $NEEDGRP "$0" "$@"
    fi
    
    # ---------------------- DEMO: CUT HERE ---------------------------
    # This is a demonstration of creating files.
    echo HELLO my group is $(id -ng), GID=$(id -g)
    # NOTE: files are created with the current group only if the directory
    # is not sgid.
    # Show current directory and permissions on it
    echo
    pwd -P
    ls -ld .
    echo
    # Create and list some new files, the remove them.
    touch my-$$.{a,b,c}
    echo Created  my-$$.{a,b,c}...
    ls -l  my-$$.{a,b,c}
    echo
    rm -v  my-$$.{a,b,c}
    

    以下是一些测试的打印输出,这些测试运行的目的是为了解释为什么仅仅更改组就不足以确保文件拥有正确的组所有权。目录权限也开始发挥作用。

    第一个日志是在常规目录中销毁的输出。脚本以用户身份运行 弗雷泽 ,和组 弗雷泽 。文件已创建 与所需的组。与下一个列表比较:

    frayser@gentoo ~/src/Answers $ (cd /tmp; $OLDPWD/set-group.sh)
    HELLO my group is wheel, GID=10
    
    /tmp
    drwxrwxrwt 16 root root 976 Sep 24 04:45 .
    
    Created my-19201.a... my-19201.b... my-19201.c...
    -rw-r----- 1 frayser wheel 0 Sep 24 04:53 my-19201.a
    -rw-r----- 1 frayser wheel 0 Sep 24 04:53 my-19201.b
    -rw-r----- 1 frayser wheel 0 Sep 24 04:53 my-19201.c
    
    removed `my-19201.a'
    removed `my-19201.b'
    removed `my-19201.c'
    

    现在,下一次运行发生在 SGID “conman”是因为作为一种策略,配置管理被授予所有 SRC 目录。 注: 文件继承目录组。

    frayser@gentoo ~/src/Answers $ ./set-group.sh 
    HELLO my group is wheel, GID=10
    
    /usr/lucho/src/frayser/practice
    drwxr-s--- 6 frayser conman 768 Sep 24 04:51 .
    
    Created my-19214.a... my-19214.b... my-19214.c...
    -rw-r----- 1 frayser conman 0 Sep 24 04:54 my-19214.a
    -rw-r----- 1 frayser conman 0 Sep 24 04:54 my-19214.b
    -rw-r----- 1 frayser conman 0 Sep 24 04:54 my-19214.c
    
    removed `my-19214.a'
    removed `my-19214.b'
    removed `my-19214.c'
    frayser@gentoo ~/src/Answers $ 
    

    由于目录权限的原因,脚本可能需要 明确设置权限和所有权。

        3
  •  2
  •   Nate    14 年前

    这个 sg 命令可以很好地做到这一点。

    #!/bin/bash
    
    sg groupb "touch myscript-output.txt"
    
        4
  •  1
  •   Dummy00001    14 年前

    通常情况下,可以通过应用于程序的修改来完成:

    chgrp groupb myprog
    chmod g+s myprog
    

    但这适用于普通程序,而不是shell脚本(出于安全原因)。对于shell脚本,除了从脚本内部调用 chgrp :

    #!/bin/bash
    
    FNAME="myscript_output.txt"
    GRP=groupb
    
    touch $FNAME
    chgrp $GRP $FNAME || { echo 2>&1 "Can't change group of $FNAME to $GRP"; exit 1; }
    

    (*)有些人专门编写了一个小的包装器C程序。但这是个蹩脚的动作。搜索“setuid shell脚本”的网络-会有很多这样的示例C程序,并替换在那里最常见的程序 setuid(0) 具有 getgrnam() + setgid() .