Fall 2010 ~ Eric Meyer

Lesson A
Using the UNIX Utilities


  Understand what UNIX utilities are available and the

classifications of utilities

  Determine hard disk usage and available free space   Display the CPU status and internal memory usage


Understanding UNIX Utilities
  UNIX utilities let you create and manage files, run programs,

produce reports, and generally interact with the system

  They also offer a full range of services that let you monitor and

maintain the system and recover from a wide range of errors

  Utility programs are vital for working through an OS and new

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


Understanding UNIX Utilities
  UNIX utilities are classified into seven major function areas

dictated by user needs   File processing   System status   Networking   Communications   Programming   Source code management   Miscellaneous










Lesson B
Working with the Text-Formatting File Utilities


  Check the spelling of text in a document   Use the cmp command to compare the contents of two files   Format text to create a man page   Use the groff utility to test a man page you have created   Use the man utility to view a man page you have created


Checking the Spelling of a Document
Ispell scans a document, displays errors on the screen and suggests alternative spellings


Comparing Files
  Use the cmp utility to compare the contents of

two files, and report the first difference between them position and line number of this difference displays nothing

  The cmp command displays the character   If there are no differences, the cmp command


Formatting Text in UNIX
  Text formatting in UNIX involves preparing a text file with

embedded typesetting commands and then processing the marked-up text file with a computer program embedded typesetting commands to format the output is included with the regular text of the file

  UNIX’s nroff and troff commands are often used to process the   An embedded code is a special sequence of characters that


Formatting Text in UNIX

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


Formatting Text in UNIX
Groff can be used to produce a man page that contains the standard man page sections


Formatting Text in UNIX
Man pages are made available to others by having a supervisor user copy it to one of the man page directories


Command Line Arguments Cont.
  $# contains the number of command line arguments.   $@ will be replaced by a string containing the command line

arguments   Example script echo.sh #!/bin/sh echo “The” $# “arguments entered:” $@   Usage: echo.sh alpha beta gamma   Output: The 3 arguments entered: alpha beta gamma


Testing Conditions
  There are two ways to test for conditions. The two general

forms are: test <condition> or [ <condition> ]   The latter method is easier to read. Remember to include a space before and after the bracket   A condition can be reversed with a ! before the condition (this is the same as not condition)‫‏‬ [ !<condition> ]   A ‘:’ command in place of condition always returns true


Testing File Attributes
  To test if a file is readable

       

[ -r prog.txt ] [ -r $1.c ] To test if a file is writeable [ -w specialfile.txt ] To test if a file is executable [ -x prog4.sh ] To test if a file exists [ -f temp.text ] Testing for the negation - use ! (eg. not writeable)‫‏‬ [ ! -w nochange.txt ]


Numeric Tests
  The following operators can be used for numeric tests:

{ -eq, -ne, -gt, -ge, -lt, -le }   Examples [ $1 –lt $2 ] [ $1 –gt 0 ] [ $# -eq 2 ] [ $# -lt 3 ]


Simple If Statement
  General Form:

if <condition> then one-or more commands fi
  Example:

if [ -r tmp.text ] then echo “temp.text is a readable file” fi

General If Statement
  General form:

if <condition> then one-or-more-commands elif <condition> then one-or-more-commands … else one-or-more-commands fi

  Note that you can have 0 or more elif statements and that

the else is optional.


Testing Strings
  Performing string comparisons. It is a good idea to put the

shell variable being tested inside double quotes. [ “$1” = “yes” ] [ “$2” != “no” ]

  Note that the following will give a syntax error when $1 is

empty since: [ $1 != “no” ]   becomes [ != “no” ]


Testing with Multiple Conditions
  && is the and operator   || is the or operator   checking for the and of several conditions

[ “$1” = “yes” ] && [ -r $2.txt ] [ “$1” = “no” ] && [ $# -eq 1 ]   checking for the or of several conditions [ “$1” = “no” ] || [ “$2” = “maybe” ]


Quoting Rules
  Using single quotes

‘xyz’ disables all special characters in xyz
  Using double quotes

“xyz” disables all special characters in xyz except $, `, and \.
  using the backslash

\x disables the special meaning of character x


Quoting Examples
var1=“alpha” #set the variable echo $var1 #prints: alpha echo “$var1” #prints: alpha echo ‘$var1’ #prints: $var1 cost=2000 echo ‘cost:$cost’ #prints: cost:$cost echo “cost:$cost” #prints: cost:2000 echo “cost:\$cost” #prints: cost:$cost echo “cost:\$$cost” #prints: cost:$2000


More on String Relational Operators
  The set of string relational operators are: { =, !=, >, >=, <, <= }   The { >, >=, <, <= } operators assume an ASCII

ordering (for example “a” < “c”). These operators are used with the expr command that computes an expression. The backslash has to be used before the operators so that they are not confused with I/O redirection


Using Exit
  The exit command causes the current shell script to

       

terminate. There is an implicit exit at the end of each shell script. The exit command can set the status at the time of exit. If the status is not provided, the script will exit with the status of the last command. General form: exit or exit <status> $? is set to the value of the last executed command Zero normally indicates success. Nonzero values indicate some type of failure. Thus, exit 0 is normally used to indicate that the script terminated without errors.


If Statement Examples
if [ “$1” != “” ] || [ -r $1 ] then echo “the file” $1 “is not readable” fi if [ $var1 –lt $var2 ] then echo $var1 “is less than” $var2 elif [ $var1 –gt $var2 ] then echo $var1 “is greater than” $var2 else echo $var1 “is equal to” $var2 fi

Case Statement
  Compares stringvalue to each of the strings in the patterns.

At a match, it does the corresponding commands. ;; indicates to jump to the statement after the esac (end of case). *) means the default case.   Form: case stringvalue in pattern1) one or more commands;; pattern2) one or more commands;; … *) one or more commands;; esac


Case Statement Example
echo “do you want to remove file $1?” echo “ please enter yes or no” read ans case $ans in “yes”) rm $1 echo “file removed” ;; “no”) echo “file not removed” ;; *) echo “do not understand your request” esac


while and until statements
  while form:

while <condition> do one or more commands done
  until form:

until <condition> do one or more commands done


while and until examples
read cmd while [ $cmd != “quit” ] do … read cmd done read cmd until [ $cmd = “quit” ] do … read cmd done

Good reference on scripting
  http://steve-parker.org/sh/sh.shtml


Chapter Summary
  UNIX utilities are classified into seven major functional areas

dictated by user needs: file processing, networking, communications, system status, programming, source code, management, and miscellaneous tasks because they are add-ons and not a part of the UNIX shells

  Utility programs are distinguished from other OS programs,

  Because utility programs are executed by entering names on

the command line, they are commonly referred to as commands


Chapter Summary
  The df utility checks and reports on free disk space   The du command checks for disk usage   Use the find command to retrieve wasteful files and then use

the rm command to remove them

  The top and free utilities provide detailed views of the

“internals” of the system

  Redirect output of the top and free commands to a disk file

to use as input for a report to the system administrator


Chapter Summary
  Run a program in the background by appending the &

operator to the end of a command line

  The ps command displays all processes currently running   The kill command terminates a specific process   The utility that checks spelling, ispell, scans a document for

typing errors


Chapter Summary
  Text formatting involves embedding typesetting commands

in a file, and then processing the the marked-up file with a program that generates commands for the output device

  The text containing the embedded typesetting commands is

processed by a program like nroff and troff that formats the output

  Linux introduced groff, which implements the features of

both nroff anf troff pages

  Those who have supervisor privileges most often create man


Sign up to vote on this title
UsefulNot useful