Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've written a shell script that automates the installation and configuration of a software system I'm working on. I run the script as root. I would like, as a first step in the script, to change the active group id from the default group of "root" to a different group. But I can find no way to do this within a shell script. A call to "newgrp" causes a subshell to be spawned and the script is halted waiting for that shell to exit.
Surely there's a simple way to do this. Who can help me?
Well, as 'man newgrp' learns me, you could use 'sg' to execute commands with another group.
You could easily build a header on your script that makes sure the group is set right.
example code: (assumption: you have a script with exec bits on and call it directly (like './<script>')
Code:
#!/bin/bash
new_group="users"
current_group=`groups|awk '{print $1}'`
if [ "$current_group" != "$new_group" ] ; then
echo "have to change from $current_group to $new_group, in order to run $0 $*"
sg $new_group -c $0 $*
echo "done with sg"
exit
fi
# I am in the new group.
echo "I am in a new group:"
id
echo "done with id"
Is there someway I can create a new user account "foo" with root privs but having a default group matching the one I desire? If so I could use something like:
su foo -c $*
In response to your proposals...
I saw the "sg" command during my Google attempts to resolve this problem, but curiously "man sg" on my ancient FC2 system simply brings up the man page for "newgrp" (with no reference made to sg). And when I try to run "sg" it acts just like newgrp -- the "command" arg appears to be ignored.
I presume "sg" just wasn't implemented on FC2. This claim is supported by the fact that sg is just a symlink to newgrp:
As I interpret your text in your first message, you created your own install script. This way you are in control of what the script has as its contents. So you can use whichever 'header' you like...
How do you mean that with my 'header' solution users would have to do extra steps? You say yourself you would want a first step to switch group. That's what this 'header' provides, or am I mis-interpreting your response?
About sg, it's indeed a symlink:
# ll `which sg`
lrwxrwxrwx 1 root root 6 Jun 2 2005 /usr/bin/sg -> newgrp
The man page says that sg allows a command to executes. I have proven that to myself using the above script on my own machine as a test.
Perhaps you should upgrade to a newer FC which has 'sg'.. it seems to solve your problem.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.