微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更改脚本中的默认组

是否可以在该脚本的执行期间更改脚本内的用户认组?

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

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

但是我想要 “groupb”。

myscript.sh:

用ps分组进程

在msysgit bash中,语法错误=〜运算符

如何在shell脚本中获取ec2-describe-instances的标签

使用pipe道时的string操作等基本名称

Bash – 如何使用printf打印多行string(带' n')

#!/bin/bash touch "myscript_output.txt"

前台产生并行进程

一个variables列表传递给简单的bash命令

如何在git中删除合并分支的别名

如何确定Linux中某些用户下运行的进程产生多lesscpu负载

使用for循环seq分配dynamicbashvariables名称

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

#!/bin/bash newgrp groupb << END touch "myscript_output.txt" END

该组可以从脚本中设置。 它只需要下面的“if”语句。 该组被检查,如果不正确,那么脚本将使用s命令Nate提到的重新启动。

使用循环检查(以防发生不可预见的情况)。

要使用,只需将组从“车轮”更改为所需的。 用正则代码替换“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,c}... ls -l my-$$.{a,c} echo rm -v my-$$.{a,c}

以下是打印出的一些测试运行,以解释为什么只是改变组我不足以确保文件有正确的组所有权。 目录权限也发挥作用。

这第一个日志是从一个普通的目录中破坏的输出。 该脚本以用户frayser运行,并分组frayser 。 使用所需的组创建文件。 比较下一个清单:

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'

在这个下一次运行发生在sid为 “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 $

由于目录权限,脚本可能需要显式设置权限和所有权。

通常可以通过应用程序来完成这些修改

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程序。 但是,这是kludgy。 搜索“setuid shell脚本”的网络 – 会有很多这样的示例C程序,并用getgrnam() + setgid()替换最常见的setuid(0) setgid() 。

sg命令可以做得很好。

#!/bin/bash sg groupb "touch myscript-output.txt"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐