Shell Variables   «Prev 

Using Environment Variables to create Shell Scripts

Environment variables
Line by line execution of the script above is described below.

line 1 Describe the purpose of the script. The comments should include information about how to use the script, who wrote it, etc.
line 2 Execute the pwd command and store the string that is returned in the user-defined variable named ARCHIVE_DIR.
line 3 Change to the directory defined by the DB_DIR variable, which the user must have defined before running this script. You could also test that this variable exists before using this command.
line 4 Copy the new file from the directory in which the script was launched to the DB_DIR directory, represented by a period (the current directory after the cd command line on 3).
line 5 Unpack the new archive file using the UNIX tar command.
line 6 Change back to the directory in which the script was originally launched by the user.

Linux Shell Scripting

Shell Script Variable

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. We are going to use variables so much in our scripts that it will be unusual for us not to use them. In this module we are always going to specify a variable in uppercase, for example, UPPERCASE. Using uppercase variable names is not recommended in the real world of shell programming, though, because these uppercase variables may step on system environment variables, which are also in uppercase. Uppercase variables are used in this book to emphasize the variables and to make them stand out in the code. When you write your own shell scripts or modify the scripts in this book, make the variables lowercase text.
To assign a variable to point to data, we use
UPPERCASE="value_to_assign"
as the assignment syntax. To access the data that the variable, UPPERCASE, is pointing to, we must add a dollar sign, $, as a prefix. for example, $UPPERCASE. To view the data assigned to the variable, we use echo $UPPERCASE, print $UPPERCASE for variables, or cat $UPPERCASE, if the variable is pointing to a file, as a command structure.