Chapter 7 Study Guide

From ITCwiki
Jump to navigation Jump to search

Command input and output

Bash Shell Responsible for providing user interface

Interprets commands entered on command line

Can manipulate command input and output

Input and Outputs of commands Represented by labels know as file descriptors. There are 3 file descriptors:

Standard input (stdin) information processed by the command during execution

Standard output (stdout) refers to the normal output of a command

Standard error (stderr) refers to any error messages generated by the command

Both stdout and stderr are displayed on the terminal screen by default

Use the BASH shell to redirect stdout and stderr from the terminal screen to a file on filesystem

Use the > shell metacharacter followed by absolute or relative pathname of the file

ls /etc/hosts /etc/h 1>goodoutput

Use the < to redirect a file to the stdin

cat </etc/issue

Redirection only occurs between a command and a file. However you can send stdout of 1 command to another command as stdin. You can do this by using the pipe shell metacharacter "|" This symbol can be created by pressing the shift key along with the \ key. Common use for piping is to reduce the amount of information displayed.

Shell variables

A BASH shell has several variables in memory at one time. A variable is a reserved portion of memory containing info that might be accessed. Most variables in the shell are referred to as environmental variables because they are typically set by the system and contain info that the system and programs access regularly. Users can also create custom variables. Special variables can also be used for executing commands and creating new files and directories.

Environmental Variables Many are set by default in the BASH shell. To see a list of these variables and their values use the set command. Other variables used to set the user’s work environment:

PS1 – The default shell prompt
HOME – The absolute pathname to the user’s home directory
PWD – The present working directory in the directory tree
PATH – A list of directories to search for executable programs

Home Variable Used by programs that require pathname to the current user’s home directory. If root user logs in, the HOME variable is set to /root. If user logs in, the HOME variable is set to /home/user.


PWD Variable PWD stores the current user’s location in the directory tree. It is affected by the cd command and used by other commands, such as pwd, when the current directory need to be identified.


Path Variable One of the most important variables in the BASH shell. It allows users to execute commands by typing the command name alone. If the executable file is in a directory not listed in the PATH variable (such as the /root directory)the user must specify either the absolute or relative pathname to the executable file.

User Defined Variables To create a variable, specify the name of the variable followed by = and the new contents. Variable identifies can contain alphanumeric characters, the "–" character, or the "_" character. They must not start with a number. They are typically capitalized to follow convention (HOME, PATH, etc.). To create a variable called MYVAR with the contents “This is a sample variable” you would use these command:

MYVAR="This is a sample variable"

Environmental Files To ensure that variables are accssible to a shell at all times, you must place variables in a file that is executed each time a user logs in and starts a BASH shell. These are called environmental files. Some common BASH shell environment files and the order in which they are executed are:

~/.bashrc Typically used to set aliases and variables that must be present in each BASH shell. It is executed immediately after login for all users as well as when a new BASH shell is created after login

/etc/profile Always executed after login for all users on the system and sets most environmental variables




Shell Scripts

A set of commands saved in a text file used to automate common tasks. Any command you can use in the command line can be used in a script. hashpling (#! /bin/bash) specifies the pathname to the shell that interprets the script. “#” by itself is a comment in the script.

Sample Script:

#this is a comment
echo “The date is:”
echo “ “
echo “People logged in:”

'Escape Sequences Used to change output of text in the terminal.

Common ones: \??? -A 3 digit ASCII character (in octal) \\ -Backslash \a –ASCII beep \b –Backspace \c –prevents a new line between commands \n –New Line \t –Horizontal Tab \v –Vertical Tab

Read Command Saves user’s input and places it in a variable used by a script. In the example the script asks for a name, when you type it in it is saved to the NAME variable.

Echo –e “Who are you? \c”
Echo “Hello $NAME”

Who are you? Jason <--User's typed response

Hello Jason

Decision Constructs Not all scripts will run straight through, sometimes you need to see if something installed or the user typed something. Decision constructs allow a script to do different things in different scenarios.

Format of an If construct:

If “something is true”
Do these commands
Do these commands


if [ $NAME = “Jason”]
echo –e “Hello, Jason”
echo –e “Go Away”

If the variable NAME is "Jason" it will say Hello, Jason. If it is anything else it will echo "Go Away"

Test Functions:

-eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ]

-ne is not equal to 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ]

-lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ]

-le is less than or equal to 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]

-gt is greater than 5 > 6 if test 5 -gt 6 if [ 5 -gt 6 ]

-ge is greater than or equal to 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6 ] string1 = string2 string1 is equal to string2

string1 != string2 string1 is NOT equal to string2

string1 string1 is NOT NULL or not defined

-n string1 string1 is NOT NULL and does exist

-z string1 string1 is NULL and does exist-s file Non empty file

-f file Is File exist or normal file and not a directory

-d dir Is Directory exist and not a file

-w file Is writeable file

-r file Is read-only file

-x file Is file is executable


'Case Constructs You can use the case command for multiple outcomes of a variables Start statement with “Case” End a case statement with “Esac” case backwards


echo –e “What is the Capital of France?
Milan (a)
Frankfurt (b)
Paris (c)”
case $ANSWER in

a | A ) echo –e “Incorrect” ,, b | B) echo –e “Incorrect” ,, c | C) echo –e “Correct” ,,


Will ask the question "What is the capital of France?" and list 3 choices. The user then types in their response (a | A means it will accept capitalized response or not). If "a", "A", "b", or "B" are typed it will say "Incorrect". If "c" or "C" are typed it will say "Correct".

&& and || && and || are tests for if a command ran successfully or not

Format: command1 (&& or ||) command2

&& means if command 1 ran then run command2

|| means if command1 did not then run command 2

Loops Sometimes you need to run something multiple times. This is what a loop is for. A loop will keep running a set of commands until it is told to stop.

For Loops will run the set commands for each string listed after the variable.

for NAME in bob sue mary jane frank lisa
mail –s “here is your schedule” <newschedule $NAME
echo “$NAME was emailed successfully”

This script will email bob, sue, mary, jane, frank, and lisa. It will also print when they are emailed successfully.

While Loops will run while something is true and stop once they become false.

while [ $COUNTER –lt 7 ]
echo “All work and no play makes Jack a dull boy” >> /tmp/redrum COUNTER=‘expr $COUNTER + 1’

This script sets a counter to 0 and then adds 1 to the counter each time the commands are run. It will continue running until the counter is equal to 7. So the output would look like:

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

Example Video!