10.2 Using Variables

   

10.2 Using Variables

Variables can be set and used in the same way you have used them on the command line. Any variable that is set during the execution of a shell program is not visible after the execution is finished. Shell programs can read environment variables and can also modify their values for the duration of the execution of the program. Variables are also a useful way of passing data to shell programs. Let us see another program named script-01 where we have set two variables TAB and FUR . These variables are then displayed using the echo command. The program is shown below.

 $  cat script-01  #!/usr/bin/sh echo "Use of Variables" echo "----------------" echo TAB=table FUR=furniture echo "The $TAB is an example of $FUR" $ 

When this program is executed, the results are:

 $  ./script-01  Use of Variables ---------------- The table is an example of furniture $ 

Note that these two variables are not available after the execution of the program is finished. If you try to display the values of any of these variables from the command line after executing the shell program, you will get the following message.

 $  echo $TAB  sh: TAB: Parameter not set. $ 

Now let us try to change the value of the TAB variable within the program from " table " to " chair " using the following program example (script-02) .

 $  cat script-02  #!/usr/bin/sh echo "Use of Variables" echo "----------------" echo TAB=table FUR=furniture echo "The $TAB is an example of $FUR" TAB=chair Echo "After change" echo "The $TAB is an example of $FUR" $ 

When this program is executed, the results are as follows . Note that the line used for printing the text is the same; only the variable value is changed.

 $  ./script-01  Use of Variables ---------------- The table is an example of furniture After change The chair is an example of furniture $ 

Passing Data Through Environment Variables

Shell programs can access environment variables. The example below is script-03 , which prints the values of the PATH and TERM environment variables.

 $  cat script-03  #!/usr/bin/sh echo "The TERM variable is" echo $TERM echo "The current PATH setting is" echo $PATH $ 

The result after execution is:

 $  ./script-03  The TERM variable is ansi The current PATH setting is /baan/bse/bin:/usr/bin:/usr/ccs/bin:/usr/contrib/bin:/opt/ nettladm/bin:/opt/fc/bin:/opt/fcms/bin:/opt/upgrade/bin:/ opt/pd/bin:/usr/contrib/bin/X11:/usr/bin/X11:/opt/hparray/ bin:/opt/perf/bin:/opt/ignite/bin:/usr/sbin:/sbin:. $ 

We can pass data to a program by using environment variables. As you already know, we can change the value of an environment variable in a program, but that change gets lost as soon as the program terminates. In script-04 , we get an environment variable COLOR having value red and then change it to green . After execution of the program, when we check the value of the COLOR variable, it is still red . Let us first see the contents of script-04 and then execute it.

 $  cat script-04  #!/usr/bin/sh echo "The current COLOR variable is" echo $COLOR COLOR=green echo "The new COLOR variable is" echo $COLOR $ 

Before executing this program, you need to set and export the COLOR variable. You can verify the exported value of the variable by using the echo command. After you execute the program, it changes the value and prints the new value green . When the program finishes, you use the echo command again to check the variable value, and you find out that it is the same as it was before executing the program.

 $  COLOR=red  $  export COLOR  $  echo $COLOR  red $  ./script-04  The current COLOR variable is red The new COLOR variable is green $ $  echo $COLOR  red $ 

   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net