Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO UNIX: The Shell Command Interface

Similar presentations


Presentation on theme: "INTRODUCTION TO UNIX: The Shell Command Interface"— Presentation transcript:

1 INTRODUCTION TO UNIX: The Shell Command Interface
© Ronald J. Leach All Rights Reserved

2 © Ronald J. Leach All Rights Reserved
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

3 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

4 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

5 © Ronald J. Leach All Rights Reserved
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

6 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

7 © Ronald J. Leach All Rights Reserved
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

8 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

9 © Ronald J. Leach All Rights Reserved
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

10 © Ronald J. Leach All Rights Reserved
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 frog and the dog went to study the compter at the dog house © Ronald J. Leach All Rights Reserved

11 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

12 © Ronald J. Leach All Rights Reserved
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

13 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

14 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

15 © Ronald J. Leach All Rights Reserved
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

16 © Ronald J. Leach All Rights Reserved
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

17 © Ronald J. Leach All Rights Reserved
find, cont Can be combined with other utilities exec can be used to add executable files with arguments find /dir1 /dir2 -exec grep -l ‘Buy Advanced Topics in UNIX’{} \; © Ronald J. Leach All Rights Reserved

18 © Ronald J. Leach All Rights Reserved
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

19 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

20 Some shell scripts, cont.
for i in *.DIR do cd $i echo "DATA ANALYSIS FOR <=================> $i">>$i.DOC echo "FILES\t\t\tSTART DATE\tFINISHED DATE\t\tCOMMENTS">>$i.DOC echo " ">>$i.DOC © Ronald J. Leach All Rights Reserved

21 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] mkdir $j.DIR © Ronald J. Leach All Rights Reserved

22 Some shell scripts, cont.
echo $j>>$i.DOC floppy -t -cn $j>>$j.txt flow -g $j.floptre>>$j.txt1 rm -f *.floptre *.flopold mv $j* $j.DIR mv flow.ps $j.DIR done cd ../ SCRIPT TO COMPILE THE FORTRAN SOURCE CODE for i in *.f do f77 -col72 $i SCRIPT TO SEPARATE FORTRAN, ASSEMBLY, AND DOCUMENTATION for i in *.DIR cd $i cp *.f REUSE/IMP8.18/FORTRANFILES/DISK cp *.txt2 REUSE/IMP8.18/DOCUMENTATION cp *.a REUSE/IMP8.18/ASSEMBLY © Ronald J. Leach All Rights Reserved

23 Shell scripts to compile FORTRAN source code
for i in *.f do f77 -col72 $i done © Ronald J. Leach All Rights Reserved

24 Shell script to separate FORTRAN, assembly, ...
for i in *.DIR do cd $i cp *.f REUSE/IMP8.18/FORTRANFILES/DISK cp *.txt2 REUSE/IMP8.18/DOCUMENTATION cp *.a REUSE/IMP8.18/ASSEMBLY cd ../ done © Ronald J. Leach All Rights Reserved


Download ppt "INTRODUCTION TO UNIX: The Shell Command Interface"

Similar presentations


Ads by Google