UNIX Shell-Scripting Basics
-
Upload
kulandhaiswamychrist -
Category
Documents
-
view
20 -
download
0
description
Transcript of UNIX Shell-Scripting Basics
-
UNIX Shell-Scripting Basics
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Agenda
What is a shell? A shell script?
Introduction to bash
Running Commands
Applied Shell Programming
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
What is a shell?
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
What is a shell?
/bin/bash
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
What is a shell?
#!/bin/bash
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
What is a shell?
INPUT
shell
OUTPUT ERROR
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
What is a shell?
Any Program
But there are a few popular shells
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Bourne Shells
/bin/sh
/bin/bash Bourne-Again Shell
Steve Bourne
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Other Common Shells
C Shell (/bin/csh)
Turbo C Shell (/bin/tcsh)
Korn Shell (/bin/ksh)
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
An aside: What do I mean by /bin ?
C Shell (/bin/csh)
Turbo C Shell (/bin/tcsh)
Korn Shell (/bin/ksh)
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
An aside: What do I mean by /bin ?
/bin, /usr/bin, /usr/local/bin
/sbin, /usr/sbin, /usr/local/sbin
/tmp
/dev
/home/borwicjh
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
What is a Shell Script?
A Text File
With Instructions
Executable
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
What is a Shell Script?
% cat > hello.sh
-
What is a Shell Script? A Text File
% cat > hello.sh
-
An aside: Redirection
cat > /tmp/myfile
cat >> /tmp/myfile
cat 2> /tmp/myerr
cat < /tmp/myinput
cat &1
INPUT
env
OUTPUT ERROR
0
1 2
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
What is a Shell Script? How To Run
% cat > hello.sh
-
What is a Shell Script? What To Do
% cat > hello.sh
-
What is a Shell Script? Executable
% cat > hello.sh
-
What is a Shell Script? Running it
% cat > hello.sh
-
Finding the program: PATH
% ./hello.sh
echo vs. /usr/bin/echo
% echo $PATH /bin:/usr/bin:/usr/local/bin: /home/borwicjh/bin
% which echo /usr/bin/echo
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Variables and the Environment
% hello.sh
bash: hello.sh: Command not found
% PATH=$PATH:.
% hello.sh
Hello, world
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
An aside: Quoting
% echo $USER $USER % echo $USER borwicjh % echo \ % echo deacnet\\sct deacnet\sct % echo \ \
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Variables and the Environment
% env [variables passed to sub-programs] % NEW_VAR=Yes % echo $NEW_VAR Yes % env [PATH but not NEW_VAR] % export NEW_VAR % env [PATH and NEW_VAR]
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Welcome to Shell Scripting!
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
How to Learn
man man bash
man cat
man man
man k man k manual
Learning the Bash Shell, 2nd Ed.
Bash Reference Cards
http://www.tldp.org/LDP/abs/html/
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Introduction to bash
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Continuing Lines: \
% echo This \
Is \
A \
Very \
Long \
Command Line
This Is A Very Long Command Line
%
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Exit Status
$?
0 is True
% ls /does/not/exist
% echo $?
1
% echo $?
0
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Exit Status: exit
% cat > test.sh
-
Logic: test
% test 1 -lt 10
% echo $?
0
% test 1 == 10
% echo $?
1
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: test
test
[ ] [ 1 lt 10 ]
[[ ]] [[ this string =~ this ]]
(( )) (( 1 < 10 ))
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: test
[ -f /etc/passwd ]
[ ! f /etc/passwd ]
[ -f /etc/passwd a f /etc/shadow ]
[ -f /etc/passwd o f /etc/shadow ]
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
An aside: $(( )) for Math
% echo $(( 1 + 2 ))
3
% echo $(( 2 * 3 ))
6
% echo $(( 1 / 3 ))
0
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: if
if something
then
:
# elif a contraction of else if:
elif something-else
then
:
else
then
:
fi
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: if
if [ $USER eq borwicjh ]
then
:
# elif a contraction of else if:
elif ls /etc/oratab
then
:
else
then
:
fi
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: if
# see if a file exists
if [ -e /etc/passwd ]
then
echo /etc/passwd exists
else
echo /etc/passwd not found!
fi
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: for
for i in 1 2 3
do
echo $i
done
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: C-style for
for (( expr1 ;
expr2 ;
expr3 ))
do
list
done
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: C-style for
LIMIT=10
for (( a=1 ;
a
-
Logic: while
while something
do
:
done
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Logic: while
a=0; LIMIT=10
while [ "$a" -lt "$LIMIT" ]
do
echo -n "$a
a=$(( a + 1 ))
done
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Counters
COUNTER=0
while [ -e $FILE.COUNTER ]
do
COUNTER=$(( COUNTER + 1))
done
Note: race condition
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Reusing Code: Sourcing
% cat > /path/to/my/passwords
-
Variable Manipulation
% FILEPATH=/path/to/my/output.lis
% echo $FILEPATH
/path/to/my/output.lis
% echo ${FILEPATH%.lis}
/path/to/my/output
% echo ${FILEPATH#*/}
path/to/my/output.lis
% echo ${FILEPATH##*/}
output.lis
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
It takes a long time to
become a bash
guru
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Running Programs
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Reasons for Running Programs
Check Return Code $?
Get Job Output OUTPUT=`echo Hello`
OUTPUT=$(echo Hello)
Send Output Somewhere Redirection:
Pipes
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Pipes
Lots of Little Tools
echo Hello | \
wc -c
INPUT
echo
OUTPUT ERROR
0
1 2
INPUT
wc
OUTPUT ERROR
0
1 2
A Pipe!
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Email Notification
% echo Message | \
mail s Heres your message \
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Dates
% DATESTRING=`date +%Y%m%d`
% echo $DATESTRING
20060125
% man date
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
FTP the Hard Way
ftp n u server.wfu.edu
-
FTP with wget
wget \ ftp://user:[email protected]/file
wget r \ ftp://user:[email protected]/dir/
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
FTP with curl
curl T upload-file \
-u username:password \
ftp://server.wfu.edu/dir/file
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Searching: grep
% grep rayra /etc/passwd
% grep r rayra /etc
% grep r RAYRA /etc
% grep ri RAYRA /etc
% grep rli rayra /etc
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Searching: find
% find /home/borwicjh \
-name *.lis
[all files matching *.lis]
% find /home/borwicjh \
-mtime -1 name *.lis
[*.lis, if modified within 24h]
% man find
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Searching: locate
% locate .lis
[files with .lis in path]
% locate log
[also finds /var/log/messages]
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Applied Shell Programming
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Make Your Life Easier
TAB completion
Control+R
history
cd -
Study a UNIX Editor
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
pushd/popd
% cd /tmp
% pushd /var/log
/var/log /tmp
% cd ..
% pwd
/var
% popd
/tmp
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Monitoring processes
ps
ps ef
ps u oracle
ps C sshd
man ps
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
DOS Mode Files
#!/usr/bin/bash^M
FTP transfer in ASCII, or
dos2unix infile > outfile
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
sqlplus
JOB=ZZZTEST
PARAMS=ZZZTEST_PARAMS
PARAMS_USER=BORWICJH
sqlplus $BANNER_USER/$BANNER_PW
-
sqlplus
sqlplus $USER/$PASS @$FILE_SQL \ $ARG1 $ARG2 $ARG3 if [ $? ne 0 ] then exit 1 fi if [ -e /file/sql/should/create ] then [use SQL-created file] fi Ask Amy Lamy!
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Passing Arguments
% cat > test.sh
-
INB Job Submission Template
$1: user ID
$2: password
$3: one-up number
$4: process name
$5: printer name
% /path/to/your/script $UI $PW \
$ONE_UP $JOB $PRNT
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Scheduling Jobs
% crontab -l
0 0 * * * daily-midnight-job.sh
0 * * * * hourly-job.sh
* * * * * every-minute.sh
0 1 * * 0 1AM-on-sunday.sh
% EDITOR=vi crontab e
% man 5 crontab
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M
-
Other Questions?
Shells and Shell Scripts
bash
Running Commands
bash and Banner in Practice
http://oracleapps88.blogspot.com/
O
R
A
C
L
E
A
P
P
S
8
8
.
B
L
O
G
S
P
O
T
.
C
O
M