0

I have a bash script which creates a new user and adds the current $USER to newly created user group. I then want to restart the script using newgrp so that the shell has read/write permissions to files own by the new user.

I am expecting the person at the terminal to respond to some input. When I restart the script using newgrp then read no long works. It is completely bypassed.

I have included an example below which exhibits the same behavior.

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

Here is the output of the script:

./test.sh 
We will restart the script.

Script restarted.
Done.

1 Answers1

0

The sg command does this, everything works fine using this function:

do_restart_script_with_updated_permissions () {
exec sg newgroup $0
}

Answer found here:

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