Title: ITSK 1601 Introduction to UNIX Chapter 11 Shell programming
1ITSK 1601 Introduction to UNIXChapter 11Shell
programming
2Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
- Shell Programming
- Similar to DOS batch files
- Quick and simple programming
- Text file interpreted by shell, effectively new
command - List of shell commands to be run sequentially
- Execute permissions, no special extension
necessary
3Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
- Magic First Line Starts With !
- Include full path to interpreter (shell)
- !/bin/sh
- !/bin/csh -f
4Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
- Special Variables (sh)
- Number of arguments on command line
- 0 Name that script was called as
- 1 9 Command line arguments
- _at_ All arguments (separately quoted)
- All arguments
- ? Numeric result code of previous command
- Process ID of this running script
5Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Interacting With the User echo output text
Talk to user (or ask questions) read
variable Get input from user, put it in
variable
6Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
- Decisions
- test and
- if . . . then
- . . .
- fi
-
- case variable in . . . esac
-
- for variable in . . .
- do . . . done
7Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Loops For Scripts For_script1 !/bin/bash for
i in seq 1 10 do echo i done For_script2
!/bin/sh for i in hello 1 2 goodbye do echo
"Looping ... i is set to i" done
8Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Loops while_script1 !/bin/bash COUNTER0
while COUNTER lt 10 do echo The
counter is COUNTER let COUNTERCOUNTER1 done
9Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Loops while_script2 !/bin/sh while read f do
case f in hello) echo English
howdy) echo American gday) echo Australian
bonjour) echo French "guten tag") echo
German ) echo Unknown Language f
esac done
10Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Loops The Until Script !/bin/bash COUNTER20
until COUNTER lt 10 do echo
COUNTER COUNTER let COUNTER1 done
11Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Functions Sample !/bin/bash function quit
exit function hello echo Hello!
hello quit
12Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Functions With Parameters Sample !/bin/bash
function quit exit function e echo 1
e Hello e World quit echo foo
13Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Exit Code Sample (Page1) !/bin/sh adduser()
USER1 PASSWD1 shift shift
COMMENTS_at_ useradd -c "COMMENTS" USER
if "?" -ne "0" then echo "Useradd
failed" return 1 fi passwd USER PASSWD
if "?" -ne "0 then echo "Setting
password failed" return 2 fi echo "Added
user USER (COMMENTS) with password PASSWORD"
14Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Exit Code Sample (Page 2) Main script starts
here adduser bob letmein Bob Holness, the famous
BlockBusters presenter! if "? -eq "1
then echo "Something went wrong with
useradd" else if "?" -eq "2" then
echo "Something went wrong with passwd" else
echo "Bob Holness added to the system." fi
This script checks the two external calls it
makes (useradd and passwd), and lets the user
know if they fail. The function then defines a
return code of 1 to indicate any problem with
useradd, and 2 to indicate any problem with
passwd. That way, the calling script knows where
the problem lay.
15Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
Sample Script AVGSIZE Write a script called
"avgsize" which takes one argument, the name of a
directory, and calculates the average size of all
visible files in the directory. Don't use any ls
options other than l. Treat subdirectories the
same as ordinary files. NOTE A typical output
of the "ls -l" command is total 630
-rw------- 1 jbloe users 66 Mar 20
1146 a11 -rwxr--r-- 1 jbloe users
121 Mar 20 2041 a13 -rwxr--r-- 1 jbloe
users 553 Apr 1 1140 add -rw-r--r--
1 jbloe users 1417 Apr 1 0916
test.991 -rw-r--r-- 1 jbloe users
3331 Apr 1 1144 exam.991 -rw-r--r-- 1
jbloe users 16384 Apr 4 1949 exam1.991
-rw-r--r-- 1 jbloe users 636 Mar
22 1232 hold -rw-r--r-- 1 jbloe users
1878 Mar 16 2021 index.html -rw-r--r--
1 jbloe users 36610 Mar 16 2031
inflate.c -rw-r--r-- 1 jbloe users
17219 Mar 25 0923 inflate1.c -rwxr--r-- 1
jbloe users 195 Apr 1 1032 trash
16Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
AVGSIZE Script Solution Write a script called
"avgsize" which takes one argument, the name of a
directory, and calculates the average size of all
visible files in the directory. Don't use any ls
options other than -l. Treat subdirectories the
same as ordinary files. Solution ls -l "1"
awk 'size5 END print size/(NR-1)'
17Course ITSK 1601 Introduction to
UNIXChapter 11 Shell Programming
The End Instructor Thelma Jones Room
TEC-105 Phone (770) 961-3636 Email
thelmajones_at_mail.clayton.edu Website
http//newcollege.clayton.edu/tjones/