chapter 7: advanced shell programming

51
Chapter 7: Advanced Shell Programming

Upload: melosa

Post on 15-Jan-2016

192 views

Category:

Documents


12 download

DESCRIPTION

Chapter 7: Advanced Shell Programming. Ensuring the Correct Shell Runs the Script. Each UNIX/Linux user can choose which shell they use by default Ensure the correct shell is used to run a script Not all shells support the same commands and programming statements - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 7: Advanced Shell Programming

Chapter 7:Advanced Shell Programming

Page 2: Chapter 7: Advanced Shell Programming

2

Ensuring the Correct Shell Runs the Script

Each UNIX/Linux user can choose which shell they use by default Ensure the correct shell is used to run a

script Not all shells support the same

commands and programming statements

The first line of a script should specify which shell to use

Page 3: Chapter 7: Advanced Shell Programming

3

Setting the Default Shell

System administrator establishes the default shell for a user account User shell set in /etc/passwd file

File can only be edited (carefully!) by system administratorSome systems provide management software to assist in setting default shells

Page 4: Chapter 7: Advanced Shell Programming

4

Using Bash Login and Logout Scripts

When Bash is your default shell, scripts run automatically upon login or re-entry .bash_profile .bashrc (also runs in a subshell)

Administrator controls /etc/bashrc and /etc/profile.bash_logout runs when user logs out Often used to clear the screen

Page 5: Chapter 7: Advanced Shell Programming

5

Defining the Shell (Sha-Bang)

A shell script is little more than a list of commands that are run in sequence. Conventionally, a shellscript should start with a line such as the following: #!/bin/bash

This indicates that the script should be run in the bash shell regardless of which interactive shell the user has chosen. This is important, since the syntax of different shells can vary greatly.

Page 6: Chapter 7: Advanced Shell Programming

6

A Simple Example

Here's a very simple example of a shell script with comments. It just runs a few simple commands

#!/bin/bash # Tell the user what is happeningecho "hello, $USER. Here is a directory listing” # list files echo "listing files in the current directory, $PWD" ls

Page 7: Chapter 7: Advanced Shell Programming

7

More on Shell Variables#!/bin/bash X=ABC

echo "$Xabc" This gives no output. What went wrong ? The answer is that the shell thought that we were asking for the variable Xabc, which is un-initialized. The way to deal with this is to put braces around X to separate it from the other characters.

The following gives the desired result: #!/bin/bash X=ABC

echo "${X}abc"

Page 8: Chapter 7: Advanced Shell Programming

8

Setting Defaults for Using the vi Editor

To use the vi editor for code development, configure .exrc in your home directoryAutomatically sets up the vi environment Set the number of tab spaces to use

when nesting lines of code Display line numbers

Page 9: Chapter 7: Advanced Shell Programming

9

Some vi tips for .exrc

To view the configurations of the parameters in your current vi session, go into the command mode and issue the command set all .:set all or:setall

Page 10: Chapter 7: Advanced Shell Programming

10

Some vi tips for .exrc

Setting syntax on/off syntax off

Precede each line with a number set number

Set word wrap 3 characters from edge of screen set wm=3

Create an abbreviation/shortcut ab wwr William W. Richards

Page 11: Chapter 7: Advanced Shell Programming

11

Spell Checking with vi

:map V :w^M:!ispell % ^M:e!^M^M

Page 12: Chapter 7: Advanced Shell Programming

12

Reading in the Date

:ab DATE ^M^[:.!date '+\%a \%d \%h \%Y'^MkJA

Page 13: Chapter 7: Advanced Shell Programming

13

Clearing the Screen

The clear command is useful for clearing the screen, but there is a faster way Store the output of the clear command

in a variable and then echo the contents of the variable on the screen

About ten times faster than the actual command since the system does not have to locate and execute the clear command

Page 14: Chapter 7: Advanced Shell Programming

14

Clearing the Screen

How do you do this?Simple…

CLEAR=`/usr/bin/clear`export CLEARecho $CLEAR

Page 15: Chapter 7: Advanced Shell Programming

Chapter 8:Exploring UNIX/Linux Utilities

Page 16: Chapter 7: Advanced Shell Programming

16

Objectives

Understand many of the UNIX/Linux utilities that are available and how they are classifiedUse the dd utility to copy and convert filesMake a bootable removable diskMonitor hard disk usage

Page 17: Chapter 7: Advanced Shell Programming

17

Objectives (continued)

Use system status utilitiesMonitor and manage processesCheck the spelling of text in a documentUse the cmp command to compare the contents of two filesFormat text to create and use a man page

Page 18: Chapter 7: Advanced Shell Programming

18

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 19: Chapter 7: Advanced Shell Programming

19

Understanding UNIX/Linux Utilities (continued)

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

Page 20: Chapter 7: Advanced Shell Programming

20

Classifying UNIX/Linux 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 21: Chapter 7: Advanced Shell Programming

21

Classifying UNIX/Linux Utilities (continued)

Page 22: Chapter 7: Advanced Shell Programming

22

Classifying UNIX/Linux Utilities (continued)

Page 23: Chapter 7: Advanced Shell Programming

23

Classifying UNIX/Linux Utilities (continued)

Page 24: Chapter 7: Advanced Shell Programming

24

Classifying UNIX/Linux Utilities (continued)

Page 25: Chapter 7: Advanced Shell Programming

25

Classifying UNIX/Linux Utilities (continued)

Page 26: Chapter 7: Advanced Shell Programming

26

Classifying UNIX/Linux Utilities (continued)

Page 27: Chapter 7: Advanced Shell Programming

27

Classifying UNIX/Linux Utilities (continued)

Page 28: Chapter 7: Advanced Shell Programming

28

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 29: Chapter 7: Advanced Shell Programming

29

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 30: Chapter 7: Advanced Shell Programming

30

Using the df Utility

The df utility reports on the status of1024-byte blocks that are allocated, used, and available

Page 31: Chapter 7: Advanced Shell Programming

31

Using the du Utility

The du utility summarizes disk usage, expressed in 512-byte blocks (default) or by the number of bytes(-b option)

Page 32: Chapter 7: Advanced Shell Programming

32

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 33: Chapter 7: Advanced Shell Programming

33

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 34: Chapter 7: Advanced Shell Programming

34

Using the top Command

One of the most effective utilities for auditing system performance is the top command

The top command displays a listing of the most CPU-intensive tasks in real time

Updates every five seconds by default

Page 35: Chapter 7: Advanced Shell Programming

35

Using the top Command (continued)

The top utility run without any options specified

Page 36: Chapter 7: Advanced Shell Programming

36

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 Load average for 1, 5, and 15 minutes

Page 37: Chapter 7: Advanced Shell Programming

37

Using the free Command

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

Page 38: Chapter 7: Advanced Shell Programming

38

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 39: Chapter 7: Advanced Shell Programming

39

Monitoring Processes

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

Page 40: Chapter 7: Advanced Shell Programming

40

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

See man page for descriptions of the signals (remember the man trick)

Page 41: Chapter 7: Advanced Shell Programming

41

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&

Page 42: Chapter 7: Advanced Shell Programming

42

Checking the Spellingof a Document

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

Page 43: Chapter 7: Advanced Shell Programming

43

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 44: Chapter 7: Advanced Shell Programming

44

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 programsAn embedded code is a special sequence of characters that is included with the regular text of the file

Page 45: Chapter 7: Advanced Shell Programming

45

Formatting Text in UNIX/Linux (continued)

Linux introduced groff, which implements the features of both nroff and troff

Page 46: Chapter 7: Advanced Shell Programming

46

Formatting Text in UNIX/Linux (continued)

Groff can be used to produce a man page that contains the standard man page sections

Page 47: Chapter 7: Advanced Shell Programming

47

Formatting Text in UNIX/Linux (continued)

Man pages are made available to others by having a privileged user copy it to one of the man page directories

Page 48: Chapter 7: Advanced Shell Programming

48

Chapter Summary

UNIX/Linux utilities are classified into eight major functional areasUtility programs are called commands: executed by entering names on the command linedd command options allow it to handle copies when other copying methods failTo make a bootable removable disk, use provided utilities such as mkbootdisk

Page 49: Chapter 7: Advanced Shell Programming

49

Chapter Summary (continued)df checks and reports on free disk spacedu checks for disk usageUse find to retrieve temporary files and use rm to remove themtop and free provide detailed views of the “internals” of the system that can be redirected to a file for system tune-up

Page 50: Chapter 7: Advanced Shell Programming

50

Chapter Summary (continued)Run a program in the background by appending & to the end of a commandps displays all running processeskill terminates a specific processispell scans for spelling errorsText formatting involves Embedding typesetting commands in a

file Processing the file with a program that

generates commands for the output device

Page 51: Chapter 7: Advanced Shell Programming

51

Chapter Summary (continued)

Linux introduced groff, which implements the features of both nroff and troffText formatted with groff can be used to create new man pages