WWW - Wfu.edu/ borwicjh/presentations/UNIX Shell-Scripting Basics
WWW - Wfu.edu/ borwicjh/presentations/UNIX Shell-Scripting Basics
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt
Agenda
What
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
What is a shell?
%
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
What is a shell?
/bin/bash
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
What is a shell?
#!/bin/bash
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
What is a shell?
INPUT
shell
OUTPUT
ERROR
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
What is a shell?
Any
Program
But there are a few popular shells
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Bourne Shells
/bin/sh
/bin/bash
Bourne-Again Shell
Steve Bourne
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Shell (/bin/csh)
Turbo C Shell (/bin/tcsh)
Korn Shell (/bin/ksh)
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Shell (/bin/csh)
Turbo C Shell (/bin/tcsh)
Korn Shell (/bin/ksh)
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
/usr/bin, /usr/local/bin
/sbin, /usr/sbin, /usr/local/sbin
/tmp
/dev
/home/borwicjh
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
File
With Instructions
Executable
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
An aside: Redirection
cat
> /tmp/myfile
cat >> /tmp/myfile
cat 2> /tmp/myerr
cat < /tmp/myinput
cat <<INPUT
Some input
INPUT
cat > /tmp/x 2>&1
INPUT 0
env
OUTPUT 1
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
ERROR 2
./hello.sh
echo vs. /usr/bin/echo
% echo $PATH
/bin:/usr/bin:/usr/local/bin:
/home/borwicjh/bin
% which echo
/usr/bin/echo
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
An aside: Quoting
% echo $USER
$USER
% echo $USER
borwicjh
% echo \
% echo deacnet\\sct
deacnet\sct
% echo \
\
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
How to Learn
man
man bash
man cat
man man
man
man k manual
Learning
Introduction to bash
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt
Continuing Lines: \
% echo This \
Is \
A \
Very \
Long \
Command Line
This Is A Very Long Command Line
%
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Exit Status
$?
0
is True
% ls /does/not/exist
% echo $?
1
% echo $?
0
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: test
%
%
0
%
%
1
test 1 -lt 10
echo $?
test 1 == 10
echo $?
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: test
test
[
[[
1 lt 10 ]
]]
[[
((
))
((
1 < 10 ))
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: test
[
-f /etc/passwd ]
[ ! f /etc/passwd ]
[ -f /etc/passwd a f /etc/shadow ]
[ -f /etc/passwd o f /etc/shadow ]
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: if
if something
then
:
# elif a contraction of else if:
elif something-else
then
:
else
then
:
fi
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: if
if [ $USER eq borwicjh ]
then
:
# elif a contraction of else if:
elif ls /etc/oratab
then
:
else
then
:
fi
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: if
# see if a file exists
if [ -e /etc/passwd ]
then
echo /etc/passwd exists
else
echo /etc/passwd not found!
fi
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: for
for i in 1 2 3
do
echo $i
done
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
;
;
))
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: while
while something
do
:
done
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Logic: while
a=0; LIMIT=10
while [ "$a" -lt "$LIMIT" ]
do
echo -n "$a
a=$(( a + 1 ))
done
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Counters
COUNTER=0
while [ -e $FILE.COUNTER ]
do
COUNTER=$(( COUNTER + 1))
done
Note:
race condition
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
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
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Running Programs
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt
Return Code
$?
Get
Job Output
OUTPUT=`echo
Hello`
OUTPUT=$(echo Hello)
Send
Output Somewhere
Redirection:
<, >
Pipes
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Pipes
Lots
of Little Tools
INPUT 0
echo
echo Hello | \
wc -c
OUTPUT 1
ERROR 2
A Pipe!
INPUT 0
wc
OUTPUT 1
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
ERROR 2
Email Notification
% echo Message | \
mail s Heres your message \
[email protected]
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Dates
% DATESTRING=`date +%Y%m%d`
% echo $DATESTRING
20060125
% man date
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
\
ftp://user:[email protected]/file
wget r \
ftp://user:[email protected]/dir/
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Searching: grep
%
%
%
%
%
grep
grep
grep
grep
grep
rayra /etc/passwd
r rayra /etc
r RAYRA /etc
ri RAYRA /etc
rli rayra /etc
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
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
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Searching: locate
% locate .lis
[files with .lis in path]
% locate log
[also finds /var/log/messages]
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt
completion
Control+R
history
cd Study a UNIX Editor
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
pushd/popd
% cd /tmp
% pushd /var/log
/var/log /tmp
% cd ..
% pwd
/var
% popd
/tmp
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Monitoring processes
ps
ps
ef
ps u oracle
ps C sshd
man ps
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
transfer in ASCII, or
dos2unix infile > outfile
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
sqlplus
JOB=ZZZTEST
PARAMS=ZZZTEST_PARAMS
PARAMS_USER=BORWICJH
sqlplus $BANNER_USER/$BANNER_PW << _EOF_
set serveroutput on
set sqlprompt ""
EXECUTE WF_SATURN.FZ_Get_Parameters('$JOB',
'$PARAMS', '$PARAMS_USER');
_EOF_
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
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
Passing Arguments
% cat > test.sh <<_TEST_
echo Your name is \$1 \$2
_TEST_
% chmod +x test.sh
% ./test.sh John Borwick ignorethis
Your name is John Borwick
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Scheduling Jobs
%
0
0
*
0
%
%
crontab -l
0 * * * daily-midnight-job.sh
* * * * hourly-job.sh
* * * * every-minute.sh
1 * * 0 1AM-on-sunday.sh
EDITOR=vi crontab e
man 5 crontab
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-
Other Questions?
Shells
bash
Running
Commands
bash and Banner in Practice
www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-