Вы находитесь на странице: 1из 71

http://oracleapps88.blogspot.

com/

.C
T

G
UNIX Shell-Scripting
Basics
O
L

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

Agenda

What is a shell? A shell script? O


P
Introduction to bash
S
G
Running Commands LO
B
.
Applied Shell Programming
8
8

S
P

A
E

.C
T

http://oracleapps88.blogspot.com/

What is a shell?

.B
8

S
P

A
E

.C
T

http://oracleapps88.blogspot.com/

What is a shell?

.C
T

/bin/bash
L

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

What is a shell?

.C
T

#!/bin/bash
L

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

What is a shell?

.C
T

INPUT

.B
8

8
shell

S
P

A
E

A
OUTPUT
R

ERROR

http://oracleapps88.blogspot.com/

What is a shell?

.C
T

Any Program
O
P
But there are a few popular shells
S
L

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

Bourne Shells

.C
T

/bin/sh
.B
8
8 /bin/bash
S
P
Bourne-Again Shell

A
E

Steve Bourne

http://oracleapps88.blogspot.com/

Other Common Shells

C Shell (/bin/csh)
O
P
Turbo C Shell (/bin/tcsh) S
G
Korn Shell (/bin/ksh)LO
8

.B
8

S
P

A
E

.C
T

http://oracleapps88.blogspot.com/

An aside: What do I mean by /bin ? M


O

C Shell (/bin/csh)
O
P
Turbo C Shell (/bin/tcsh) S
G
Korn Shell (/bin/ksh)LO
8

.B
8

S
P

A
E

.C
T

http://oracleapps88.blogspot.com/

An aside: What do I mean by /bin ? M


.C
/bin, /usr/bin, /usr/local/bin T
O
/sbin, /usr/sbin, /usr/local/sbin
P
S
G
/tmp
O
L
/dev
.B
8
/home/borwicjh
8
S
P
P
A
E
L
C
A
R
O

http://oracleapps88.blogspot.com/

What is a Shell Script?

A Text File
With Instructions
Executable

.B
8

S
P

A
E

.C
T

http://oracleapps88.blogspot.com/

What is a Shell Script?


.C
T
% cat > hello.sh <<MY_PROGRAM
O
P
#!/bin/sh
S
G
echo Hello, worldLO
B
.
MY_PROGRAM
8
8
S
% chmod +x hello.sh
P
P
A
% ./hello.sh
E
L
Hello,C world
A
R
O

http://oracleapps88.blogspot.com/

What is a Shell Script? A Text File M


.C
T
% cat > hello.sh <<MY_PROGRAM
O
P
#!/bin/sh
S
G
echo Hello, worldLO
B
.
MY_PROGRAM
8
8
S
% chmod +x hello.sh
P
P
A
% ./hello.sh
E
L
Hello,C world
A
R
O

http://oracleapps88.blogspot.com/

An aside: Redirection

.C
T

cat > /tmp/myfile


O
cat >> /tmp/myfile
P
S
G 0
cat 2> /tmp/myerr
INPUT
O
L
cat < /tmp/myinput
env
.B
8
cat <<INPUT
8
OUTPUT 1
S
Some input
P
INPUT
P
A
E
cat > /tmp/x
2>&1
L
C
A
R
O

ERROR 2

http://oracleapps88.blogspot.com/

What is a Shell Script? How To Run


M
.C
T
% cat > hello.sh <<MY_PROGRAM
O
P
#!/bin/sh
S
G
echo Hello, worldLO
B
.
MY_PROGRAM
8
8
S
% chmod +x hello.sh
P
P
A
% ./hello.sh
E
L
Hello,C world
A
R
O

http://oracleapps88.blogspot.com/

What is a Shell Script? What To DoM


.C
T
% cat > hello.sh <<MY_PROGRAM
O
P
#!/bin/sh
S
G
echo Hello, worldLO
B
.
MY_PROGRAM
8
8
S
% chmod +x hello.sh
P
P
A
% ./hello.sh
E
L
Hello,C world
A
R
O

http://oracleapps88.blogspot.com/

What is a Shell Script? ExecutableM


.C
T
% cat > hello.sh <<MY_PROGRAM
O
P
#!/bin/sh
S
G
echo Hello, worldLO
B
.
MY_PROGRAM
8
8
S
% chmod +x hello.sh
P
P
A
% ./hello.sh
E
L
Hello,C world
A
R
O

http://oracleapps88.blogspot.com/

What is a Shell Script? Running it


.C
T
% cat > hello.sh <<MY_PROGRAM
O
P
#!/bin/sh
S
G
echo Hello, worldLO
B
.
MY_PROGRAM
8
8
S
% chmod +x hello.sh
P
P
A
% ./hello.sh
E
L
Hello,C world
A
R
O

http://oracleapps88.blogspot.com/

Finding the program: PATH

.C
T

% ./hello.sh
O
P
echo vs. /usr/bin/echo S
G
O
% echo $PATH
L
.B
/bin:/usr/bin:/usr/local/bin:
8
8
/home/borwicjh/bin
S
P
P
% which A
echo
E
/usr/bin/echo
L
C

http://oracleapps88.blogspot.com/

Variables and the Environment


O

.C
T

% hello.sh
O
P
bash: hello.sh: CommandS not
G
found
O
L
% PATH=$PATH:.8.B
8
% hello.sh PS
P
Hello, world
A
L

http://oracleapps88.blogspot.com/

An aside: Quoting

% echo $USER
O
$USER
P
S
% echo $USER
G
O
borwicjh
L
% echo \
.B
8

8
S
P
% echo deacnet\\sct
P
deacnet\sctA
E
L
% echo \
C
\
A
R
O

.C
T

http://oracleapps88.blogspot.com/

Variables and the Environment

.C
T

% env
O
[variables passed to sub-programs]
P
S
% NEW_VAR=Yes
G
O
% echo $NEW_VAR
L
Yes
.B
8
% env
8
S
[PATH but notP NEW_VAR]
P
% export NEW_VAR
A
E
L
% env
C
[PATH
A and NEW_VAR]
R
O

http://oracleapps88.blogspot.com/

Welcome to Shell Scripting!


O

.B
8

S
P

A
E

.C
T

http://oracleapps88.blogspot.com/

How to Learn

man

.C
T

man bash
man cat
man man

man k

man k manual S

.B
8

P
P Shell, 2nd Ed.
Learning the Bash
A
E
Bash Reference
Cards
L
C
http://www.tldp.org/LDP/abs/html/
A
R
O

http://oracleapps88.blogspot.com/

.C
T

G to bash
Introduction
O
L

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

Continuing Lines: \

.C
T

% echo This \
O
P
Is \
S
G
A \
O
L
Very \
.B
8
8
Long \
S
P
Command Line
P
A
This IsLE
A Very Long Command Line
C
%
A
O

http://oracleapps88.blogspot.com/

Exit Status

$?
0 is True

% ls /does/not/exist
.B
8
8
% echo $?
S
P
1
P
A
E
% echo L
$?
C
0
A
O

.C
T

http://oracleapps88.blogspot.com/

Exit Status: exit

% cat > test.sh <<_TEST_ O


P
exit 3
S
G
O
_TEST_
L
B
.
% chmod +x test.sh
8
8
% ./test.sh PS
P
A
% echo $?
E
L
C
3
A

.C
T

http://oracleapps88.blogspot.com/

Logic: test

%
%
0
%
%
1

test 1 -lt 10
echo $?

.B
8

test 1 == 10
8
S
echo $?
P
L

A
E

.C
T

http://oracleapps88.blogspot.com/

Logic: test

test
[ ]
[ 1 lt 10 ]

.C
T

.B
8
8
[[ this string
=~ this ]]
S
P
P
(( ))
A
E
(( 1 L< 10 ))
C
A
R
O

[[ ]]

http://oracleapps88.blogspot.com/

Logic: test

[
[
[
[

.C
T

-f /etc/passwd ]
O
! f /etc/passwd ]
P
S
G
-f /etc/passwd a f /etc/shadow
]
O
L /etc/shadow ]
-f /etc/passwd o f
.B
8
8
S
P
P
A
E
L
C
A
R

http://oracleapps88.blogspot.com/

An aside: $(( )) for Math

% echo $(( 1 + 2 ))
O
P
3
S
G
% echo $(( 2 * 3 ))LO
B
.
6
8
8
% echo $(( 1PS
/ 3 ))
P
A
0
E
L

.C
T

http://oracleapps88.blogspot.com/

Logic: if

.C
T

if something
O
then
P
S
:
G
# elif a contraction of O
else if:
L
elif something-else
.B
8
then
8
S
:
P
P
else
A
E
then
L
:
C
A
fi R

http://oracleapps88.blogspot.com/

Logic: if

.C
T

if [ $USER eq borwicjh ]
O
then
P
S
:
G
# elif a contraction of O
else if:
L
elif ls /etc/oratab
.B
8
then
8
S
:
P
P
else
A
E
then
L
:
C
A
fi R

http://oracleapps88.blogspot.com/

Logic: if

.C
T

# see if a file exists


O
P
if [ -e /etc/passwd ] S
G
O
then
L
B
.
echo /etc/passwd
exists
8
8
S
else
P
P
A
echo /etc/passwd
not found!
E
L
C
fi
A

http://oracleapps88.blogspot.com/

Logic: for

for i in 1 2 3
O
P
do
S
G
O
echo $i
L
B
.
done
8
8
P

S
P

A
E

.C
T

http://oracleapps88.blogspot.com/

Logic: for

for i in /*
O
P
do
S
G
O
echo Listing $i:
L
B
.
ls -l $i
8
8
S
read
P
P
A
done
E
L

.C
T

http://oracleapps88.blogspot.com/

Logic: for

for i in /*
O
P
do
S
G
O
echo Listing $i:
L
B
.
ls -l $i
8
8
S
read
P
P
A
done
E
L

.C
T

http://oracleapps88.blogspot.com/

Logic: for

for i in /*
O
P
do
S
G
O
echo Listing $i:
L
B
.
ls -l $i
8
8
S
read
P
P
A
done
E
L

.C
T

http://oracleapps88.blogspot.com/

Logic: C-style for

for (( expr1
;
S
G
expr2
; LO
B
.
expr3
8))
8
S
do
P
P
list EA
L
done C
A

.C
T

http://oracleapps88.blogspot.com/

Logic: C-style for

LIMIT=10
O
P
for (( a=1
;
S
G
a<=LIMIT ; LO
B
.
a++
8))
8
S
do
P
P
A$a
echo n
E
L
done C
A

.C
T

http://oracleapps88.blogspot.com/

Logic: while

while something
S
G
O
do
L
B
.
:
8
8

done

A
E

S
P

.C
T

http://oracleapps88.blogspot.com/

Logic: while

.C
T

a=0; LIMIT=10
O
P
while [ "$a" -lt "$LIMIT"
]
S
G
O
do
L
B
.
echo -n "$a 8
8
a=$(( a + P
1S))
P
A
done
E
L

http://oracleapps88.blogspot.com/

Counters

.C
T

COUNTER=0
O
P
while [ -e $FILE.COUNTER
]
S
G
O
do
L
B
.
COUNTER=$(( COUNTER
+ 1))
8
8
S
done
P
P

A
E

C
Note:
race
condition
A
O

http://oracleapps88.blogspot.com/

Reusing Code: Sourcing


.C
% cat > /path/to/my/passwords <<_PW_
T
O
FTP_USER=sct
P
S
G
_PW_
O
L
% echo $FTP_USER
.B
8
8
S
% . /path/to/my/passwords
P
P
% echo $FTP_USER
A
E
sct
L
C
A
%
R
O

http://oracleapps88.blogspot.com/

Variable Manipulation
C
.
% FILEPATH=/path/to/my/output.lis
T
O
% echo $FILEPATH
P
S
/path/to/my/output.lis G
O
L
% echo ${FILEPATH%.lis}
.B
/path/to/my/output8
8
S
% echo ${FILEPATH#*/}
P
P
path/to/my/output.lis
A
E
% echo ${FILEPATH##*/}
L
C
output.lis
A
R
O

http://oracleapps88.blogspot.com/

.C
T

O
L
.ItBtakes a long time to
8 become a bash

S
P

A
E

guru

http://oracleapps88.blogspot.com/

.C
T

GPrograms
Running
O
L

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

Reasons for Running Programs


O

Check Return Code


$?

Get Job Output

O
L
OUTPUT=`echo Hello`
.B
8
8Hello)
OUTPUT=$(echo
S
P
PSomewhere
Send Output
A
E <, >
Redirection:
L
C
Pipes
A
R
O

.C
T

http://oracleapps88.blogspot.com/

Pipes

Lots of Little Tools

INPUT 0

.C
T

O
echo

echo Hello | \ L
B
.
wc -c
8
8
P

S
P

A
E

OUTPUT 1

A Pipe!
INPUT 0
wc

ERROR 2

OUTPUT 1

ERROR 2

http://oracleapps88.blogspot.com/

Email Notification

.C
T

% echo Message | \
O
P
mail s Heres your message
\
S
G
borwicjh@wfu.edu LO
8

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

Dates
.C
T
% DATESTRING=`date +%Y%m%d`
O
P
% echo $DATESTRING
S
G
O
20060125
L
B
.
% man date
8
8
S
P
P
A
E
L
C
A
R
O

http://oracleapps88.blogspot.com/

FTP the Hard Way

.C
T
<<_FTP_
O

ftp n u server.wfu.edu
P
user username password S
G
O
put FILE
L
B
.
_FTP_
8
8

S
P

A
E

http://oracleapps88.blogspot.com/

FTP with wget


O

.C
wget \
T
O
ftp://user:pass@server.wfu.edu/file
P
S
wget r \
G
O
ftp://user:pass@server.wfu.edu/dir/
L
.B
8
8
S
P
P
A
E
L
C
A
R
O

http://oracleapps88.blogspot.com/

FTP with curl

.C
T

curl T upload-file \
O
P
-u username:password S\
G
O
ftp://server.wfu.edu/dir/file
L
8

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

Searching: grep

%
%
%
%
%

grep
grep
grep
grep
grep

rayra /etc/passwd O
P
r rayra /etc S
G
O
r RAYRA /etc
L
B
.
ri RAYRA8 /etc
8
S
rli P
rayra
/etc
P

A
E

.C
T

http://oracleapps88.blogspot.com/

Searching: find

% find /home/borwicjh \ O
P
-name *.lis
S

.C
T

[all files matchingLO


*.lis]

B
.
% find /home/borwicjh
\
8
8
-mtime -1PS
name *.lis
P
[*.lis, E
ifA modified within
L
% man C
find
A
R
O

24h]

http://oracleapps88.blogspot.com/

Searching: locate

% locate .lis

.C
T

[files with .lis in path]


S
% locate log

B
.
/var/log/messages]
8
8
S
P

[also finds
P

A
E

http://oracleapps88.blogspot.com/

.C
T

G
Applied Shell Programming
O
L

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

Make Your Life Easier

TAB completion
O
P
Control+R
S
G
O
history
L
B
.
cd 8
8
S
Study a UNIX P
Editor
P

A
E

.C
T

http://oracleapps88.blogspot.com/

pushd/popd

% cd /tmp
O
P
% pushd /var/log
S
G
/var/log /tmp
O
L
% cd ..
.B
8
8
% pwd
S
P
/var
P
A
% popd LE
C
/tmp A
O

.C
T

http://oracleapps88.blogspot.com/

Monitoring processes

ps
O
P
ps ef
S
G
O
ps u oracle
L
B
.
ps C sshd
8
8
S
man ps
P
P

A
E

.C
T

http://oracleapps88.blogspot.com/

DOS Mode Files

.C
T

#!/usr/bin/bash^M
O
P
FTP transfer in ASCII, or
S
G
O
dos2unix infile >Loutfile
8

.B
8

S
P

A
E

http://oracleapps88.blogspot.com/

sqlplus

.C
T

JOB=ZZZTEST
O
PARAMS=ZZZTEST_PARAMS
P
S
PARAMS_USER=BORWICJH
G
O
sqlplus $BANNER_USER/$BANNER_PW
<< _EOF_
L
.B
set serveroutput on
8
set sqlprompt "" S8

A
EXECUTE WF_SATURN.FZ_Get_Parameters('$JOB',
E
L '$PARAMS_USER');
'$PARAMS',
C
_EOF_ A
O

http://oracleapps88.blogspot.com/

sqlplus

sqlplus
$ARG1
if [ $?
then
exit 1
fi
if [ -e
then

$USER/$PASS @$FILE_SQL \
O
$ARG2 $ARG3
P
ne 0 ]
S

8
/file/sql/should/create
]
S
P

P
[use SQL-created
file]
A

fi

A Lamy!
AskRAmy
O

.B
8

.C
T

http://oracleapps88.blogspot.com/

Passing Arguments

.C
T

% cat > test.sh <<_TEST_ O


P
echo Your name is \$1 S\$2
G
O
_TEST_
L
B
.
% chmod +x test.sh
8
8
S
% ./test.sh P
John
Borwick ignoreP
this
A
E
L is John Borwick
Your name
C
A

http://oracleapps88.blogspot.com/

INB Job Submission Template

$1: user ID
O
P
$2: password
S
G
$3: one-up number
O
L
$4: process name
.B
8
$5: printer name S8

.C
T

A
E
/path/to/your/script
L
C
$ONE_UP
$JOB $PRNT
A
R
O

$UI $PW \

http://oracleapps88.blogspot.com/

Scheduling Jobs

%
0
0
*
0
%
%

.C
T

crontab -l
O
P
0 * * * daily-midnight-job.sh
S
G
O
* * * * hourly-job.sh
L
B
.
* * * * every-minute.sh
8
8
S
1 * * 0 1AM-on-sunday.sh
P
P
A crontab e
EDITOR=vi
E
L
man C
5 crontab
A

http://oracleapps88.blogspot.com/

.B
8

S
P

A
E

.C
T

http://oracleapps88.blogspot.com/

Other Questions?

Shells and Shell Scripts


O
P
bash
S
G
Running Commands LO
B
.
bash and Banner in
8 Practice
8

S
P

A
E

.C
T

Вам также может понравиться