
402a0cac351ff33163f575653fbe193f.ppt
- Количество слайдов: 24
INTRODUCTION TO UNIX: The Shell Command Interface © Ronald J. Leach All Rights Reserved 1
The UNIX shell • The primary UNIX user interface • A command interpreter • A powerful programming language, includes facilities for – execution of processes – combination of processes – control structures and variables © Ronald J. Leach All Rights Reserved 2
Example - connecting processes • Problem - computing the percentage of misspelled words in a document • Most word processors have a spell checkers • These word processors don't usually provide counts of spelling errors • We don’t want to write a program by hand © Ronald J. Leach All Rights Reserved 3
Connecting processes, cont. • A simple UNIX shell program wc -w filename spell filename | wc - w • The first line counts the number of words in "filename” invoking the wc utility (the -w option counts only the number of words) • The second line uses the spell utility and sends the results to the wc utility © Ronald J. Leach All Rights Reserved 4
Pipes in the UNIX shell • The character '|' is called the pipe symbol • Connects two UNIX processes • The connection of two processes by a pipe is automatic (in the UNIX shell) and requires no special programming © Ronald J. Leach All Rights Reserved 5
Modifying shell programs: an example • Count the misspelled words in a file • Ignore any duplications • Hard to modify a program in a standard programming language to do this • In UNIX, we just use some additional building blocks © Ronald J. Leach All Rights Reserved 6
The new shell program cat filename | unique | wc -w spell filename | wc -w • The UNIX utility cat lists all the contents of the file on the terminal screen. • A user-written utility, unique, is to produce a list of the unique occurrences of words in the file © Ronald J. Leach All Rights Reserved 7
The user-written unique utility • Made up of three standard UNIX utilities: tr, sort, and uniq • Process the input file to one word per line • Output only one occurrence of each word • Sort the lines into alphabetical order © Ronald J. Leach All Rights Reserved 8
Unique, cont. • Each step involves an output that is a transformation of its input • The original input is not affected since we use a copy of the file ( produced by cat) • tr translates characters specified in its first argument into characters specified in the second argument © Ronald J. Leach All Rights Reserved 9
Unique, cont. Input: The frog and the dog went to study the compter at the dog house Output (combined five per line, to fit on one slide): The went at frog and the dog to study the compter the dog house © Ronald J. Leach All Rights Reserved 10
Some non-portability issues • In the C shell, the tr utility is used like tr -c 'A-Za-z' '12' • while in the Bourne shell that is most common in System V UNIX, the usage would be tr -c '[A-Z][a-z]' '[12*]’ The 12 is the ASCII new line character © Ronald J. Leach All Rights Reserved 11
Unique, cont. The unique utility is therefore tr -c 'A-Za-z' '12’ | sort |uniq in the C shell and tr -c '[A-Z][a-z]' '[12*]’ | sort |uniq in the Bourne shell Create a file name unique with the appropriate contents © Ronald J. Leach All Rights Reserved 12
How to make unique executable • Change the “mode” to make it executable by the shell chmod +x unique • Each file has 3 sets of permissions: read, write, execute • for each of owner, group, world © Ronald J. Leach All Rights Reserved 13
An important shell command: find • find has many possible variations • Like most shell commands, it has multiple uses • The manual pages are sometimes confusing man find • Can be combined with other utilities © Ronald J. Leach All Rights Reserved 14
find, cont. • To list all files in current directory: find. -name * -print • The. indicates the current directory • The -name option says we search the directory until we match the name, *, which is a wild card • The -print option says print what we find. © Ronald J. Leach All Rights Reserved 15
find, cont • We can compare a file in NEW_DIR to an existing file to see if they are the same find NEW_DIR -name REUSED_FILE -print | diff REUSED_FILE • This technique saved 19% in a reengineering project by determining identical modules in a project with poor editors and naming conventions © Ronald J. Leach All Rights Reserved 16
find, cont • Can be combined with other utilities • exec can be used to add executable files with arguments find /dir 1 /dir 2 -exec grep -l ‘Buy Advanced Topics in UNIX’{} ; © Ronald J. Leach All Rights Reserved 17
Some shell scripts # Code used to analyze FORTRAN files echo "STARTING ANALYSIS “ mkdir $1 cp COPY/* $1 cd $1 # Now the main loop © Ronald J. Leach All Rights Reserved 18
Some shell scripts, cont. for i in [A-Z]* do echo "Creating Directory $i. DIR. . . " mkdir $i. DIR mv $i $i. DIR/$i. SEQ done © Ronald J. Leach All Rights Reserved 19
Some shell scripts, cont. for i in *. DIR do cd $i echo "DATA ANALYSIS FOR <=========> $i">>$i. DOC echo "FILESttt. START DATEt. FINISHED DATEtt. COMMENTS">>$i. DOC echo "------------------">>$i. DOC © Ronald J. Leach All Rights Reserved 20
Some shell scripts, cont. for j in *. SEQ do csplit -s -k -f $j $j '/ADD NAME/+1' '{99}' rm -f $j done for j in *[0 -9] do mkdir $j. DIR © Ronald J. Leach All Rights Reserved 21
Some shell scripts, cont. echo $j>>$i. DOC floppy -t -cn $j>>$j. txt flow -g $j. floptre>>$j. txt 1 rm -f *. floptre *. flopold mv $j* $j. DIR mv flow. ps $j. DIR done cd. . / done © Ronald J. Leach All Rights Reserved 22
Shell scripts to compile FORTRAN source code for i in *. f do f 77 -col 72 $i done © Ronald J. Leach All Rights Reserved 23
Shell script to separate FORTRAN, assembly, . . . for i in *. DIR do cd $i cp *. f REUSE/IMP 8. 18/FORTRANFILES/DISK cp *. txt 2 REUSE/IMP 8. 18/DOCUMENTATION cp *. a REUSE/IMP 8. 18/ASSEMBLY cd. . / done © Ronald J. Leach All Rights Reserved 24