Solved

Global variable in Unix shell programming

Posted on 2004-08-23
9
1,155 Views
Last Modified: 2013-12-26
How to declare a variable as global in unix shell programming so that it can be accessible by other shell pgms?

-Thanks
0
Comment
Question by:Thilak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 50 total points
ID: 11869205
Hi Thilak,

> How to declare a variable as global in unix shell programming so that it can be accessible
> by other shell pgms?

There is no concept of declaring a variable in a script and you cannot access variables of one script from another ...
What you can do is output the values from the script and read them in another ... something like

var=`num1`

the backticks will cause execution of script num1 and resulting value will be stored in var ... you can access this value by using $var

echo $var

Sunnycoder
0
 
LVL 11

Expert Comment

by:avizit
ID: 11869258
or maybe you mean ENVIRONMENT VARIABLES

in that case check  http://www.ncl.ac.uk/ucs/unix/unixhelp/variables.html
0
 
LVL 11

Accepted Solution

by:
avizit earned 100 total points
ID: 11869267
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 11869281
Use environment variables with extreme caution ... moreover setting environment variables in a script will make it visible only to its children (sub shells) and not all other processes
0
 
LVL 11

Expert Comment

by:avizit
ID: 11869381
but it can be set in the

.cshrc file for csh and .bash_profile ( i think ) for bash  etc etc
0
 
LVL 2

Expert Comment

by:mansoor_a_khan
ID: 11876095
how abt setting it in .envfile is its for local user?
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 50 total points
ID: 11877004
1. Write the information in a file and have the other programs source it
i.e.  (assuming bourne shell)

cat << EOF > /path/to/filename
VAR1="$VAR1"
VAR2="$VAR2"
EOF

then
. /path/to/filename

within the other scripts to source filename into the current script

To make it visible to child processes, save the information in the environment, i.e.
export VAR1="blah"

then it's $VAR1
0
 
LVL 38

Assisted Solution

by:yuzh
yuzh earned 50 total points
ID: 11877032
I would put the ENV setting in files (one for sh/ksh/bash, another one for csh/tcsh)

eg:

for sh/ksh/bash

myenv.sh looks like:
LM_LICENSE_FILE=/opt/cds/share/license/license.807cf15c
CDS_LIC_TIMEOUT=300
export CDS_LIC_TIMEOUT LM_LICENSE_FILE
.....


for csh/tcsh
myenv.csh
setenv LM_LICENSE_FILE /opt/cds/share/license/license.807cf15c
setenv CDS_LIC_TIMEOUT 300
.....

To use them,

For sh/ksh/bash do:
. /path-to/myenv.sh

For csh/tcsh
source /path-to/myenv.csh

It will take care of the subshells!


0
 
LVL 6

Expert Comment

by:Talmash
ID: 11880095
hi ,

maybe u need setenv :

example :

setenv MY_VAR /project/leo102/gigemac/VERIF_TOOLS/Coverage/current/port

usage :

echo $MU_VAR

usage in perl :

$ENV{'MY_VAR'}

tal
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question