【问题标题】:Bash Script: Cannot read input after newgrpBash 脚本:newgrp 后无法读取输入
【发布时间】:2019-10-20 00:52:09
【问题描述】:

我有一个 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.

【问题讨论】:

    标签: bash


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 2020-06-18
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多