CNT-2311-Chapter 9 Notes

From ITCwiki

Chapter 9

Managing the Shell Environment

Environment Variables

Environment variables provide the means to pass named data (variables) to programs launched from a shell.
Environment variables usually have a preceding ($) when you are using it in a script and on a shell command line.
Shells themselves also rely on environment variables.
Environment variables may be manually assigned a value using the equal sign assignment operator.
The variable becomes available to the programs you launch in your shell when you use the export command.
Example: $
$ export HOSTNAME
Note that the two commands can simplfied into one statement
$ export
A program that needs to know the TCP/IP hostname of the computer can refer to the $HOSTNAME to get this information.
On a bash command line, use env to display all environment variables or you may refer to an single environment variable by using the echo command.

Common Environment Variables

Any system may have several environment variable values set each relating to specific programs.
Using command env alone, will display all environment variables that are currently set, otherwise it is used to run a program with a changed set of environment variables.
Example: $ env | grep HOSTNAME
Use the echo command if you want to know the value of a single variable. If you pass a variable preceded by a ($) the value of the variable is returned.
Example: $ echo $HOSTNAME
The following list has a few Common Environment Variables and their meanings
SHELL: Holds the path to the current command shell
PWD: Present working directory and maintained by the system
HOME: Points to your home directory
HOSTNAME: The current TCP/IP hostname of the computer
PATH: Sets the path for a session and is unusually important. The PATH variable is typically built up in several configuration files, such as /etc/profile and in the .bashrc file in the user’s home directory
MAIL: Holds the location of the user’s mail spool which is usually /var/spool/mail/username.
PS1: This is the bash default prompt. PS1 includes variables of it’s own such as \u (for username) , \h (for hostname) and \W for working directory.
LD_LIBRARY_PATH: Some programs use this to identify the directory where the library files may be located. This is similar to PATH.


Most Linux shells, including bash, support using command aliases. They are new names that you can give to regular commands.
You can define aliases individually at any bash prompt, but typically they are included in your bash startup script.
To implement an alias, use the following syntax: alias alias_name=‘commands’

Shell configuration

These files may be classified in a couple of ways: Global files: These affect all users of a shell or are local files that affect one user User/Login files: These are launched only by a login process or by non-login files that are launched by other processes

Global file types: Login file location - /etc/profile and in /etc/profile.d Non-Login file location - /etc/bashrc or /etc/bash.bashrc

User file types: Login file location - ~/.bash_login, ~/.profile, or ~/.bash_profile Non-Login file locations - ~/.bashrc

Global configuration files affect all users of a system; however, their settings may be overridden by individual users, either in user configuration files or in commands that the user may have entered themselves.

Skeleton files are files copied to the user’s home directory when the account was created. They are a set of files that include local bash startup files and are located in the /etc/skel directory. Changes made to these files only affect new accounts.

Writing Scripts

As you use Linux you will more than likely encounter tasks that involve repetitive entry of commands at a shell prompt. Shell Scripts are simple programs written in a interpreted language that are embedded in the Linux shell which you use to type commands. Most Linux systems use the bash shell by default, so more than likely shell scripts are written in the bash shell scripting language.

Beginning a Shell Script

The first line of the shell script identifies the shell being used to run it. The first two characters are a special code which tell the Linux kernal that this is a script and that the rest of the line is the pathway to the program which will interpret the script. This line is refered to as the shebang, hashbang, hashpling or pound bang.

Example: #! /bin/sh * /bin/sh as a symbolic link to /bin/bash

The hash mark ( # ) is a comment character. The script utility ignores this line, but the Linux kernal does not. After writing the script, you make it executable by using the chmod command.

Example: $ chmod a+x my-script

Here the +x option, adds execute permissions to a script called my-script and the a option supplies those permissions to all users.

Using Commands

Shell scripts can run commands both internally and externally to the script. Using ampersand ( & ) at the end of each line in the script running a program file tells the shell to continue on to the next line without waiting for the previous line to finish.

Normal file manipulation commands are used in scripts.

Examples of commands used are: ls, mv, cp, rm as well as grep, find, cut, sed, echo and mail.

Using Variables

Variables, or they can be called Parameters, may be passed to the script. The syntax used to identify a parameter is by using a leading dollar sign ( $ ) with a 0 - 9 number following. Example: $5

Another type of variable used in scripts receive their value from the output of a command. These variables have a leading dollar sign ( $ ) and are typically followed by a name beginning with a alphabetic character, A - Z. Example: $Addr

When assigning a variable the output of a command, that command needs to be enclosed with the back quote character ( ` ).

Using Conditional Expressions

Conditional expressions are used by the compound command and the test commands. Expressions may be unary or binary. Unary expressions are often used to examine the status of a file. The following list is a small sample of conditional commands and their test condition:

-a file ,True if file exists
-d file ,True if file exists and is a directory
-f file ,True if file exists and is a regular file
-s file ,True if file exists and has a size greater than zero

Conditional expressions use string operators and numeric comparison operators as well.

Example: string1 == string2 The condition is True if the the two strings have equal values

Conditionals may combined together using a logical AND ( && ) or a logical OR ( | | ).

When conditionals are joined with &&, both sides of the expression must be True for the entire expression to the True.

When | | is used, if either side of the expression is True, the entire expression will be True.

Using Loops

These constructs tell a script to perform a certain task repeatedly until a stated condition is met ( or when it is no longer met).

The until command executes consequent-commands as long as test-commands has an exit status which is not zero.

until command syntax is: until test-commands;
do consequent-commands;

The while command executes consequent-commands as long as test-commands has an exit status of zero.

while command syntax is: while test-commands;
do consequent-commands;

Using Functions

These are used to group commands for later execution by referencing the single name for the group.

These are executed just like a "regular" command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed.

Functions syntax: name () { command-list; } -The shell function is name. The body of the function is the command-list in between { and }.

Managing E-mail

E-mail is one of the most important network services. Most Linux distributions have e-mail server software installed and configured with basic settings.

Understanding E-mail

SMTP: Simple Mail Transfer Protocol is a push mail protocol. It is designed to send data so SMTP is used thoughout most of the mail delivery system. The sending machine is the initiator of the mail transfer. SMTP servers are known as mail transfer agents (MTAs). SMTP will enable a message to be relayed through a set of SMTP servers.

POP and IMAP: These are both pull mail protocols. Post Office Protocol (POP) and Internet Message Access Protocol (IMAP) servers transfer the mail message to the receiving computer when a relay request is initiated.

A header is added by each server in the relay chain. This provides documentation to the path which the message has taken. It may be that a large number of servers are involved in the chain, but usually a message passes through 6 or less systems.

In general, a user’s mail server incoming messages are stored in a file in /var/spool/mail/username. This file or directory is known as the user’s mail spool.

E-mail Software

Linux supports several different e-mail servers. Generally, one of the major mail servers will be included in your system. If not, you will have to determine which to use and install it on your machine.

The following are four of the most popular e-mail servers use on Linux.

  • Postfix
  • Sendmail
  • Exim
  • qmail

Managing E-mail

The mailq program is the main tool available for e-mail management. Basically, the command shows the contents of the e-mail queue on all systems. This can be useful to you when the messages in a mail queue need to be cleared out.

Managing Data with SQL

Structured Query Language (SQL) is a language used to retrieve data from a database or to store data on a database.

SQL is a language used to access data on a database. In principle, you may use any SQL package in Linux that meets your needs. Certain products may work best with a specific SQL package or even may require a particular one. You should research each package in detail before deciding on one. Some packages operate on the client-server model. One program (the server) manages the database, while another (the client) supplies users and programs access to the database.

SQL packages

MySQL can be pronounced My-S-Q-L , or commonly as My Sequel, and is a relation database management system. Most major Linux distributions include MySQL in their packages. For a complete installation, you probably will install multiple packages, such as a client, a server and probably development tools. You can learn more at

PostgreSQL is a powerful, open source object-relational database system and runs on all major operating systems. Like MySQL, implementation most likely will have a client and server installed, along with additional support packages. Information about PostgreSQL can be found at

SQLite is not a standalone database. It provides programs with a method to store data using a SQL interface within the program. SQLite is a software library that implements a SQL database engine. This package is based at

SQL basics

Pros and Cons: Pros are that SQL is a flexible language, it is universal and there are relatively few commands to learn. Cons are that you need to have a detailed knowledge of the database and it's structure and that extracted data may provide you with misleading results.

Row (also known as a tuple), represents a single object.

Column (also known as an attribute or field) represents a specific feature.

Commands are terminated by a semicolon ( ; ). It is good practice to use it, but not all commands require it.

Combination of rows and columns is referred to as a table. Databases may contain multiple tables and SQL may also support multiple databases.

Using SQL

Primary Keys have 1 or More Columns Used to Uniquely Identify a record. All Columns Defined as PK’s MUST be populated

Foreign Keys are a Value on a table that references a Primary Key from a different table.

Joining tables are usually based on Primary / Foreign Keys.

SQL Command examples

SELECT: This statement returns a set of records from a table or view. It can be used in combination with JOIN to return a set of records from multiple tables. The set of records returned can be reduced by using the WHERE clause conditions. The order that they are returned can be set by using the ORDER BY clause.

Most common form: SELECT field1, field2, etc FROM tableName
WHERE conditions ORDER BY listOfFields

INSERT: Adds new records (rows) to the table.

Most common form: INSERT INTO tableName(field1,field2)

UPDATE: Is most often used in combination with the WHERE clause to update a record in a table.

Most common form: UPDATE tableName SET field1=value1, field2=value2
WHERE condition

DELETE: When you need to delete a row in your table, or a set of rows, you will use this statement.

Most common form: DELETE FROM tableName WHERE condition

WHERE: This clause specifies what set of records the operation should be performed on.

The most common are: =, <>, <, >, IN, and LIKE operands.