LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-17-2010, 11:20 PM   #1
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Rep: Reputation: 16
Bash commands in /etc/profile file


Hi guys, this didnt really fit in the programming section so I'll post here. Hopefully a nice easy one.

Basically, I'm trying to work some logic into the /etc/profile file when setting some environment variables based on group membership. The problem I'm having is because this isnt a straight bash script, trying to manipulate a string as I normally would isnt working. (I THINK its because i'm trying to process a local variable from inside the script, outside of the script)

For instance I'm trying to truncate a variable.

The variable value is SYD_ADMIN which I'm trying to shorten to SYD.

Not so hard 'echo $variable |cut -c -3'

But this doesnt seem to work in this /etc/profile file. Im not sure how the command logic goes. Anyone have any ideas? or point me in the right direction.

Or even still, should I be doing this in a completely different way?

Thanks in advance champs!
 
Old 11-18-2010, 12:17 AM   #2
barriehie
Member
 
Registered: Nov 2010
Distribution: Debian Lenny
Posts: 136
Blog Entries: 1

Rep: Reputation: 23
So can you export the variable and make it global?
 
1 members found this post helpful.
Old 11-18-2010, 12:19 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by mhouston100
For instance I'm trying to truncate a variable.

The variable value is SYD_ADMIN which I'm trying to shorten to SYD.

Not so hard 'echo $variable |cut -c -3'
Are you using single quotes (') or backticks (`) to surround the echo-cut command? You need to use backticks to get command substitution to work--or use the newer form: $()

For example:
Code:
trunced_variable=`echo ${variable} | cut -c -3`

-or-

trunced_variable=$( echo ${variable} | cut -c -3 )
 
1 members found this post helpful.
Old 11-18-2010, 12:39 AM   #4
crashpoint_zero
LQ Newbie
 
Registered: Nov 2010
Posts: 5

Rep: Reputation: 1
Misunderstood the question. And dark helmet already solved it.

I thought you wanted to truncate a variable name. Food for thought though:

What interests me more is why would you want to truncate a variable name?

But as the above post says, all of the bash initialization files - profile, .bashrc - after setting the values export them so that they are available to any shells that spawn out after that.

I don't see a need to truncate a varname but if you have to here's a sample:

Say you want to truncate the variable "$this" to "$is"

$this=12
$echo $this
12
$eval export `echo "this" | cut -c3-`=12
$echo $is
12

This gives you $is in current and any other shells that spawn out of this one.

Hope this helps.

Last edited by crashpoint_zero; 11-18-2010 at 12:41 AM.
 
1 members found this post helpful.
Old 11-18-2010, 03:21 PM   #5
mhouston100
Member
 
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Whew, must have been a goddamned spelling mistake or something.

I have it sorted now with:

export tmpgrp=`echo $grp |cut -c -3`

So it all seems to be workign the way I want it.

The reason I'm cutting it is that all the users are in groups like syd_admin, syd_service, syd_store etc and I need to assign some variables based on the branch which is always the first 3 characters 'syd'.

Unless I'm doing this the complete wrong way which is entirely probable I'm not exactly a jedi when it comes to scripting, this has just sort of pushed into my lap...

But thanks for all the help guys, it got me sorted.
 
Old 11-18-2010, 10:16 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If the system has an /etc/profile.d directory then there are advantages in creating a new file in there for your changes. If not then in creating a new file (say /etc/profile.local) and modify /etc/profile to source it. The advantages are that it is then simple a) to identify the local changes and b) to switch them off for problem investigation.

If you are only running bash (or whatever other shells support it) a faster solution is export tmpgrp=${variable%_*}. It is faster because it does not spawn any child processes while the $( | } and equivalent ` | ` method spawns two.

There is a convention of using uppercase for environment variable names (example: output of env command). If you like that convention you may prefer export TMPGRP=${variable%_*}

Last edited by catkin; 11-18-2010 at 10:17 PM. Reason: B -> b
 
  


Reply

Tags
environment, profile, variable



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running bash but common bash commands not working michellepace Linux - Newbie 11 07-17-2010 09:19 AM
Need help about bash profile file. gardenair Linux - Newbie 6 03-05-2010 07:50 AM
[Bash] - sed - read commands from file mrq Programming 4 08-24-2009 06:43 AM
Bash Profile ...... babujd Linux - Newbie 16 07-23-2009 06:25 PM
how to edit file from bash commands legend1079 Linux - Newbie 3 03-03-2004 01:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:47 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