Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i read/set environment variables

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi all,

basicaly what i'm doing is writing a script that will check a environment variable periodically while running (looping)in the background (&)

i need to be able to set/change this variable with any script.

The variable name i have chosen to use is 'STATUS', which will have a number value of 0 - 2.

e.g.
#!/usr/bin/perl
start of loop
check STATUS env var
if (0)
sleep 60
exit
elseif (1)
do something
sleep longer
exit
fi
go to start of loop



Hope this is clear, any help appreciated

JayBot!
"Always know what you say, but don't always say what you know!"



 
You can check if a variable is defined with

if ( defined ($ENV{STATUS}) )
{
if ( $ENV{STATUS} eq 2 )
{
Do something
}
else
{
Something else
}
}

You can set the variable with
$ENV{STATUS} = "0";

I do not know if the variable will be visible to other processes because the for every process a new shell is created by unix. See a perl book about the %ENV variable:

The hash containing your current environment. Setting a value in %ENV changes the environment for child processes

I also do not know what this code will do on NT or other platforms.

succes.
 
Perl is very forgiving about undefined variables, so I don't think you really need to check of it's defined. If it's not defined, it will have a value of null ("") in a string context, or 0 in a numeric context. It doesn't hurt to check though, if you just want to be thorough. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top