Embed
Email

Intermediate UNIX OS

Document Sample

Shared by: zhouwenjuan
Categories
Tags
Stats
views:
0
posted:
2/13/2012
language:
pages:
148
UNIX/Linux





CIS140U

Winter 2009







Gary DeRoest



2/13/2012 Gary DeRoest 1

Schedule (general)

Part 1: Ways to automate

alias, script files, join, awk

Part 2: Tools to organize

pipes, redirection, grep, uniq,

comm, sed, pr, cut, sort, tr

Part 3: bash tools

if, then, else, test, case, for,

let, while, tput cup

Part 4: Building an application









2/13/2012 Gary DeRoest 2

Schedule (general)

Part 5: Installation - SLES 9

YAST, YOU, tar, rpm, cpio

Part 6: Boot/Service/User Configuration

setup, chkconfig, service

Part 7: User/Group configuration tools

useradd, usermod, userdel

groupadd, groupmod, groupdel

GUI tools

Part 8: Configuration Documentation







2/13/2012 Gary DeRoest 3

Schedule (general)

Part 9: Process Management

fg, bg, jobs, ps, top

Part 10: Administration Responsibilities

Part 11: Security Concerns









2/13/2012 Gary DeRoest 4

Alias’s



A keyboard shortcut for frequently used or

long commands.



Aliases are processed before internal and

external commands









2/13/2012 Gary DeRoest 5

Alias Examples For Bash Shell



• alias c=‘clear’

• alias ll=‘ls -lA | more’

• alias dir=‘c; ls -l’

• alias cd=‘who -iH’

• cd

• unalias cd

• alias







2/13/2012 Gary DeRoest 6

Viewing All Aliases



• alias



[garyd@bct2 bin]$ alias



alias l.='ls -d .[a-zA-Z]*

alias ll='ls -l --color=tty'

alias ls='ls --color=tty'

alias vi='vim'



[garyd@bct2 bin]$





2/13/2012 Gary DeRoest 7

Remembering Aliases

Aliases can be set automatically each time you

log in



Your home directory contains a hidden file

named .bashrc which is the usual place where

users define aliases



.bashrc is executed each time you log in. Any

alias definitions will then be defined

automatically





2/13/2012 Gary DeRoest 8

~/.bashrc



An unmodified .bashrc file might look like

this

# .bashrc



# User specific aliases and functions



# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi





2/13/2012 Gary DeRoest 9

.bashrc - Adding Your Own Stuff



A modified .bashrc file might look like this

# .bashrc



# User specific aliases and functions

alias c=‘clear’

alias md=‘mkdir’

alias cp=‘cp -i’

# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

2/13/2012 Gary DeRoest 10

/etc/bashrc



• Aliases are generally assigned by individual

users

• Administrators may wish some aliases set

for all users

• Global aliases are set in a file in the /etc

directory

– /etc/bashrc

• /etc/bashrc is owned and modified by root





2/13/2012 Gary DeRoest 11

FAQ



I changed my .bashrc file - so why aren’t my

aliases working?



The .bashrc file is executed when you log

into the server. Making changes to .bashrc

will not be noticed until you exit and log

back in to the server







2/13/2012 Gary DeRoest 12

Creating Files



• cat > filename

– ^d

• > filename

• touch filename

• vi filename

• pico filename

– pico -w filename

• command > filename

• command >> filename

2/13/2012 Gary DeRoest 13

Warm-up Activity









2/13/2012 Gary DeRoest 14

Script file



• A sequence of UNIX commands that you

wish to execute can be saved to a text file

• This text file must be executable

• This text file should be in a directory

named in the search PATH

• The files ~/.bashrc and ~/.bash_profile are

two examples of bash shell scripts (or

programs)





2/13/2012 Gary DeRoest 15

Bash Scripts



vi script1

clear

echo -n Welcome to my world.

date

save the file :x

make the file executable

chmod u+x script1

execute the program

[gd@bct2~]$ script1

or

[gd@bct2~]$ ./script1



2/13/2012 Gary DeRoest 16

Bash Scripts



In addition to executing external

programs like ‘who’, ‘cal’, and

‘sort’, each UNIX or Linux shell has

its own set of internal commands and

functions.



cd, alias, echo, history, set,

fg, test, and pwd are internal

commands to the bash shell and my not

be available in other shell programs.





2/13/2012 Gary DeRoest 17

File Management Commands



mkdir cp paste

rmdir mv cut

rm sort

grep









2/13/2012 Gary DeRoest 18

File Processing Commands

awk

comm

cat

cut









manipulate & transform

chmod

select commands





diff

join

grep

paste

head

pr

tail

sort

uniq

sed

wc

tr

2/13/2012 Gary DeRoest 19

User must

mkdir command have write

permission

for the

 cd parent

directory to

 mkdir programs create sub-

directories



– mkdir /user



– mkdir ~/files



– mkdir /programs/bin



– mkdir -p stuff/cis140u







2/13/2012 Gary DeRoest 20

rmdir command

– cd

Cannot delete

– rmdir programs non-empty

Directories

With rmdir

command





– rmdir /programs/user

– rmdir ../../directory







2/13/2012 Gary DeRoest 21

cp command



 cp fileA fileZ

 cd mystuff/fileB yourstuff/

 cp fileC morestuff

 cp /junk/*.txt yourstuff

 cp -i fileA fileB

 cp -r /home/stuff ~/mystuff



2/13/2012 Gary DeRoest 22

mv command



 mv fileA fileZ

 mv mystuff/fileB yourstuff/

 mv fileC morestuff

 mv /junk/*.txt yourstuff

 mv -i fileA fileB

 mv -r /home/stuff ~/mystuff

If you cannot

delete a file you

2/13/2012 Gary DeRoest cannot move it 23

rm command



 cd



 “file name with spaces”

rm



 rm -i LinuxWebSites





 tree house





 rm -ri house









2/13/2012 Gary DeRoest 24

Combining Files



output

file A cat fileA fileB

cat

cat

dog

dog fileB

crow

crow frog

frog

bird

bird

bear

bear





2/13/2012 Gary DeRoest 25

Combining Files



output



file A cat frog

paste fileA fileB

cat dog bird



dog fileB crow bear



crow frog

paste fileA fileB > fileC

bird

bear paste fileA fileB > “my file”









2/13/2012 Gary DeRoest 26

Combining Files

output

paste –d”:” fileA fileB

file A cat:frog



cat dog:bird

output

fileB crow:bear

dog

cat

frog

crow

bird frog

dog

bear paste –d”\n” fileA fileB

bird

crow

bear

2/13/2012 Gary DeRoest 27

Splitting Files

fileC output

cat:frog:blue cut –f2 –d”:” fileC frog

dog:bird:black bird

crow:bear:white bear



output

cut –f1,3 –d”:” fileC

cat:blue

dog:black

crow:white





2/13/2012 Gary DeRoest 28

Splitting Files

fileC cut –c2,5 fileC output

cat:frog:blue af

dog:bird:black ob

crow:bear:white r:



cut –c2-5,8 fileC output

at:fg

og:bd

row:a





2/13/2012 Gary DeRoest 29

Sorting Files



sort fileC



fileC

cat:frog:blue output

dog:bird:black cat:frog:blue

crow:bear:white crow:bear:white

dog:bird:black









2/13/2012 Gary DeRoest 30

Sorting Files

fileC

sort -t: -k2 fileC

cat:frog:blue

dog:bird:black

sort -t: +1 fileC

crow:bear:white



output

crow:bear:white

dog:bird:black

cat:frog:blue





2/13/2012 Gary DeRoest 31

Sorting Files

fileC

cat:frog:blue sort -t: -k1.3 fileC



dog:bird:black

crow:bear:white

output

dog:bird:black

crow:bear:white

cat:frog:blue







2/13/2012 Gary DeRoest 32

Sorting Files

sort fileD output

100



fileD 20



100 3



3 output

sort -n fileD

20 3

20

100





2/13/2012 Gary DeRoest 33

Search through Files





fileE

John Henry grep “John” fileE



Joe Montana

Henry Ford output

Sam the Eagle John Henry









2/13/2012 Gary DeRoest 34

Search through Files





fileE grep -i “h” fileE



John Henry

Joe Montana output



Henry Ford John Henry



Sam the Eagle Henry Ford

Sam the Eagle







2/13/2012 Gary DeRoest 35

Search through Files

grep -c “n” fileE output

2



fileE grep -n “y” fileE output



John Henry 1 John Henry



Joe Montana 3 Henry Ford

grep “[HS]” fileE output

Henry Ford

Sam the Eagle John Henry

Henry Ford

Sam the Eagle



2/13/2012 Gary DeRoest 36

Search through Files

grep “Jo..” fileE output

John



fileF output



John grep “...” fileE John



Joe Joe



Henry Henry



Sam Sam

grep -x “...” fileE output

Joe



2/13/2012 Gary DeRoest

Sam 37

join command





Used to combine files using key fields.

Relational database tools.

4 Greg

2 McCartney

1 Gary

3 the Eagle

2 Paul

1 DeRoest

3 Sam

4 Brady







2/13/2012 Gary DeRoest 38

join command



Step one: Sort files on their key fields



first

last

1 Gary

1 DeRoest

2 Paul

2 McCartney

3 Sam

3 the Eagle

4 Greg

4 Brady







2/13/2012 Gary DeRoest 39

join command



Step two: use the join command to

link key fields and display desired

output.

join -a1 -1 1 -2 1 -o 1.2 –o 2.2 first last

1 Gary 1 DeRoest

2 Paul 2 McCartney

3 Sam 3 the Eagle

4 Greg 4 Brady

2/13/2012 Gary DeRoest 40

join command



join 1 Gary



–a1 File 1 to be joined with file 2 2 Paul



–1 1 File 1 has key in column 1 3 Sam

4 Greg

–2 1 File 2 has key in column 1

Print out from file 1, field 2

–o 1.2 –o 2.2 Print out from file 2, field 2

first last 1 DeRoest

2 McCartney

3 the Eagle



2/13/2012 Gary DeRoest 4 Brady 41

join command



join 1 Gary



–a1 2 Paul 1 DeRoest

–1 1 3 Sam 2 McCartney

4 Greg

–2 1 3 the Eagle



–o 1.2 –o 2.2 4 Brady



first last

Gary DeRoest

Paul McCartney

Sam the

Greg Brady

2/13/2012 Gary DeRoest 42

join command



join 1 Gary



–a1 2 Paul 1 DeRoest

–1 1 3 Sam 2 McCartney

4 Greg

–2 1 3 the Eagle



–o 1.1 –o 2.2 4 Brady



first last

1 DeRoest

2 McCartney

3 the

4 Brady

2/13/2012 Gary DeRoest 43

join command

join 1:Gary

–a1 2:Paul 1:DeRoest

-t: 3:Sam

–1 1 2:McCartney

4:Greg

–2 1 3:the Eagle

–o 2.2 –o 1.1 4:Brady

first last

DeRoest 1

McCartney 2

the Eagle 3

Brady 4

2/13/2012 Gary DeRoest 44

join command

join 1:Gary

–a1 2:Paul

-t: 3:Sam

1:DeRoest

-e “No Match” 4:Greg

–1 1 2:McCartney

–2 1 4:Brady

–o 1.2 –o 2.2 –o 1.1

first last

Gary DeRoest 1

Paul McCartney 2

Sam No Match 3

Greg Brady 4

2/13/2012 Gary DeRoest 45

join command

join 1:Gary

–a1 2:Paul

-t: 3:Sam

1:DeRoest

-e “No Match” 4:Greg

–1 1 2:McCartney

–2 1 4:Brady

–o 1.2 –o 2.2 –o 2.1

first last

Gary DeRoest 1

Paul McCartney 2

Sam No Match No Match

Greg Brady 4

2/13/2012 Gary DeRoest 46

join command

join 1:Gary

–a2 2:Paul

-t: 3:Sam

1:DeRoest

-e “No Match” 4:Greg

–1 1 2:McCartney

–2 1 4:Brady

–o 1.2 –o 2.2 –o 2.1

first last

Gary DeRoest 1

Paul McCartney 2

Greg Brady 4



2/13/2012 Gary DeRoest 47

join command

join -a2 -t: -e "No Match" -1 3 -2 2 -o 2.3 –o 1.2 file1 file2









file1

1:Apple:Washington file2

2:Pineapple:Hawaii 1:Oregon:Salem

3:Orange:Florida 2:Washington:Olympia

4:Grape:Oregon 3:Florida:Tallahassee

4:California:Sacramento

5:Hawaii:Honolulu







2/13/2012 Gary DeRoest 48

join command

join -a2 -t: -e "No Match" -1 3 -2 2 -o 2.3 –o 1.2 file1 file2









output

Salem:No Match

Olympia:Apple

Tallahassee:No Match

Sacramento:No Match

Honolulu:Pineapple







2/13/2012 Gary DeRoest 49

join command

join -a2 -t: -e "No Match" -1 3 -2 2 -o 2.3 –o 1.2 file3 file4









file3

3:Orange:Florida file4

2:Pineapple:Hawaii 4:California:Sacramento

4:Grape:Oregon 3:Florida:Tallahassee

1:Apple:Washington 5:Hawaii:Honolulu

1:Oregon:Salem

2:Washington:Olympia







2/13/2012 Gary DeRoest 50

join command

join -a2 -t: -e "No Match" -1 3 -2 2 -o 2.3 –o 1.2 file3 file4









output

Sacramento:No Match

Tallahassee:Orange

Honolulu:Pineapple

Salem:Grape

Olympia:Apple







2/13/2012 Gary DeRoest 51

awk command



Programming language used to

produce formatted reports

Pattern scanning tools allow

for a variety of actions based

on patterns of text









2/13/2012 Gary DeRoest 52

awk command



awk instructions can be

executed straight from the

command line or more

complicated tasks will make

use of script files.









2/13/2012 Gary DeRoest 53

awk command line w/ print



1> awk ‘{print $1}’ table

2> awk ‘{print “---” $1}’ table

3> awk ‘{print “---”, $1}’ table

4> awk ‘{print “---”, $1, $3}’ table

5> awk ‘ /o/ {print $1, $3}’ table

6> awk ‘ /[oO]/ {print $1, $3}’ table

7> awk –F. ‘ /o/ {print $1, $3}’ table



2/13/2012 Gary DeRoest 54

awk command



The print action has limited

formatting capabilities.





The printf action allows for

more control over text

placement.







2/13/2012 Gary DeRoest 55

awk command

The printf action is proceeded by

a pattern definition.





awk –F: ‘{printf “pattern”,$1,$3}’ table





Patterns will contain %

placeholders and characteristics,

text characters, and/or special

characters.



2/13/2012 Gary DeRoest 56

printf pattern definitions



printf “%s\t%s\n”, $1 $2

% Placeholder for $1 variable or text

s String

d Decimal (Integer)

f Floating point number

\t Tab character

\n New line character





2/13/2012 Gary DeRoest 57

awk 1

awk -F: ‘{printf “%s\t%s\n”,$1,$3}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama Birmingham

Alaska Anchorage

Arizona Phoenix

Arkansas Little Rock

California Los Angeles

Colorado Denver

Connecticut Bridgeport

Delaware Wilmington

Florida Jacksonville



2/13/2012 Gary DeRoest 58

awk 2

awk -F: ‘{printf “%10s\t%s\n”,$1,$3}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama Birmingham

Alaska Anchorage

Arizona Phoenix

Arkansas Little Rock

California Los Angeles

Colorado Denver

Connecticut Bridgeport

Delaware Wilmington

Florida Jacksonville



2/13/2012 Gary DeRoest 59

awk 3

awk -F: ‘{printf “%10.10s\t%s\n”,$1,$3}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama Birmingham

Alaska Anchorage

Arizona Phoenix

Arkansas Little Rock

California Los Angeles

Colorado Denver

Connecticu Bridgeport

Delaware Wilmington

Florida Jacksonville



2/13/2012 Gary DeRoest 60

awk 4

awk -F: ‘{printf “%-10.10s\t%s\n”,$1,$3}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama Birmingham

Alaska Anchorage

Arizona Phoenix

Arkansas Little Rock

California Los Angeles

Colorado Denver

Connecticu Bridgeport

Delaware Wilmington

Florida Jacksonville



2/13/2012 Gary DeRoest 61

awk 5

awk -F: ‘{printf “%10.10s\t%s\n”,$1,$6}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama 23.10

Alaska 7.22

Arizona 100.3

Arkansas 65.1

California 1644

Colorado 2.33

Connecticu 7.8

Delaware 1287.2

Florida 44.46



2/13/2012 Gary DeRoest 62

awk 6

awk -F: ‘{printf “%10.10s\t%d\n”,$1,$6}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama 23

Alaska 7

Arizona 100

Arkansas 65

California 1644

Colorado 2

Connecticu 7

Delaware 1287

Florida 44



2/13/2012 Gary DeRoest 63

awk 7

awk -F: ‘{printf “%10.10s\t%f\n”,$1,$6}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama 23.100000

Alaska 7.220000

Arizona 100.300000

Arkansas 65.100000

California 1644.000000

Colorado 2.330000

Connecticu 7.800000

Delaware 1287.200000

Florida 44.460000



2/13/2012 Gary DeRoest 64

awk 8

awk -F: ‘{printf “%10.10s\t%15f\n”,$1,$6}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama 23.100000

Alaska 7.220000

Arizona 100.300000

Arkansas 65.100000

California 1644.000000

Colorado 2.330000

Connecticu 7.800000

Delaware 1287.200000

Florida 44.460000



2/13/2012 Gary DeRoest 65

awk 9

awk -F: ‘{printf “%10.10s\t%-15f\n”,$1,$6}’ states_exp





1 2 3 4

12345678901234567890123456789012345678901234

Alabama 23.100000

Alaska 7.220000

Arizona 100.300000

Arkansas 65.100000

California 1644.000000

Colorado 2.330000

Connecticu 7.800000

Delaware 1287.200000

Florida 44.460000



2/13/2012 Gary DeRoest 66

awk 10

awk -F: ‘{printf “%10.10s%15.3f\n”,$1,$6}’ states_exp



1 2 3 4

12345678901234567890123456789012345678901234

Alabama 23.100

Alaska 7.220

Arizona 100.300

Arkansas 65.100

California 1644.000

Colorado 2.330

Connecticu 7.800

Delaware 1287.200

Florida 44.460



2/13/2012 Gary DeRoest 67

Scripting awk 11a



BEGIN {

{ FS = “:” }

{ print “\tUsers and Home Directories\n” }

{ “date” | getline d }

{ printf “ %s\n”,d }

{ print “==============================\n“ }

}



{ printf “%10s%s\n”,$1,$6 }



END {

{ print “==============================\n” }

}

2/13/2012 Gary DeRoest 68

Scripting awk 11b



awk -f awk_script_file filename

Users and Home Directories

Thu Apr 24 22:05:38 PDT 2003

=======================================

nobody /nohome

root /var/root

daemon /var/root

smmsp /private/etc/mail

www /Library/WebServer

mysql /nohome

sshd /var/empty

unknown /nohome

=======================================

2/13/2012 Gary DeRoest 69

tr



translate characters



tr [options] [string1] [string2]



-c complement

-d delete character(s)

-s squeeze multiple occurrences of

character









2/13/2012 Gary DeRoest 70

tr



translate characters



tr “a” “i”

\b

\f

\n

\r

\t

\v

\\





2/13/2012 Gary DeRoest 75

tr



translate characters



special groups of characters

[:alnum:]

[:alpha:]

[:blank:]

[:cntrl:]

[:digit:]

[:graph:]

[:lower:]

[:print:]

[:punct:]

[:space:]

[:upper:]

[:xdigit:]

2/13/2012 Gary DeRoest 76

comm



select or reject lines common to two files

comm [-123] file1 file2



The comm utility reads file1 and file2,

which should be sorted lexically,and

produces three text columns as output:

lines only in file1; lines only in

file2; and lines in both files.



-1 Suppress printing of column 1.

-2 Suppress printing of column 2.

-3 Suppress printing of column 3.



2/13/2012 Gary DeRoest 77

comm



select or reject lines common to two files

animals1 animals2

bear bear

cow cat

yak zebra



comm animals1 animals2



bear

cat

cow

yak

zebra

2/13/2012 Gary DeRoest 78

comm

select or reject lines common to two files

animals1 animals2

bear bear

cow cat

yak zebra



comm -13 animals1 animals2



cat

zebra







2/13/2012 Gary DeRoest 79

pr

print files

The pr utility is a printing and pagination

filter for text files.



When multiple input files are specified, each is

read, formatted, and written to standard output.



By default, the input is separated into 66-line

pages, each with



o A 5-line header with the page number, date,

time, and the pathname of the file.



o A 5-line trailer consisting of blank lines.





2/13/2012 Gary DeRoest 80

pr



print files

pr [options] [file…]



-h Use the string header to replace

the file name in the header line.

-l Sets the number of lines per page.

Default = 66 lines.

-d double spaces the output.

-w Set page width

-n Set the number of columns

+n Set the starting page number





2/13/2012 Gary DeRoest 81

pr



print files



pr -l15 -h “Animals” animals1



Apr 24 22:12 2003 Animals Page 1





bear

cow

yak









2/13/2012 Gary DeRoest 82

pr



print files



pr -l15 -2 -h “Animals” animals1



Apr 24 22:12 2003 Animals Page 1





bear cow

yak









2/13/2012 Gary DeRoest 83

uniq



report or filter out repeated lines in a file

uniq [-c -d -u] filename



-c Precede each output line with the

count of the number of times the

line occurred.

-d Don't output lines that are not

repeated in the input.

-u Don't output lines that are

repeated in the input.







2/13/2012 Gary DeRoest 84

uniq



report or filter out repeated lines in a file



names uniq names

bob

bob bob

frank frank

sue sue

trish trish

trish









2/13/2012 Gary DeRoest 85

uniq



report or filter out repeated lines in a file



names uniq -u names

bob

bob frank

frank sue

sue

trish

trish









2/13/2012 Gary DeRoest 86

uniq



report or filter out repeated lines in a file

names uniq -d names

bob

bob bob

frank trish

sue

trish

trish









2/13/2012 Gary DeRoest 87

uniq



report or filter out repeated lines in a file

names uniq -c names

bob

bob 2 bob

frank 1 frank

sue 1 sue

trish 2 trish

trish









2/13/2012 Gary DeRoest 88

sed



stream editor



command line execution of editor

sed [options] ‘command’ file(s)





use when sed commands are stored in a

script file

sed [options] -f scriptfile file(s)









2/13/2012 Gary DeRoest 89

sed

print





animals3 sed p animals3

polar bear

aardvark polar bear

grizzly bear polar bear

leopard aardvark

camel aardvark

grizzly bear

grizzly bear

leopard

leopard

camel

camel

2/13/2012 Gary DeRoest 90

sed

suppress

echo



animals3 sed -n p animals3

polar bear

aardvark polar bear

grizzly bear aardvark

leopard grizzly bear

camel leopard

camel









2/13/2012 Gary DeRoest 91

sed delete

line 2

print all

others



animals3 sed -n -e 2d -e p animals3

polar bear

aardvark polar bear

grizzly bear grizzly bear

leopard leopard

camel camel









2/13/2012 Gary DeRoest 92

sed

display lines

containing

text



sed -n /bear/p animals3



animals3 polar bear

polar bear grizzly bear

aardvark

grizzly bear

leopard

camel







2/13/2012 Gary DeRoest 93

sed substitute

text and

print altered

lines



sed -n s/bear/BEAR/p animals3



animals3 polar BEAR

polar bear grizzly BEAR

aardvark

grizzly bear

leopard

camel







2/13/2012 Gary DeRoest 94

sed substitute

text and print

all lines



sed -n -e s/bear/BEAR/ -e p animals3



animals3 polar BEAR

polar bear aardvark

aardvark grizzly BEAR

grizzly bear leopard

leopard camel

camel









2/13/2012 Gary DeRoest 95

sed substitute

first occurrence

of text on each

line



sed -n s/z/X/p animals3



animals3 griXzly BEAR

polar bear

aardvark

grizzly bear

leopard

camel







2/13/2012 Gary DeRoest 96

sed substitute

each occurrence

of text globally



sed -n s/z/X/gp animals3



animals3 griXXly BEAR

polar bear

aardvark

grizzly bear

leopard

camel







2/13/2012 Gary DeRoest 97

sed





sed -n -e s/z/X/g -e p animals3



animals3 polar BEAR

polar bear aardvark

aardvark griXXly BEAR

grizzly bear leopard

leopard camel

camel









2/13/2012 Gary DeRoest 98

sed Script File





script files contain sed commands



sed_script

s/bear/BEAR/

s/z/X/g

/aardvark/d









2/13/2012 Gary DeRoest 99

Using sed Script File





sed -f sed_script animals3



polar BEAR

griXXly BEAR

leopard

camel









2/13/2012 Gary DeRoest 100

Alabama:Montgomery:Birmingham:Yellowhammer:Camellia:23.10

Alaska:Juneau:Anchorage:Willow Ptarmigan:Forget-me-not:7.22

Arizona:Phoenix:Phoenix:Cactus Wren:Saguaro Cactus Blossom:100.3

Arkansas:Little Rock:Little Rock:Mockingbird:Apple Blossom:65.1

California:Sacramento:Los Angeles:California Valley Quail:Golden Poppy:1644

Colorado:Denver:Denver:Lark:Bunting Columbine:2.33

Connecticut:Hartford:Bridgeport:American Robin:Mountain Laurel:7.8

Delaware:Dover:Wilmington:Blue Hen Chicken:Peach Blossom:1287.2

Florida:Tallahassee:Jacksonville:Mockingbird:Orange Blossom:44.46









2/13/2012 Gary DeRoest 101

Script files



Shell scripts are computer programs that

contain an ordered set of statements

(commands) that the shell understands.



Using the structures available in each shell

(bash, csh, tcsh, ksh, perl, etc.) programs

can be written to perform various tasks.







2/13/2012 Gary DeRoest 102

Executing Shell Programs

• Shell programs should have the execute

permission set

– chmod u+x myfile

• If the program is located in a directory

identified in the $PATH environment variable,

then typing the command name is sufficient

• If the program is not in an aforementioned

directory, then override $PATH using the .

notation





2/13/2012 Gary DeRoest 103

Executing Shell Programs

• fill

• count

• . count

• mv count ~/bin

• count

• . /home/students/nametag

• . /home/students/nametag Samantha







2/13/2012 Gary DeRoest 104

Executing Shell Programs



• If a shell program is not set as executable,

the interpreter must precede the program

to be executed





– bash count

– tcsh /home/bin/list

– sh ./helloworld



2/13/2012 Gary DeRoest 105

Viewing Shell Programs



• cat nametag

• cat /usr/local/bin/4.sh

• cat /home/students/count









2/13/2012 Gary DeRoest 106

Creating Shell Programs



Using a text editor type the command you

wish to be executed



Identify the appropriate command

interpreter on the first line of the program



Use chmod to make program executable







2/13/2012 Gary DeRoest 107

Sample Bash Script welcome.bsh

Programmer Communicates which

Comments shell to use





#!/bin/bash

# Program to clear screen and print

# welcome

clear

echo Hello Hank! Welcome to bct2.



2/13/2012 Gary DeRoest 108

Sample Bash Script



#!/bin/bash

# Program to clear screen and print

# welcome

clear

echo Hello $USER! Welcome to bct2.



Use the logged

in users name





2/13/2012 Gary DeRoest 109

Sample Bash Script

#!/bin/bash

# Program to clear screen and print

# welcome

clear

echo Hello $USER! Welcome to $HOSTNAME.

echo Your lucky number is: $RANDOM

echo Program written by: $PROGRAMMER







2/13/2012 Gary DeRoest 110

Variables in Bash Scripts

Assigning values to temporary storage

for later use.



PROGRAMMER=Gary

echo PROGRAMMER

echo $PROGRAMMER



NUMBER=3.14

echo NUMBER

echo $NUMBER

2/13/2012 Gary DeRoest 111

Variables in Bash Scripts

Assigning values to temporary storage

for later use.

X=3

Y=2

let ANSWER=$X+$Y

echo $ANSWER









2/13/2012 Gary DeRoest 112

Variables in Bash Scripts

Values of variables containing spaces.





PROGRAMMER=“Gary DeRoest”

echo $PROGRAMMER









2/13/2012 Gary DeRoest 113

Variables in Bash Scripts

Using Double Quotation Marks

(Interpreted)



PROGRAMMER=“$USER”

echo $PROGRAMMER









2/13/2012 Gary DeRoest 114

Variables in Bash Scripts

Using Single Quotation Marks (Literal)





PROGRAMMER=‘$USER’

echo $PROGRAMMER









2/13/2012 Gary DeRoest 115

Variables in Bash Scripts

Using Backward Tick Marks





TODAY=`date`

echo $TODAY



C=`clear`

echo $C





2/13/2012 Gary DeRoest 116

The test Command



bct2]$ NAME=Tom

bct2]$ test $NAME == Tom

bct2]$ echo $?

0

bct2]$ [ $NAME == Bill ]

bct2]$ echo $?

1

bct2]$









2/13/2012 Gary DeRoest 117

The test Command



bct2]$ NAME=Tom

bct2]$ [ $NAME == T?? ]

bct2]$ echo $?

1

bct2]$





the test command

does not allow

wildcard expansion :-(





2/13/2012 Gary DeRoest 118

The compound test Command



bct2]$ NAME=Tom

bct2]$ FRIEND=Bill

bct2]$ [[ $NAME == [Tt]om ]]

bct2]$ echo $?

0

bct2]$ [[ $NAME == “Tom” && $FRIEND == “Joe” ]]

bct2]$ echo $?

1

bct2]$ [[ $NAME == “Tom” || $FRIEND == “Joe” ]]

bct2]$ echo $?

0



2/13/2012 Gary DeRoest 119

The test Command



bct2]$ x=5

bct2]$ y=20

bct2]$ [ $x -gt $y ]

bct2]$ echo $?

1

bct2]$ [ $x -le $y ]

bct2]$ echo $?

0

bct2]$







2/13/2012 Gary DeRoest 120

Numeric Comparison Operators



operator meaning example

-eq equal to [ $A –eq $B ]



-gt greater than [ $B –gt $C ]



-lt less than [ $C –lt $D ]



-ge greater or equal [ $D –ge $E ]



-le less or equal [ $E –le $D ]



-ne not equal [ $C –ne $A ]





2/13/2012 Gary DeRoest 121

String Comparison Operators



operator meaning example

= equal to [[ $A = $B ]]

== equal to [[ $B == $B ]]

!= not equal [[ $C != $C ]]

> greater [[ $D > $E ]]

than

< less than [[ $E < $D ]]



TOM, TED, TIM, TOD, TAD

2/13/2012 Gary DeRoest 122

Logic Structures

There are four basic program logic

structures.



Sequential

Decision

Looping

Case









2/13/2012 Gary DeRoest 123

Sequential Logic

#!/bin/bash

Each command

statement is #seqtotal

performed in #Input

sequence - One

after the other.

a=1

b=2

c=3

#Process

let total=$a+$b+$c

#Output

echo $total

2/13/2012 Gary DeRoest 124

Decision Logic No carriage

return here

Certain #!/bin/bash

statements #guess

will

execute

only if a echo -n “Pick a number

condition between 1 and 10: “

is met. read GUESS

if [ “$GUESS” == “7” ]

then

echo “Great job!”

fi



2/13/2012 Gary DeRoest 125

Decision Logic

#!/bin/bash

#number guess

Certain echo -n “Pick a number between 1 and 10: “

statements read GUESS

will execute

if [ “$GUESS” == 7 ]

only if a

condition is then

met. echo “Great job!”

else

echo “You’ll have to do better next

time.”

fi

date

2/13/2012 Gary DeRoest 126

The if Command os_choice



#!/bin/bash

#Choice

echo -n “What’s your favorite

operating system? ”

read OS_NAME



if [ $OS_NAME == “UNIX” ]

then

echo “You’ll love Linux.”

fi





2/13/2012 Gary DeRoest 127

The if Command os_choice



#!/bin/bash

echo -n “What’s your favorite

operating system? ”

read OS_NAME

if [ $OS_NAME == “UNIX” ]

then

echo “You’ll love Linux.”

else

echo “You should try Linux.”

fi





2/13/2012 Gary DeRoest 128

The if Command os_choice



#!/bin/bash

echo -n “What’s your favorite

operating system ? ”

read OS_NAME

if [ $OS_NAME == “UNIX” ]

then

echo “You’ll love Linux.”

fi

if [ $OS_CHOICE == “WINDOWS” ]

then

echo “Great applications”

fi

2/13/2012 Gary DeRoest 129

The if Command



#!/bin/bash

# userid

ID=`id | awk -F’[=(]’ ‘{print $2}’`

echo “Your UID is $ID.”

if [ $ID == 0 ]

then

echo “You are the SuperUser!.”

else

echo “You are an ordinary user.”

fi





2/13/2012 Gary DeRoest 130

Looping Logic

A series of commands are executed over

and over a limited number of times.



for command

while command









2/13/2012 Gary DeRoest 131

The for Command our_users



#!/bin/bash

#forloop

for USER in tom ted tad tim

do

echo “Hello $USER.”

done









2/13/2012 Gary DeRoest 132

The for Command chapters

For loops

execute a

block of #!/bin/bash

commands a #chapters

pre-

determined for FILE in file[ABC]; do

number of ls -l ~/$FILE

times

done









2/13/2012 Gary DeRoest 133

The for Command info



#!/bin/bash

#inform

for NAME in `users`; do

finger $NAME

done









2/13/2012 Gary DeRoest 134

The for Command info



#!/bin/bash

#inform2

for FILE in `users`; do

grep $FILE /etc/passwd | cut –d: -f1,6

done









2/13/2012 Gary DeRoest 135

The while Command numbers

While #!/bin/bash

loops #numbers

execute a NUM=1

block of while [ $NUM -lt 11 ]

commands do

until a echo $NUM

condition let NUM=$NUM+1

becomes sleep 1

false. done

echo I’m finished!









2/13/2012 Gary DeRoest 136

The while Command colors



#!/bin/bash

#colors

echo -n “Guess my color: “

read GUESS

while [ “$GUESS” != “red” ]

do

echo “No. Try again. “

read GUESS

done

echo “You got it!”









2/13/2012 Gary DeRoest 137

The while Command

#!/bin/bash

#nameaddr

looptest=y

while [ “$looptest” == y ]; do

echo -n “Name: “; read NAME

echo -n “Address: “; read ADDRESS

echo “$NAME:$ADDRESS”

echo -n Continue (y)es (n)o : “

read looptest

done

2/13/2012 Gary DeRoest 138

The while Command



#!/bin/bash

#sumup

NUM=0

while [[ $NUM < 5 ]]

do

echo “$NUM “

let NUM=$NUM+1

done



2/13/2012 Gary DeRoest 139

Case Logic

A decision reached between several

choices.

A program can read the contents of a

variable and execute a specific set of

commands based on the contents of the

variable.

For example, if the contents of the

variable AGE is 16 then you might want

to wish the user good luck on their

drivers test but if the contents of AGE

is 21 then you might remind them not to

drink and drive.

2/13/2012 Gary DeRoest 140

Case Logic

General structure...





case variable in

value1) command ;;

value2) command

command ;;

value3,value4) command(s) ;;

*) command(s) ;;

esac



2/13/2012 Gary DeRoest 141

The case Command manycolors

#!/bin/bash

#manycolors



echo “Pick a color: “; read COLOR

echo -n “WOW! My favorite song is “

case $COLOR in

“red”) echo -n “Red Red ”

echo -n “Wine.”;;

“blue”) echo -n “Blue Monday.”;;

“black”) echo -n “Black Velvet.”;;

*) echo -n “True Colors. “;;

esac

echo “ What a coincidence!”



2/13/2012 Gary DeRoest 142

The tput Command



tput clear Clear the screen

tput cup 10 15 Set cursor to row 10, column 15

echo Hello display text: Hello

tput cup 12 25 Set cursor to row 12, column 25

tput smso Turn on reverse type (bold)

echo “How are” display text: How are

tput cup 14 40 Set cursor to row 14, column 40

tput rmso Turn off reverse type (regular)

echo YOU? display text: YOU?

tput cup 20 0 Set cursor to row 20, column 0





2/13/2012 Gary DeRoest 143

The tput Command



#!/bin/bash

#Demo tput command



#clear screen

clear



#place cursor

tput cup 15 37

echo -n "Hello"

tput cup 16 37

echo -n "-----"

tput cup 24 0



2/13/2012 Gary DeRoest 144

Shell Functions



One or more commands stored in memory

and assigned a name.



datenow ()

{

date

}









2/13/2012 Gary DeRoest 145

Shell Functions

A script file may contain a library of

functions which are loaded into

memory.



sort_name ()

{

sort –k2 –t: corp_phones

}

sort_date ()

{

sort –k7 –t: corp_phones

}







2/13/2012 Gary DeRoest 146

Shell Functions

A script file can load functions into

memory when manually executed or

automatically when executed within a

startup script



. ~/.myfuncs





~/.bashrc









2/13/2012 Gary DeRoest 147

Shell Functions



One or more commands stored in memory

and assigned a name.



datenow ()

{

date

}









2/13/2012 Gary DeRoest 148



Related docs
Other docs by zhouwenjuan
CanaDream Business Plan
Views: 10  |  Downloads: 0
Cash on Hand
Views: 7  |  Downloads: 0
Cash In On What's Hiding In Your Closet
Views: 7  |  Downloads: 0
CASH FOR CARS
Views: 7  |  Downloads: 0
Cases Filed for CV
Views: 69  |  Downloads: 0
Case Study
Views: 7  |  Downloads: 0
Case Study 1 – Small Business Corporations
Views: 9  |  Downloads: 0
Case Studies on EU citizenship
Views: 7  |  Downloads: 0
CASE Spring Newsletter
Views: 8  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!