CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Programming] Shell Programming Series(III)


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author System On 2007-04-29 10:01:47




View and vote on the article here: Shell Programming Series(III)


Shell Programming Series(III)

Category
Programming
Summary
In algebra, you learned that you can use letters to stand for unknown quantities in algebraic equations, and that these letters were called variables. In programming, the concept is pretty much the same, although variables can hold strings as well as numb
Body
There are two types of variables you will deal with in shell programming: shell variables and environment variables. The primary difference is that environment variables will be available to other scripts or programs that you call from inside your shell program and shell variables will be available only to the script itself.


Variables in shell programming are loosely typed; they do not have to be declared or typeset before they can be used. All variables in shell programming are stored as strings.





Variable Assignment





In its most basic form, a variable can be set in the following manner:





yourvar=5





The value 5 is now stored in the variable yourvar. To access the information stored in a variable, proceed the variable name with a $. For example echo ${yourvar} will print 5 to STDOUT, which as you recall, is normally the screen.


Variable assignments are one place where whitespace does matter. The statement "yourvar=5" will assign 5 to the variable "yourvar". The statement "yourvar =5" will produce an error because the shell will try to interpret "yourvar" as a command name to execute rather than as a variable assignment.


The curly braces are optional, but can help to improve code readability since they make variable names easier to spot when quickly scanning code. It's up to you whether you want to use the braces or not.


The value of a variable can also be assigned to another variable. For example:





test=$yourvar





This line assigns whatever is in "yourvar" to the variable "test". If "test" already contains something, whatever is there will be overwritten by the new assignment


In addition, it is also possible to assign the output of a command to variable or read input from STDIN (which is normally the keyboard) and store it in a variable. We will see how to do this later in series.


Creating an environment variable is pretty much same as creating a shell variable. The only difference is that it must be exported. This is done with the export statement. For example:





VARIABLE="5"


export VARIABLE






will create a shell variable named "VARIABLE" and then export it so that it is an environment variable that will be available to other shells started from within this shell.





Variable Names





Variable names are case-sensitive and can contain letters, numbers, and underscores. The variable name cannot begin with a number, though, and it is best to avoid beginning variable names with an underscore. Use descriptive names to make program code easier to read. For example, it is much easier to guess what a variable named "avg_mark" probably would contain than to guess what a variable named "abc1_F" might contain. By convention, local variables use lowercase letters, and environment variables use uppercase letters. However, some people prefer to use a mixture of lowercase and uppercase letters in local variables to make them stand out from other shell commands, which are almost always in all lowercase.




This article was imported from zZine. (original author: ismail)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.01675 seconds


About Us | Privacy Policy | Mission Statement | Help