Solved

Global variable in Unix shell programming

Posted on 2004-08-23
9
1,103 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
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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now