Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
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.
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.
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
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.