LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 07-11-2010, 06:29 PM   #1
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Rep: Reputation: 16
Set environment variable by group


Hi guys, hopefully a quick one...

Is it possible to set an environment variable by goup rather then an individual .bash_profile or /etc/profile?

For instance I need every person in the call centre to have a specific env variable set but rather then set all of them individually I would rather just have everyone in the callcentre group have it.

Thanks guys!
 
Old 07-11-2010, 07:36 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,126

Rep: Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456
Quote:
Originally Posted by mhouston100 View Post
Hi guys, hopefully a quick one...

Is it possible to set an environment variable by goup rather then an individual .bash_profile or /etc/profile?

For instance I need every person in the call centre to have a specific env variable set but rather then set all of them individually I would rather just have everyone in the callcentre group have it.

Thanks guys!
Possible, sure, but you'll have to do a bit of tweaking. I did sort of the same thing for some 'problem users' I had (read: DBA's), and had to set certain restrictions. Put a small bit of shell-script into your /etc/profile (or whatever the system wide profile is...you don't provide version/distro of Linux or details, so can't say), to check the group membership of the user when they log in. If the group matches what you say, then just export the variables.
 
1 members found this post helpful.
Old 07-11-2010, 07:59 PM   #3
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by TB0ne View Post
Possible, sure, but you'll have to do a bit of tweaking. I did sort of the same thing for some 'problem users' I had (read: DBA's), and had to set certain restrictions. Put a small bit of shell-script into your /etc/profile (or whatever the system wide profile is...you don't provide version/distro of Linux or details, so can't say), to check the group membership of the user when they log in. If the group matches what you say, then just export the variables.
Ha ha sorry, not normally so obtuse with my posts, but up too early for the world cup final, head is full of sand.

I'm running RHEL 5.5 so /etc/profile is right, I thought that might be the case just running a check on the group membership and setting it depending on the result.

Thanks for the advice!

Last edited by mhouston100; 07-11-2010 at 08:00 PM. Reason: Spelling
 
Old 07-11-2010, 09:16 PM   #4
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Ok so the script I have worked out is basically:

Code:
username=`id -nu`
groups=`groups $username`

for word in $groups
do
case $word in group1) export variable=value1; ;; group2) export variable=value2 ;; done
Is this type of bash scripting ok to put into /etc/profile?

I guess I would need to use the full path to commands like "id -nu" due to the path variables not being set yet?

Maybe handy if I include more information:

Basically we have an application that assigns printers based on a certain environment variable. Rather then have to set the variable for every user we want to be able to assign the printers based on the groups.

I'm pretty sure this will accomplish that?

Last edited by mhouston100; 07-11-2010 at 09:26 PM.
 
Old 07-12-2010, 07:46 AM   #5
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,126

Rep: Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456
Quote:
Originally Posted by mhouston100 View Post
Ok so the script I have worked out is basically:

Code:
username=`id -nu`
groups=`groups $username`

for word in $groups
do
case $word in group1) export variable=value1; ;; group2) export variable=value2 ;; done
Is this type of bash scripting ok to put into /etc/profile?

I guess I would need to use the full path to commands like "id -nu" due to the path variables not being set yet?

Maybe handy if I include more information:

Basically we have an application that assigns printers based on a certain environment variable. Rather then have to set the variable for every user we want to be able to assign the printers based on the groups.

I'm pretty sure this will accomplish that?
On the right track, but I think you're looking for "id -ng" or "id -g". You're wanting the GROUP ID, so the "-ng" will return the text-based name, and the "-g" returns the numerical value of that group. The "-g" is probably easier to deal with...it returns the primary group ID. So, if GID=1000 (for example), set the variable, otherwise, don't. You can also do the "id -nG", which returns ALL the groups a person is a member of, in text-format. The idea here is to check the strings that are returned for something that matches, then act. Returning the numbers can be tricky here, since if you return "100 1000 25100", and you're only interested in getting "10"...the filter could return false results. Yes, you can adjust it, but a text-pattern can be easier to match in this case. Read the man pages for "id"

Try this:
Code:
groups=`id -nG`

if [[ $groups == *vid* ]]
then
     echo "In video group!";
fi
A simple check. Greps for the string, and sets a variable if it finds it. In this case, a simple check to see if you're a member of the video group. But in this case, it'll find video, vid, avideo, etc....ANYTHING with "vid" in it, so use caution, adjust as necessary.
 
1 members found this post helpful.
Old 07-12-2010, 07:54 PM   #6
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Ah maybe I didnt explain fully. Basically we will have around 20 groups and the strings will need to be set depending on which of these groups the user is in, its not just a single check. Thats why I was running with a 'case' test.

So for example we will have SYD-SALES, SYD-SERVICE, SYD-CALLCENTRE, SYD-IT... depending on which of these groups the useris in would depend on which printers its assigned to so:

For SYD-IT the env variables will be:
INVOICES=SYD-IT
PICKING=SYD-IT
PROPRINT=SYD-IT

Whereas SYD-SALES would be:
INVOICES=SYD-SALES
PICKING=SYD-SALES
PROPRINT=SYD-SALES

So I will be reading the user name, checking each group that the user is in one at a time, and if it matches any of the cases, then setting the correct variables.

The users will only be in one of the groups being tested for so there won't be any conflicts with setting the variables.

Seems the /etc/profile already sets a $USERS string anyway so I can drop the first part of the script.

What i'm now looking at is :


Code:
groups=`/usr/bin/groups $USER`

for word in $groups
do
    case $word in 
SYD-IT) export INVOICE=SYD-IT; export POPRINT=SYD-IT; export PICKING=SYD-IT; ;; SYD-SALES) export INVOICE=SYD-SALES; export POPRINT=SYD-SALES; export PICKING=SYD-SALES; ;; etc etc esac
done
The only thing I'm not 100% on is the whether the '/etc/profile' uses just normal bash scripting? no special syntax or anything like that?
 
Old 07-12-2010, 11:32 PM   #7
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Well all seems to be working now, just tested it on a clone of the active server and all is well. Thanks for your help!
 
Old 07-13-2010, 12:12 AM   #8
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,126

Rep: Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456
Quote:
Originally Posted by mhouston100 View Post
Well all seems to be working now, just tested it on a clone of the active server and all is well. Thanks for your help!
No problem...either syntax works, and (as you found out!), the standard BASH scripting language is all you need for the system-profile. It's a bit of a hack, but it's effective.

On a side note...back that up somewhere, and DOCUMENT it. Had that bite me one time. Restored a bunch of files, but that wasn't one of them. Some apps had all sorts of interesting problems, until we figured it out.
 
  


Reply

Tags
export, group, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to set an environment variable royeo Linux - Newbie 1 12-01-2006 12:59 AM
How to set a value of environment variable? ukrainet Linux - Newbie 1 12-28-2004 04:33 AM
set environment variable Jongi Linux - Newbie 7 03-01-2004 04:34 AM
How to set an environment variable? dantemarmol Linux - Newbie 7 08-22-2003 10:59 AM
How to set a environment variable in rh? yenonn Linux - Newbie 11 02-21-2003 10:24 PM


All times are GMT -5. The time now is 04:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration