UNIX Shell-Scripting Basics
Kanbay Incorporated - All Rights Reserved
Agenda
What is a shell? A shell script?
Introduction to bash
Running Commands
Applied Shell Programming
| Kanbay Incorporated. All Rights
What is a shell?
| Kanbay Incorporated. All Rights
What is a shell?
/bin/bash
| Kanbay Incorporated. All Rights
What is a shell?
#!/bin/bash
| Kanbay Incorporated. All Rights
What is a shell?
INPUT
shell
OUTPUT
| Kanbay Incorporated. All Rights
ERROR
What is a shell?
Any Program
But there are a few popular shells
| Kanbay Incorporated. All Rights
Other Common Shells
C Shell (/bin/csh)
Turbo C Shell (/bin/tcsh)
Korn Shell (/bin/ksh)
| Kanbay Incorporated. All Rights
An aside: What do I mean by /bin ?
C Shell (/bin/csh)
Turbo C Shell (/bin/tcsh)
Korn Shell (/bin/ksh)
| Kanbay Incorporated. All Rights
An aside: What do I mean by /bin ?
/bin, /usr/bin, /usr/local/bin
/sbin, /usr/sbin, /usr/local/sbin
/tmp
/dev
/home/borwicjh
| Kanbay Incorporated. All Rights
What is a Shell Script?
A Text File
With Instructions
Executable
| Kanbay Incorporated. All Rights
What is a Shell Script?
% cat > [Link] <<MY_PROGRAM
#!/bin/sh
echo Hello, world
MY_PROGRAM
% chmod +x [Link]
% ./[Link]
Hello, world
| Kanbay Incorporated. All Rights
What is a Shell Script? A Text File
% cat > [Link] <<MY_PROGRAM
#!/bin/sh
echo Hello, world
MY_PROGRAM
% chmod +x [Link]
% ./[Link]
Hello, world
| Kanbay Incorporated. All Rights
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
| Kanbay Incorporated. All Rights
INPUT 0
env
OUTPUT 1
ERROR 2
What is a Shell Script? How To Run
% cat > [Link] <<MY_PROGRAM
#!/bin/sh
echo Hello, world
MY_PROGRAM
% chmod +x [Link]
% ./[Link]
Hello, world
| Kanbay Incorporated. All Rights
What is a Shell Script? What To Do
% cat > [Link] <<MY_PROGRAM
#!/bin/sh
echo Hello, world
MY_PROGRAM
% chmod +x [Link]
% ./[Link]
Hello, world
| Kanbay Incorporated. All Rights
What is a Shell Script? Executable
% cat > [Link] <<MY_PROGRAM
#!/bin/sh
echo Hello, world
MY_PROGRAM
% chmod +x [Link]
% ./[Link]
Hello, world
| Kanbay Incorporated. All Rights
What is a Shell Script? Running it
% cat > [Link] <<MY_PROGRAM
#!/bin/sh
echo Hello, world
MY_PROGRAM
% chmod +x [Link]
% ./[Link]
Hello, world
| Kanbay Incorporated. All Rights
Finding the program: PATH
% ./[Link]
echo vs. /usr/bin/echo
% echo $PATH
/bin:/usr/bin:/usr/local/bin:
/home/borwicjh/bin
% which echo
/usr/bin/echo
| Kanbay Incorporated. All Rights
An aside: Quoting
% echo $USER
$USER
% echo $USER
borwicjh
% echo \
% echo deacnet\\sct
deacnet\sct
% echo \
\
| Kanbay Incorporated. All Rights
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]
| Kanbay Incorporated. All Rights
Welcome to Shell Scripting!
| Kanbay Incorporated. All Rights
Bash Shell
Kanbay Incorporated - All Rights Reserved
How to Learn
man
man bash
man cat
man man
man k
man k manual
Learning the Bash Shell, 2nd Ed.
Bash Reference Cards
[Link]
| Kanbay Incorporated. All Rights
Continuing Lines: \
% echo This \
Is \
A \
Very \
Long \
Command Line
This Is A Very Long Command Line
%
| Kanbay Incorporated. All Rights
Exit Status
$?
0 is True
% ls /does/not/exist
% echo $?
1
% echo $?
0
| Kanbay Incorporated. All Rights
Exit Status: exit
% cat > [Link] <<_TEST_
exit 3
_TEST_
% chmod +x [Link]
% ./[Link]
% echo $?
3
| Kanbay Incorporated. All Rights
Logic: test
% test 1 -lt 10
% echo $?
0
% test 1 == 10
% echo $?
1
| Kanbay Incorporated. All Rights
Logic: test
test
[ ]
[ 1 lt 10 ]
[[ ]]
[[ this string =~ this ]]
(( ))
(( 1 < 10 ))
| Kanbay Incorporated. All Rights
Logic: test
[ -f /etc/passwd ]
[ ! f /etc/passwd ]
[ -f /etc/passwd a f /etc/shadow ]
[ -f /etc/passwd o f /etc/shadow ]
| Kanbay Incorporated. All Rights
An aside: $(( )) for Math
% echo $(( 1 + 2 ))
3
% echo $(( 2 * 3 ))
6
% echo $(( 1 / 3 ))
0
| Kanbay Incorporated. All Rights
Logic: if
if something
then
:
# elif a contraction of else if:
elif something-else
then
:
else
then
:
fi
| Kanbay Incorporated. All Rights
Logic: if
if [ $USER eq borwicjh ]
then
:
# elif a contraction of else if:
elif ls /etc/oratab
then
:
else
then
:
fi
| Kanbay Incorporated. All Rights
Logic: if
# see if a file exists
if [ -e /etc/passwd ]
then
echo /etc/passwd exists
else
echo /etc/passwd not found!
fi
| Kanbay Incorporated. All Rights
Logic: for
for i in 1 2 3
do
echo $i
done
| Kanbay Incorporated. All Rights
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
| Kanbay Incorporated. All Rights
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
| Kanbay Incorporated. All Rights
Logic: for
for i in /*
do
echo Listing $i:
ls -l $i
read
done
| Kanbay Incorporated. All Rights
Logic: C-style for
for (( expr1
expr2
expr3
))
do
list
done
| Kanbay Incorporated. All Rights
Logic: C-style for
LIMIT=10
for (( a=1
a<=LIMIT ;
a++
))
do
echo n $a
done
| Kanbay Incorporated. All Rights
Logic: while
while something
do
:
done
| Kanbay Incorporated. All Rights
Logic: while
a=0; LIMIT=10
while [ "$a" -lt "$LIMIT" ]
do
echo -n "$a
a=$(( a + 1 ))
done
| Kanbay Incorporated. All Rights
Reusing Code: Sourcing
% cat > /path/to/my/passwords <<_PW_
FTP_USER=sct
_PW_
% echo $FTP_USER
% . /path/to/my/passwords
% echo $FTP_USER
sct
%
| Kanbay Incorporated. All Rights
Running Programs
Kanbay Incorporated - All Rights Reserved
Reasons for Running Programs
Check Return Code
$?
Get Job Output
OUTPUT=`echo Hello`
OUTPUT=$(echo Hello)
Send Output Somewhere
Redirection: <, >
Pipes
| Kanbay Incorporated. All Rights
Pipes
Lots of Little Tools
INPUT 0
echo Hello | \
wc -c
echo
OUTPUT 1
ERROR 2
A Pipe!
INPUT 0
wc
OUTPUT 1
| Kanbay Incorporated. All Rights
ERROR 2
Email Notification
% echo Message | \
mail s Heres your message \
borwicjh@[Link]
| Kanbay Incorporated. All Rights
Dates
% DATESTRING=`date +%Y%m%d`
% echo $DATESTRING
20060125
% man date
| Kanbay Incorporated. All Rights
FTP the Hard Way
ftp n u [Link] <<_FTP_
user username password
put FILE
_FTP_
| Kanbay Incorporated. All Rights
FTP with wget
wget \
[Link]
wget r \
[Link]
| Kanbay Incorporated. All Rights
FTP with curl
curl T upload-file \
-u username:password \
[Link]
| Kanbay Incorporated. All Rights
Searching: grep
% grep rayra /etc/passwd
% grep r rayra /etc
% grep r RAYRA /etc
% grep ri RAYRA /etc
% grep rli rayra /etc
| Kanbay Incorporated. All Rights
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
| Kanbay Incorporated. All Rights
Searching: locate
% locate .lis
[files with .lis in path]
% locate log
[also finds /var/log/messages]
| Kanbay Incorporated. All Rights
Applied Shell Programming
Kanbay Incorporated - All Rights Reserved
Make Your Life Easier
TAB completion
Control+R
history
cd Study a UNIX Editor
| Kanbay Incorporated. All Rights
Monitoring processes
ps
ps ef
ps u oracle
man ps
| Kanbay Incorporated. All Rights
DOS Mode Files
#!/usr/bin/bash^M
FTP transfer in ASCII, or
dos2unix infile > outfile
| Kanbay Incorporated. All Rights
Passing Arguments
% cat > [Link] <<_TEST_
echo Your name is \$1 \$2
_TEST_
% chmod +x [Link]
% ./[Link] John Borwick ignore-this
Your name is John Borwick
| Kanbay Incorporated. All Rights
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
| Kanbay Incorporated. All Rights
Scheduling Jobs
% crontab -l
0 0 * * * [Link]
0 * * * * [Link]
* * * * * [Link]
0 1 * * 0 [Link]
% EDITOR=vi crontab e
% man 5 crontab
| Kanbay Incorporated. All Rights
| Kanbay Incorporated. All Rights
Other Questions?
Shells and Shell Scripts
bash
Running Commands
bash and Banner in Practice
| Kanbay Incorporated. All Rights