69
Review Chapters 5 thru 8

Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

Embed Size (px)

Citation preview

Page 1: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

Review

Chapters 5 thru 8

Page 2: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

2

Two Groups of Commands

Select commandsManipulate and Format commands

Page 3: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

3

Using the Select CommandsSelect commands:

grep, diff, uniq, comm, wc

Using Pipes – The pipe operator (|) redirects the output of one command to the input of another command An example would be to redirect the

output of the ls command to the less command

The pipe operator can connect several commands on the same command line

Page 4: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

4

Using Pipes

Using pipe operators and connecting commands is useful when viewing directory information

ls /etc | sort –r | less

Page 5: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

5

Using the grep Command

Used to search for a specific pattern in a file, such as a word or phrase

grep’s options and wildcard support allow for powerful search operations

You can increase grep’s usefulness by combining with other commands, such as head or tail

Page 6: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

6

Using the grep Command

grep can take input from other commands and also be directed to provide input for other commands

grep IBM /etc/termcap | head

Page 7: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

7

Using the uniq Command

Removes duplicate lines from a file

It compares only consecutive lines, therefore uniq requires sorted input

Uniq has an option that allows you to generate output that contains a copy of each line that has a duplicate

Page 8: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

8

Using the comm Command

Used to identify duplicate lines in sorted files

Unlike uniq, it does not remove duplicates, and it works with two files rather than one

It compares lines common to file1 and file2, and produces three column output Column one contains lines found only in file1 Column two contains lines found only in file2 Column three contains lines found in both files

Page 9: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

9

Using the diff Command

Attempts to determine the minimal changes needed to convert file1 to file2

The output displays the line(s) that differ

The associated codes in the output indicate that in order for the files to match, specific lines must be added or deleted

Page 10: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

10

Using the wc Command

Used to count the number of lines, words, and bytes or characters in text files

You may specify all three options in one issuance of the command

If you don’t specify any options, you see counts of lines, words, and characters (in that order)

Page 11: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

11

Using the wc Command

The options for the wc command:

–l for lines

–w for words

–c for characters

wc –l /etc/passwd

Page 12: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

12

Using the Manipulate and Format Commands

These commands are: sed, tr, pr

Used to edit and transform the appearance of data before it is displayed or printed

Page 13: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

13

Translating CharactersUsing the tr command

tr copies data from the standard input to the standard output, substituting or deleting characters specified by options and patterns

The patterns are strings and the strings are sets of characters

A popular use of tr is converting lowercase characters to uppercase

Page 14: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

14

Using the pr Command toFormat Your Output

pr prints specified files on the standard output in paginated formBy default, pr formats the specified files into single-column pages of 66 linesEach page has a five-line header, its latest modification date, current page, and five-line trailer consisting of blank lines

Page 15: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

15

Running a Shell Script

You can run a shell script in virtually any shell that you have on your system

The Bash shell accepts more variations in command structures that other shells

Run the script by typing sh followed by the name of the script, or make the script executable and type ./ prior to the script name

Page 16: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

16

Using UNIX/Linux Shell Scripts

After creating shell script, the OS is instructed that the file is an executable shell script via the chmod commandScript files can be run in several ways: Set the path variable and type the

script name at the command prompt Type ./filename if script is in current

directory Type the script name preceded by the

full path

Page 17: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

17

Using Comments

Comments are important!Provide useful documentation to both the programmer and to others who need to understand or debug the codeTo use, start comment line with a #

Page 18: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

18

Variables

Variables are symbolic names that represent values stored in memoryThree types of variables: Configuration variables store information

about the setup of the OS Environment variables hold information

about your login session Shell variables are created at the command

prompt or in shell scripts and are used to temporarily store information

Page 19: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

19

Variables

Use the printenv variable to see a list of environment variables.

You can also use env

Look at the man pages. What is different between the two commands?

Page 20: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

20

Environment and Configuration Variables

Page 21: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

21

Environment and Configuration Variables

Page 22: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

22

Environment and Configuration Variables

Page 23: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

23

Environment and Configuration Variables

Page 24: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

24

Shell Variables

Shell Variables are variables that you can define and manipulate for use with program commands in a shellObserve basic guidelines for handling and naming shell variables I recommend that you use all UPPERCASE

characters when naming your variables

Page 25: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

25

Shell Variables

Variables are handled differently depending on the syntaxType: echo $USERNAME echo “$USERNAME” echo ’$USERNAME’ echo `$USERNAME`

Page 26: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

26

Shell Operators

Bash shell operators are in four groups: Defining operators Evaluating operators Arithmetic operators Redirection operators

Page 27: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

27

Defining Operators

Used to assign a value to a variableMost common is = (equal sign)Use quotation marks with stringsBackquote says execute the command inside the backquotes and store the result in the variable

Page 28: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

28

Evaluating Operators

Used for determining the contents of a variableecho $variablename will show the value of variablenameDouble quotes can be used, but not single quotes

Page 29: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

29

Arithmetic Operators

Page 30: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

30

Arithmetic Operators (continued)

Regular mathematical precedence rules apply to arithmetic operatorsTo store arithmetic values in a variable, use let statement let x=6+4*2 echo $x

Page 31: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

31

Redirection Operators

The > redirection operator overwrites an existing file-o noclobber option of set command will prevent overwriting

Page 32: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

32

Exporting Shell Variables to the Environment

Shell scripts cannot automatically access variables created and assigned On the command line By other scripts

Make variables global in your environment by using the export command

Page 33: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

33

Modifying the PATH Variable

PATH variable controls where your shell will look for shell scriptsYou can add directories to your PATH Special directories for scripts Your current working directory

Page 34: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

34

Shell Logic Structures

Four basic logic structures needed for program development are: Sequential logic Decision logic Looping logic Case logic

Page 35: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

35

Sequential Logic

Commands are executed in the order in which they appear in the script or programThe only break in this sequence comes when a branch instruction changes the flow of execution by redirecting to another location in the script or programUsed for simple, straightforward command sequences

Page 36: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

36

Decision Logic

Enables your script or program to execute a statement or series of statements only if a certain condition existsIn many cases, the condition depends upon the result of a command or on a comparisonThe if statement is the primary decision-making control structure in this type of logic

Page 37: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

37

Looping Logic

A control structure repeats until some condition exists or some action occursTwo common looping mechanisms: for loops cycle through a range of

values until the last in a set of values is reached

The while loop cycles as long as a particular condition exists

Page 38: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

38

Program control structures can be entered from the command lineWildcard characters can be used in loopsThe while loop is set up to test repeatedly for a matching conditionThe while loop is used when code must be repeatedly executed an undetermined number of times

Looping Logic (continued)

Page 39: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

39

Case Logic

The case logic structure simplifies the selection from a list of choicesIt allows the script to perform one of many actions, depending on the value of a variableTwo semicolons (;;) terminate the actions taken after the case matches what is being tested

Page 40: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

40

Using Shell Scripting to Create a Menu

Often useful to create a menu that branches to specific shell scriptsThe tput command is useful when creating menus Can initialize the terminal display to place

text and prompts in specific locations and respond to the user

Page 41: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

41

Debugging a Shell Script

A shell script will not execute if there is an error in one or more commandsRunning a shell script using sh enables quick debugging of problems sh -v option displays lines of code in the

script as they are read by the interpreter sh -x option displays the command and

its arguments line by line as they are run

Page 42: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

42

Customizing YourPersonal Environment

When programming and shell scripting, customizing your environment by modifying the initial settings in the login scripts provides many benefitsLogin scripts run just after logging inSetting up personal bin directories and modify editor defaults are common customizations

Page 43: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

43

Customizing Your Personal Environment

An alias is a name that represents another command The .bashrc file in your home directory is used to establish customizations that take effect at each loginThe .bashrc script is executed each time a shell is generated, such as when shell scripts are run

Page 44: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

44

The trap Command

The trap command causes a shell program to automatically remove temporary files created when shell scripts runProgrammers often set up a subdirectory to store temporary files, and when a script file exits, trap removes the filesHaving files removed from a temporary directory like this is considered “good housekeeping”

Page 45: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

45

Putting It All Together in an Application

Applications require you to: Assign and manage variables Use shell operators Employ shell logic structures Use additional wildcard characters Use tput for managing screen

initialization Use trap to clean up temporary files

Will use these skills to build a shell script application in Hands-on Project

Page 46: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

46

Understanding UNIX/Linux Utilities

UNIX/Linux utilities let you Create and manage files Run programs Produce reports Monitor and maintain the system Recover from a range of errors

New utilities are continually being added in order to make UNIX/Linux run more efficiently

Page 47: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

47

Understanding UNIX/Linux Utilities

Classified into eight major areas: File processing System status Networking Communications Security Programming Source code management Miscellaneous

Page 48: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

48

File Processing Utilities

cat Display files fmt Formats text

cp Copy files grep Matches patterns in files

cpio Copy/back files fgrep Matches patterns in files

cut Selects char/fields gzip Zip/compress files

dd Convert/copy a file/image

gunzip

Unzip/uncompress files

dump Backs up files head Displays 1st part of files

file Displays file type ispell Spell Checks

find Finds files less Displays files

Page 49: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

49

File Processing Utilities

ln Creates symbolic links pwd Displays current directory

lpr Sends file to printer rm Deletes files/directories

ls Lists files/directories rmdir Removes/directories

man Displays man pages sorts Sorts files/input

mkdir Makes directories tail Displays end of files

mkfs Builds filesystems tar Copies and archives files

mount

Mounts filesystems touch Changes files dates

mv Renames/moves files/dir’s

whereis Locates info on files

Page 50: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

50

Classifying UNIX/Linux Utilities

Page 51: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

51

Classifying UNIX/Linux Utilities

Page 52: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

52

Classifying UNIX/Linux Utilities

Page 53: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

53

Classifying UNIX/Linux Utilities

Page 54: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

54

Classifying UNIX/Linux Utilities

Page 55: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

55

Classifying UNIX/Linux Utilities

Page 56: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

56

Using the dd Command

Allows you to copy a file and change the format of the destination fileHas a rich set of options to handle copies when other methods are inappropriateAn advantage to using the dd command over cp is that all users, not just the administrator, can copy files to and from the floppy drive

Page 57: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

57

Checking Hard Disk Usage

To maintain adequate hard disk free space, use these strategies: Be vigilant against running dangerously

low on free space by using the df command

Watch for conspicuous consumption using the du command

Follow a routine schedule for “garbage” collection and removal by using the find and rm commands

Page 58: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

58

Removing Garbage Files

Garbage files are temporary files that lose their usefulness after several daysTwo examples of garbage files are core files (named core) and a.out filesUse the find command to assist you in locating these files and the rm command to remove them

Page 59: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

59

Using System Status Utilities

System status commands reflect the system’s performanceSystem engineers primarily use the data related to system statusGood to know how to obtain and store relevant information to send to system administrator and tune-up specialists

Page 60: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

60

Using the top Command

One of the most effective utilities for auditing system performance is the top commandThe top command displays a listing of the most CPU-intensive tasks in real timeUpdates every five seconds by default

Page 61: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

61

Using the uptime Command

uptime tells you how long a system has been running since the last time it was booted

Displays current time how long the system has been up number of users on the system the load average for 1, 5, and 15 minutes

Page 62: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

62

Using the free Command

The free utility displays the amount of free and used memory in the system

Page 63: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

63

Managing Processes

A process is identified through a unique number called a process id (pid)Unix/Linux offer utilities to run, monitor, and kill processes using pids

Page 64: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

64

Running Processes in the Background

Can run a process in the background while working with another program in the foregroundTo run a program in the background, append the & character to end of the startup command, e.g., top&Another method is the bg and fg commands

Page 65: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

65

Monitoring Processes

The ps command with the –A or -a option shows a list of all system processes currently running

ps -ax

Page 66: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

66

Killing Processes

Administrator with root privileges can kill any user’s processesUser can kill owned processesUse kill command with the pid of the processUse kill –9 to stop a process that doesn’t respond to an initial kill command

Page 67: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

67

Checking the Spellingof a Document

ispell scans a document, displays errors on the screen and suggests alternative spellings

Page 68: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

68

Comparing Files

Use the cmp utility to compare the contents of two files, and report the first difference between themThe cmp command displays the position and line number of this differenceIf there are no differences, the cmp command displays nothing

Page 69: Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands

69

Formatting Text in UNIX/Linux

Text formatting in UNIX/Linux involves preparing a text file with embedded typesetting commands and then processing the fileUNIX’s nroff and troff commands were the early standard in formatting programsgroff is newer version of both nroff and troff