unix - 您如何在脚本中使用 newgrp 然后在脚本退出时留在该组中

标签 unix shell solaris csh

我在solaris Box 上运行脚本。特别是 SunOS 5.7。我不是root。我正在尝试执行类似于以下内容的脚本:

newgrp thegroup << FOO
source .login_stuff
echo "hello world"
FOO



脚本运行。问题是它返回到调用过程,这使我进入了旧组,而源 .login_stuff 没有被提供。我理解这种行为。我正在寻找的是一种留在子 shell 中的方法。现在我知道我可以在脚本中放置一个 xterm&(见下文),这样就可以了,但是拥有一个新的 xterm 是不可取的。

Passing your current pid as a parameter.

newgrp thegroup << FOO
source .login_stuff
xterm&
echo $1
kill -9 $1
FOO



我没有可用的 sg。
此外,newgrp 是必要的。

最佳答案

以下工作很好;将以下位放在(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 上运行良好。一个警告:包含空格的参数被分解。我建议你使用
env arg1='value 1' arg2='value 2' script.sh 构造来传递它们(由于某种原因,我无法让它与 $@ 一起使用)

关于unix - 您如何在脚本中使用 newgrp 然后在脚本退出时留在该组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/299728/

相关文章:

linux - 如何以表格格式邮寄脚本输出

shell - "Pipe stderr, and not stdout"在 Zsh 中不工作

unix - 如何让 find 命令打印出文件大小和文件名?

c - 在 macOS 上编译 XDR 时出现问题(RPC types.h 问题)

bash - Hyperledger Fabric 设置下载特定平台二进制文件权限错误 OSX

shell - 列出busybox中的所有环境变量名

linux - 我正在尝试使用 awk 旋转文件

c - 重定向标准输出时删除了日志文件

Linux shell 总和列

solaris - 多线程 printf 与 write(2) 缓冲