Bash脚本:newgrp之后无法读取输入

时间:2019-10-20 00:52:09

标签: bash

我有一个bash脚本,它创建一个新用户并将当前的$USER添加到新创建的用户组中。然后,我想使用newgrp重新启动脚本,以使Shell对新用户拥有的文件具有读/写权限。

我希望终端上的人能够响应一些输入。当我使用newgrp重新启动脚本时,read将不再起作用。完全绕过了。

我在下面提供了一个显示相同行为的示例。

do_restart_script_with_updated_permissions () {
local cwd=$(pwd)
export SCRIPT_PATH="$cwd"
newgrp "newgroup" <<EONG
export NEW=true
cd $SCRIPT_PATH
./test.sh
EONG
}


if [ -z "$NEW" ]; then

    echo "We will restart the script."

    read

    do_restart_script_with_updated_permissions

else

    echo "Script restarted."

    # this does nothing
    read  -p "get some input" user_input

    echo "Done."
fi

这是脚本的输出:

./test.sh 
We will restart the script.

Script restarted.
Done.

1 个答案:

答案 0 :(得分:0)

sg命令执行此操作,使用此功能,一切正常:

do_restart_script_with_updated_permissions () {
exec sg newgroup $0
}

在这里找到答案

How do you use newgrp in a script then stay in that group when the script exits

相关问题