Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Global variable in Unix shell programming

Posted on 2004-08-23
9
Medium Priority
?
1,175 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 150 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 300 total points
ID: 11869267
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 150 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 150 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

722 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