TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

32
TUTORIAL OF UNIX COMMAND & SHELL SCRIPTS UNIX COMMAND & SHELL SCRIPTS 5027 Professor: Dr Shu-Ching Chen Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha

Transcript of TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

Page 1: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

TUTORIAL OFUNIX COMMAND & SHELL SCRIPTSUNIX COMMAND & SHELL SCRIPTS5027

Professor: Dr Shu-Ching ChenProfessor: Dr. Shu-Ching ChenTA: Hsin-Yu Ha

Page 2: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

OUTLINE

Log ing Unix Basic Commands Shell script

Page 3: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

OUTLINE

Log ing Unix Basic Commands Shell script

Page 4: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

HOW TO LOG IN

Download putty from p yhttp://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Page 5: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

CONNECT TO THE SERVER

The host is margay.cs.fiu.edug y

1

2

3

4

Page 6: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

LOG IN / ACCOUNT INFORMATION

Account information Login as : Your FIU username Password : Your first initial + PantherID + Your last initial

For exmaple, Steven Jackson with PID 1234567should have password s1234567j

Page 7: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

OUTLINE

Log ing Unix Basic Commands Shell script

Page 8: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILES (1) ls : list the contents of your current directory.y y

Format Options

li t ll th fil i l d th hidd

ls [option] [directory][file]

-a : list all the files include the hidden ones -l : list not only the files name but also the related info -t : list the contents by modified date

Page 9: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILES (2) File permissionp

Field1 : A set of ten permission flags Field2 : link count Field2 : link count Field3 : owner of the file Field4 : associated group of the file Field5 : size Field 6-8 : Last modification date Field9 : file name Field9 : file name

Page 10: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILES (3) chmod : change the permission flags of the filesg p g

Format

E l

chmod [option] [types][+/-][types of permission] filename

Example chmod g+w myfile chmod g-rw myfile chmod u=rw go= myfile chmod –R g+rw myfile

Page 11: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILES (4) pwd : print out the current working directoryp p g y cd : change directory

cd . (.) means the current directorycd . C (..) means the parent of current directory Cd cd with no argument will return you to your

home directory

cd ..

cd y

Page 12: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILES (5) cp : copy filesp py

Format Options

i It b d t id iti th i i l fil

cp [option] File1 File2

-i : It can be used to avoid overwriting the original file -r : Copy the folder and all the files and subfolders under it.

mv : move a file from one place to another or rename a file.

Format mv File1 File2

Page 13: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILES (6) mkdir : making directoryg y

Format rm : remove files or directories

mkdir Directory_name

Format Option

-i : ask before actually delete

rm [option] file1 file2 file3…

y -r : delete the folders and all the files and subfolders under it

Page 14: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILES (7) test : A command in Unix that evaluates conditional expressions.p

Format or The functions will return true if the object exist or the

condition specified is true

test expression [ expression ]

condition specified is true. File functions

-d Filename : Filename is a directory -s Filename : Filename has a size greater than 0 -f Filename : Filename is a regular file

Page 15: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILE’S CONTENTS (1) cat : display the contents of a file on the screenp y

Format head : display the first ten lines of a file to the screen

cat file1

Format tail : display the last ten lines of a file to the screen

Format

head –n file1

tail –n file1 Format tail –n file1

Page 16: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027
Page 17: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILE’S CONTENTS (2) wc : word count

Format Options

fi d t h d th fil h

wc [options] file

-w : find out how many words the file has -l : find out how many lines the file has

Page 18: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILE’S CONTENTS (3) grep : It searches files for the specified words or g p p

patterns. Format:

O tigrep [options] [pattern] file

Options: -c : Display the number of columns which satisfied the pattern. -i : Ignore case distinctions in both the PATTERN

d h i filand the input files. -v : Invert the sense of matching, to select non-matching lines.

Page 19: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

Pipe : It cause the execution of multiple processes from one single line

A | B | C

Page 20: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT FILE’S CONTENTS (4) Redirection

> and >> can be used on the output of most commands to direct their output to a file.

Examples Examples

Page 21: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT TEXT PROCESSING (1) sort : sort lines of a text file or files

Default : sort without any option will sort the file alphabetically

Format t [ ti ] fil Format uniq : remove duplicate adjacent lines from

sorted file.

sort [option] file

Page 22: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027
Page 23: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT TEXT PROCESSING (2) sed: One ultimate stream editor

Detail Tutorial : http://www.grymoire.com/Unix/Sed.html

Important function > Substitution Important function -> Substitution Format : sed 's/term1/term2/g‘ filename

Page 24: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

COMMANDS ABOUT TEXT PROCESSING (3) cut : extract sections from each line of a file.

Format Option

h t

cut [options] [range] filename

-c : character -f : field -d “:” : delimiter (default is a tab)

R Range N-M N- -M

Page 25: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027
Page 26: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

OUTLINE

Log ing Unix Basic Commands Shell script

Page 27: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

SHELL SCRIPT INTRO (1) Scripts are collections of commands that are stored p

in a file.

Detail Tutorial http://www.freeos.com/guides/lsst/ Basic Vi commands

vim filename vim filename i : switch to the editing(insert) mode Esc +:q! Leave the vim program without saving the file Esc +:w Save the file Esc +:wq Save the file and leave the vim program

Page 28: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

SHELL SCRIPT INTRO (2) First shell scriptp

Note that to make a file executable, you must set the eXecutable bit, and for a shell script, the Readable bit must also be setReadable bit must also be set.

Page 29: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

SHELL SCRIPT INTRO (3) Variable

Page 30: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

SHELL SCRIPT INTRO (4) IF LOOP

Page 31: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

SHELL SCRIPT INTRO (5) For Loopp

Page 32: TUTORIAL OF UNIX COMMAND SHELL SCRIPTS 5027

SHELL SCRIPT INTRO (6) Useful concept $( commands)p