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

前端 未结 8 995
误落风尘
误落风尘 2021-01-02 02:21

I am running a script on a solaris Box. specifically SunOS 5.7. I am not root. I am trying to execute a script similar to the following:

相关标签:
8条回答
  • 2021-01-02 02:55

    The following works nicely; put the following bit at the top of the (Bourne or Bash) script:

    ### first become another group
    group=admin
    
    if [ $(id -gn) != $group ]; then
      exec sg $group "$0 $*"
    fi
    
    ### now continue with rest of the script
    

    This works fine on Linuxen. One caveat: arguments containing spaces are broken apart. I suggest you use the env arg1='value 1' arg2='value 2' script.sh construct to pass them in (I couldn't get it to work with $@ for some reason)

    0 讨论(0)
  • 2021-01-02 02:56
    sudo su - [user-name] -c exit;
    

    Should do the trick :)

    0 讨论(0)
提交回复
热议问题