LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash commands in /etc/profile file (https://www.linuxquestions.org/questions/linux-general-1/bash-commands-in-etc-profile-file-844981/)

mhouston100 11-17-2010 11:20 PM

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!

barriehie 11-18-2010 12:17 AM

So can you export the variable and make it global?

Dark_Helmet 11-18-2010 12:19 AM

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 )


crashpoint_zero 11-18-2010 12:39 AM

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.

mhouston100 11-18-2010 03:21 PM

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.

catkin 11-18-2010 10:16 PM

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%_*}


All times are GMT -5. The time now is 08:35 PM.