我正在solaris Box上运行一个脚本。特别是SunOS 5.7。我不是根。我正在尝试执行类似于以下内容的脚本:
新集团<< FOO 源.login_stuff 回音“你好世界” FOO
剧本开始了。问题是,它返回到调用过程,这使我在源.login_stuff未被源的情况下处于旧组中。我理解这种行为。我要找的是一个呆在潜艇壳里的方法。现在我知道我可以在脚本中放一个xterm& (参见下面),这样就可以了,但是有一个新的xterm是不可取的。
将当前pid作为参数传递。 新集团<< FOO 源.login_stuff xterm& 回声1美元 杀人-9美元1 FOO
我没有sg。另外,newgrp也是必要的。
发布于 2012-04-09 21:27:57
newgrp adm << ANYNAME
# You can do more lines than just this.
echo This is running as group \$(id -gn)
ANYNAME..will输出:
This is running as group adm小心--确保你用斜杠避开“$”。交互有点奇怪,因为它在作为另一个组执行shell之前甚至会扩展单引号。因此,如果您的主要组是“用户”,而您试图使用的组是“adm”,那么:
newgrp adm << END
# You can do more lines than just this.
echo 'This is running as group $(id -gn)'
END..will输出:
This is running as group users..because 'id -gn‘由当前shell运行,然后发送到以adm形式运行的shell。无论如何,我知道这个帖子是古老的,但希望这是有用的人。
发布于 2010-09-08 11:33:09
以下内容很好地工作;将以下部分放在(Bourne或Bash)脚本的顶部:
### first become another group
group=admin
if [ $(id -gn) != $group ]; then
exec sg $group "$0 $*"
fi
### now continue with rest of the script这对Linuxen来说很好。一个警告:包含空格的论点被分开了。我建议您使用envarg1=‘value 1’arg2='value 2‘script.sh构造来传递它们(由于某种原因,我无法让它与$@一起工作)
发布于 2008-11-18 19:47:58
newgrp命令只能从交互式shell AFAICT中有意义地使用。事实上,我放弃了关于.那么,让我们在很久以前说,我所写的替代者现在有资格在英国和美国进行投票。
请注意,newgrp是一个“内置在”shell中的特殊命令。严格地说,它是shell外部的一个命令,但是shell已经内置了如何处理它的知识。shell实际上是exec程序,因此您随后会得到一个新的shell。它也是一个setuid根程序。至少在Solaris上,newgrp似乎也忽略了SHELL环境变量。
我有各种各样的程序来解决newgrp想要解决的问题。记住,该命令比用户一次属于多个组的能力要早(请参阅第7版Unix手册)。由于newgrp不提供在执行命令后执行命令的机制,与su或sudo不同,我编写了一个程序newgid,与newgrp一样,它是一个setuid根程序,允许您从一个组切换到另一个组。它相当简单--只是main()加上一组标准的错误报告函数。联系我(第一个点,最后在gmail网站)的来源。我还有一个更危险的命令“asroot”,它允许我(但只有我-在默认编译下)更彻底地调整用户和组列表。
asroot: Configured for use by jleffler only
Usage: asroot [-hnpxzV] [<uid controls>] [<gid controls>] [-m umask] [--] command [arguments]
<uid controls> = [-u usr|-U uid] [-s euser|-S euid][-i user]
<gid controls> = [-C] [-g grp|-G gid] [-a grp][-A gid] [-r egrp|-R egid]
Use -h for more help
Option summary:
-a group Add auxilliary group (by name)
-A gid Add auxilliary group (by number)
-C Cancel all auxilliary groups
-g group Run with specified real GID (by name)
-G gid Run with specified real GID (by number)
-h Print this message and exit
-i Initialize UID and GIDs as if for user (by name or number)
-m umask Set umask to given value
-n Do not run program
-p Print privileges to be set
-r euser Run with specified effective UID (by name)
-R euid Run with specified effective UID (by number)
-s egroup Run with specified effective GID (by name)
-S egid Run with specified effective GID (by number)
-u user Run with specified real UID (by name)
-U uid Run with specified real UID (by number)
-V Print version and exit
-x Trace commands that are executed
-z Do not verify the UID/GID numbers
Mnemonic for effective UID/GID:
s is second letter of user;
r is second letter of group(这个程序增长了:如果我从头开始重做,我将接受用户ID或用户名,而不需要不同的选项字母;组ID或组名也是如此。)
要获得安装setuid根程序的许可是很棘手的。由于多组设施,现在有一些解决办法。一种可能有效的技术是在要创建文件的目录上设置setgid位。这意味着,不管是谁创建了该文件,该文件都属于拥有该目录的组。这通常能达到你所需要的效果--尽管我知道很少有人会一直使用它。
https://stackoverflow.com/questions/299728
复制相似问题