The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do i set environment variable ahjiefreak Linux 4 01-20-2008 08:21 PM
sed on an environment variable ? lumix Shell Programming and Scripting 3 12-15-2007 06:30 PM
set environment variable? kohoutek UNIX for Advanced & Expert Users 3 12-21-2005 05:23 PM
Environment Variable karyn1617 Shell Programming and Scripting 2 02-04-2005 03:36 PM
Environment Variable jacobsa UNIX for Dummies Questions & Answers 8 06-03-2002 04:02 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2006
kaiser kaiser is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 2
Using Environment Variable

In our current environment we have each of our testing levels on individual servers (running Korn shell). So, there is a server for dev and test, and 2 servers for qa and prod. I have several scripts that utilize a code that is dependant on the server where it resides. While I was the only person using these scripts, I had that value hard coded in my scripts (they were quick and dirty scripts). Now we have several folks that will be supporting this and I want to keep the scripts in a central repository and change this value to be an environment variable.

My thought is to set this as an environment variable in the users .profile. Then the scripts will use that variable, instead of having to change it for each server/test level.

My assumption, and from everything I've read, is that if I export the variable in my profile, it should be available to scripts when executed. But, that is not what I am seeing. And these scripts are executed by the user from the command line.

Here is an example. Any help would be appreciated.

.profile:
VAR=server_a
export $VAR

script:
some_command $VAR #executes command passing in $VAR


When I do this, $VAR does not seem to contain server_a. It is empty instead. However, if I echo out $VAR at the command line, it does equal server_a.

A solution that does seem to work, is to set the variable in a .kshrc file and execute that in my script prior to using it. But I would prefer to have it set in the profile and have it available for the scripts.

Any help would be appreciated. Thanx
  #2 (permalink)  
Old 04-28-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,127
It is:
export var
not
export $var

But do you need that? The boxes should know their own names. I just use the hostname command when I want the hostname. "uname -n" is another possibility.
  #3 (permalink)  
Old 04-28-2006
buffoonix buffoonix is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 145
/etc/profile and $HOME/.profile are sourced for login (Bourne compatible) shells.
That's probably why you see the exported variables set when you echo them from your login shell.
On the other hand if you need special settings for your sub shells as well
then export ENV in your .profile and let it point to an appropiate .shrc file in your HOME
Refer to the FILES section in the manpage of your shell.
Also note that if your script is for instance run from a cron job that it only gets a pretty rudimentary environment (see man crontab).
As for host or node names,
I often have such an export in my .profile
(you may have to separate assignment and export to two lines, and use backticks
if you don't have a Posix shell)

export NODENAME=$(/usr/bin/uname -n)
  #4 (permalink)  
Old 05-01-2006
kaiser kaiser is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 2
OK, I feel like a moron. I dropped the $ from my export and it works fine. I should have caught that.

Thanks for the replies.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0