LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 03-29-2006, 12:16 AM   #1
mbusche
LQ Newbie
 
Registered: Mar 2006
Posts: 2

Rep: Reputation: 0
changing group id within a script


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?

Thanks much,
Matt Busche
Lakewood CO
 
Old 03-29-2006, 01:55 AM   #2
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
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"
Hope this helps.

Last edited by rhoekstra; 03-29-2006 at 01:56 AM.
 
Old 03-29-2006, 07:59 AM   #3
mbusche
LQ Newbie
 
Registered: Mar 2006
Posts: 2

Original Poster
Rep: Reputation: 0
rhoekstra,

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:

[root@starburst src]# ls -l /usr/bin/sg
lrwxrwxrwx 1 root root 6 Apr 6 2004 /usr/bin/sg -> newgrp

(which is not to say it couldn't have different behaviors depending on $0 -- still I'm thinking i just don't have sg.)

I also saw your "header" solution. I just plain don't like that. I shouldn't have to force my users to do extra steps.

Thanks for your reponse
Matt Busche
 
Old 03-29-2006, 08:24 AM   #4
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
changing group twallstr Linux - Newbie 2 09-11-2005 03:00 PM
Changing group in Konqueror? miscreant Linux - Software 1 07-08-2005 05:42 PM
Changing user's group? orange400 Linux - General 2 06-18-2004 04:30 AM
Changing user group atze Linux - Newbie 12 06-08-2004 08:27 AM
changing the group anwar_lpk Linux - Networking 2 05-08-2003 10:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration