UNIX Shell-Scripting Basics
UNIX Shell-Scripting Basics
Agenda
%▌
What is a shell?
/bin/bash
What is a shell?
#!/bin/bash
What is a shell?
INPUT
shell
OUTPUT ERROR
What is a shell?
Any Program
Bourne Shells
/bin/sh
/bin/bash
Steve Bourne
Other Common Shells
C Shell (/bin/csh)
Turbo C Shell (/bin/tcsh)
Korn Shell (/bin/ksh
An aside: What do I mean by /bin ?
C Shell (/bin/csh)
Turbo C Shell (/bin/tcsh)
Korn Shell (/bin/ksh)
An aside: What do I mean by /bin ?
/bin, /usr/bin, /usr/local/bin
/sbin, /usr/sbin, /usr/local/sbin
/tmp
/dev
What is a Shell Script?
A Text File
With Instructions
What is a Shell Script?
INPUT
cat > /tmp/x
What is a Shell Script? How To Run
% ./hello.sh
echo vs. /usr/bin/echo
% echo $PATH
/bin:/usr/bin:/usr/local/bin:
/home/borwicjh/bin
% which echo
Variables and the Environment
% hello.sh
bash: hello.sh: Command not
found
% PATH=“$PATH:.”
% hello.sh
An aside: Quoting
% echo ‘$USER’
$USER
% echo “$USER”
borwicjh
% echo “\””
”
% echo “deacnet\\sct”
deacnet\sct
% echo ‘\”’
\”
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
Welcome to Shell Scripting!
PATH chmod
How to Learn
man
man bash
man cat
man man
man –k
man –k manual
Learning the Bash Shell, 2nd Ed.
“Bash Reference” Cards
Introduction to bash
Continuing Lines: \
% echo This \
Is \
A \
Very \
Long \
Command Line
This Is A Very Long Command Line
Exit Status
$?
0 is True
% ls /does/not/exist
% echo $?
1
% echo $?
Exit Status: exit
% test 1 -lt 10
% echo $?
0
% test 1 == 10
% echo $?
Logic: test
test
[ ]
[ 1 –lt 10 ]
[[ ]]
[[ “this string” =~ “this” ]]
(( ))
Logic: test
[ -f /etc/passwd ]
[ ! –f /etc/passwd ]
[ -f /etc/passwd –a –f /etc/shadow ]
An aside: $(( )) for Math
% echo $(( 1 + 2 ))
3
% echo $(( 2 * 3 ))
6
% echo $(( 1 / 3 ))
Logic: if
if something
then
:
# “elif” a contraction of “else if”:
elif something-else
then
:
else
then
:
fi
Logic: if
if [ $USER –eq “borwicjh” ]
then
:
# “elif” a contraction of “else if”:
elif ls /etc/oratab
then
:
else
then
:
fi
Logic: if
for i in 1 2 3
do
echo $i
done
Logic: for
for i in /*
do
echo “Listing $i:”
ls -l $i
read
done
Logic: for
for i in /*
do
echo “Listing $i:”
ls -l $i
read
done
Logic: for
for i in /*
do
echo “Listing $i:”
ls -l $i
read
done
Logic: C-style for
for (( expr1 ;
expr2 ;
expr3 ))
do
list
done
Logic: C-style for
LIMIT=10
for (( a=1 ;
a<=LIMIT ;
a++ ))
do
echo –n “$a ”
done
Logic: while
while something
do
:
done
Logic: while
a=0; LIMIT=10
while [ "$a" -lt "$LIMIT" ]
do
echo -n "$a ”
a=$(( a + 1 ))
done
Counters
COUNTER=0
while [ -e “$FILE.COUNTER” ]
do
COUNTER=$(( COUNTER + 1))
done
Reusing Code: “Sourcing”
% cat > /path/to/my/passwords <<_PW_
FTP_USER=“sct”
_PW_
% echo $FTP_USER
% . /path/to/my/passwords
% echo $FTP_USER
sct
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
It takes a long time to
become a bash
guru…
Running Programs
Reasons for Running Programs
OUTPUT 1 ERROR 2
echo “Hello” | \
A Pipe!
INPUT 0
wc
OUTPUT 1 ERROR 2
Email Notification
% echo “Message” | \
mail –s “Here’s your message” \
[email protected]
Dates
% DATESTRING=`date +%Y%m%d`
% echo $DATESTRING
20060125
FTP the Hard Way
curl –T upload-file \
-u username:password \
Searching: grep
% find /home/borwicjh \
-name ‘*.lis’
[all files matching *.lis]
% find /home/borwicjh \
-mtime -1 –name ‘*.lis’
[*.lis, if modified within 24h]
Searching: locate
% locate .lis
[files with .lis in path]
% locate log
Applied Shell Programming
Make Your Life Easier
TAB completion
Control+R
history
cd -
Study a UNIX Editor
pushd/popd
% cd /tmp
% pushd /var/log
/var/log /tmp
% cd ..
% pwd
/var
% popd
Monitoring processes
ps
ps –ef
ps –u oracle
ps –C sshd
“DOS” Mode Files
#!/usr/bin/bash^M
FTP transfer in ASCII, or
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_
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
% 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
It's Over!
Other Questions?