7.2 Environment Object

The Environment component is used to set and get the values of environment variables. By default every NSN script has an instance of this component as a global object named ENV that can access and change the NSN shell environment.

7.2.1 Clear method

Clears the values of all the Environment variables.

Syntax

 object.Clear()
 

Parameters

None.

Return Values

Boolean. TRUE indicates that the environment variables are cleared.

Example

This example clears all the environment variables of the shell.

 ’Displays all the environment variables. 
 ’After executing this sample, exit and invoke the shell again. 
 name = Env.First 
 While name <> "" 
     value = Env.Get(name) 
     Print name & "  " & value 
     name = Env.Next 
 Wend 
 ’Clears all the environment variables and verify 
 Print "Clearing all the environment variables..." 
 env.Clear 
 name = Env.First 
 While name <> "" 
     value = Env.Get(name) 
     Print name & "  " & value 
     name = Env.Next 
 Wend
 

7.2.2 First method

Returns the first environment variable of the shell.

Syntax

 object.First()
 

Parameters

None.

Return Values

Name and Value, in the form name=value.

Example

This example displays all the environment variables including the first one.

 ’Display all the environment variables 
 print "Environment Variables: " 
 name = Env.First 
 print "The first variable is name & "  " & value 
 While name <> "" 
     value = Env.Get(name) 
     print name & "  " & value 
     name = Env.Next 
 Wend
 

7.2.3 Next method

Returns the next environment variable; if no more variables are present, it returns NULL.

Syntax

 object.Next()
 

Parameters

None.

Return Values

Name and Value, in the form name=value.

Example

This example displays all the environment variables.

 ’Display all the environment variables 
 print "Environment Variables: " 
 name = Env.First 
 While name <> "" 
     value = Env.Get(name) 
     print name & "  " & value 
     name = Env.Next 
 Wend
 

7.2.4 Get method

Gets the value of a specified environment variable. It returns NULL if no variable is found.

Syntax

 object.Get(
    Key As String)
 

Parameters

Key
The variable to get.

Return Values

String. Value of the specified variable.

Example

This example creates the ROOT_DIR environment variable and gets it.

 ’Create a new variable ROOT_DIR 
    env.set ("ROOT_DIR", "Sys:NSN") 
    ’Print the value 
 value=env.get ("ROOT_DIR") 
    print "Value of ROOT_DIR: " &value
 

7.2.5 Set method

Changes and sets the environment variable according to the specified value. If value is not specified then it clears the environment variable.

Syntax

 object.Set(
    Key As String,  
    [Value As String])
 

Parameters

Key
The variable to set.
Value
The value that the specified environment variable takes.

Return Values

Boolean. TRUE indicates that the environment variables are set.

Example

This example creates a variable called ROOT_DIR and assigns a value Sys:NSN. It also shows how to clear the variable.

 ’Create a new variable ROOT_DIR 
 env.set ("ROOT_DIR", "Sys:NSN") 
 ’Print the value 
 value=env.get ("ROOT_DIR") 
 print "Value of ROOT_DIR: " &value 
 ’Clear the value 
 env.set ("ROOT_DIR") 
 ’Print the value 
 value=env.get ("ROOT_DIR") 
 if (value = "") Then 
    print "Environment variable ROOT_DIR is cleared" 
 End if