समूह को एक स्क्रिप्ट से सेट किया जा सकता है। इसे केवल "अगर" कथन की आवश्यकता है। समूह की जांच की गई है और यदि यह गलत है, तो स्क्रिप्ट को sg कमांड नाइट के साथ पुनरारंभ किया गया है।
लूपिंग के लिए एक चेक नियोजित है (केवल अप्रत्याशित होता है।)
उपयोग करने के लिए, समूह को "व्हील" से वांछित में बदलें। नियमित कोड के साथ "डेमो" खंड को बदलें।
पर पढ़ें, नीचे (स्क्रिप्ट के बाद।)
#! /bin/sh
#
# If the group(set with NEEDGRP) is already correct, or this code has already
# run, then this section is skipped and the rest of the
# script is run; otherwise sg is called to restart the script with the
# desired group. Assumes the command "id -ng" returns the group.
if ! [ "${SBREADY:=false}" = true -o $(id -ng) = ${NEEDGRP:=wheel} ] ; then
export SBREADY=true
exec sg $NEEDGRP "$0" "[email protected]"
fi
# ---------------------- DEMO: CUT HERE ---------------------------
# This is a demonstration of creating files.
echo HELLO my group is $(id -ng), GID=$(id -g)
# NOTE: files are created with the current group only if the directory
# is not sgid.
# Show current directory and permissions on it
echo
pwd -P
ls -ld .
echo
# Create and list some new files, the remove them.
touch my-$$.{a,b,c}
echo Created my-$$.{a,b,c}...
ls -l my-$$.{a,b,c}
echo
rm -v my-$$.{a,b,c}
निम्नलिखित कुछ परीक्षण के प्रिंटआउट क्रम में चलाने के समझाने के लिए क्यों न बदल रहा समूहों पर्याप्त सुनिश्चित करने के लिए फ़ाइलों को सही समूह स्वामित्व है होनी चाहिए न की कर रहे हैं। निर्देशिका अनुमतियां भी खेल में आती हैं।
यह पहला लॉग नियमित निर्देशिका में बर्बाद होने से आउटपुट है। स्क्रिप्ट उपयोगकर्ता frayser, और समूह frayser के रूप में चलाया जाता है। वांछित समूह के साथ फ़ाइलें बनाई गई हैं।अगले सूची से तुलना करें:
[email protected] ~/src/Answers $ (cd /tmp; $OLDPWD/set-group.sh)
HELLO my group is wheel, GID=10
/tmp
drwxrwxrwt 16 root root 976 Sep 24 04:45 .
Created my-19201.a... my-19201.b... my-19201.c...
-rw-r----- 1 frayser wheel 0 Sep 24 04:53 my-19201.a
-rw-r----- 1 frayser wheel 0 Sep 24 04:53 my-19201.b
-rw-r----- 1 frayser wheel 0 Sep 24 04:53 my-19201.c
removed `my-19201.a'
removed `my-19201.b'
removed `my-19201.c'
अब यह अगले रन भी निदेशक हैं कि sgid "ठग" क्योंकि एक नीति, विन्यास प्रबंधन सभी src निर्देशिका के समूह के स्वामित्व के रूप में दिया जाता है में होता है। नोट: फ़ाइलें निर्देशिका के समूह को प्राप्त करती हैं।
[email protected] ~/src/Answers $ ./set-group.sh
HELLO my group is wheel, GID=10
/usr/lucho/src/frayser/practice
drwxr-s--- 6 frayser conman 768 Sep 24 04:51 .
Created my-19214.a... my-19214.b... my-19214.c...
-rw-r----- 1 frayser conman 0 Sep 24 04:54 my-19214.a
-rw-r----- 1 frayser conman 0 Sep 24 04:54 my-19214.b
-rw-r----- 1 frayser conman 0 Sep 24 04:54 my-19214.c
removed `my-19214.a'
removed `my-19214.b'
removed `my-19214.c'
[email protected] ~/src/Answers $
क्योंकि निर्देशिका अनुमतियों की, यह स्पष्ट रूप से सेट अनुमतियाँ और स्वामित्व के लिए एक स्क्रिप्ट के लिए आवश्यक हो सकता है।
ऐसे सिस्टम नहीं हैं जहां मैं परीक्षण करने के लिए अभी रूट पर हूं (मेरे वर्तमान खाते में केवल एक समूह है)। 'Chgrp groupb myscript.sh' +' chmod g + s myscript.sh' आज़माएं। उस समूह आईडी को बदलना चाहिए जिसके अंतर्गत स्क्रिप्ट चलती है - और उम्मीद है कि नई फाइलें बनाते समय समूह का उपयोग किया जाएगा। – Dummy00001