Solved

Global variable in Unix shell programming

Posted on 2004-08-23
9
1,132 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ORA-01403: no data found 43 106
format the code in java 6 102
ffmpeg - "rtsp://...... Operation not permitted" 4 86
sum67 challenge 35 110
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…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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