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

前端 未结 8 1064
误落风尘
误落风尘 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:46

    This example was expanded from plinjzaad's answer; it handles a command line which contains quoted parameters that contain spaces.

    #!/bin/bash
    group=wg-sierra-admin
    if [ $(id -gn) != $group ]
    then
        # Construct an array which quotes all the command-line parameters.
        arr=("${@/#/\"}")
        arr=("${arr[*]/%/\"}")
        exec sg $group "$0 ${arr[@]}"
    fi
    
    ### now continue with rest of the script
    # This is a simple test to show that it works.
    echo "group: $(id -gn)"
    # Show all command line parameters.
    for i in $(seq 1 $#)
    do
        eval echo "$i:\${$i}"
    done
    

    I used this to demonstrate that it works.

    % ./sg.test  'a b' 'c d e' f 'g h' 'i j k' 'l m' 'n o' p q r s t 'u v' 'w x y z'
    group: wg-sierra-admin
    1:a b
    2:c d e
    3:f
    4:g h
    5:i j k
    6:l m
    7:n o
    8:p
    9:q
    10:r
    11:s
    12:t
    13:u v
    14:w x y z
    

提交回复
热议问题