CSCI 553: Networking III Unix Network Programming Spring 2006 PowerPoint PPT Presentation

presentation player overlay
1 / 23
About This Presentation
Transcript and Presenter's Notes

Title: CSCI 553: Networking III Unix Network Programming Spring 2006


1
CSCI 553 Networking III Unix Network
ProgrammingSpring 2006
  • Introduction to Unix Shell Scripts

2
Unix Shells
  • Provide interactive, command line interface
  • I/O redirection
  • pipes
  • filename wildcards (globbing)
  • job control
  • command line editing, typing completion and
    history
  • Provide scripting/glue language ability

3
Unix Shells
4
Bourne Shell
  • First popular Unix shell
  • Proprietary, not available in free distributions
  • bash is a reimplementation

5
Korn Shell (ksh)
  • superset of the bourne shell
  • improves bourne shell job control, command line
    editing and programming features
  • All of which have been incorporated into free
    implementations, and new implementations of csh
    (tcsh) and bourne shell (bash)

6
C Shell (csh)
  • Written after the korn shell
  • Purpose was to provide scripting language that
    adheres more closely to the C language syntax,
    but in a scripting language

7
Bourne Again Shell (bash)
  • Free software reimplementation of bourne shell,
    but really borrows from korn and csh
  • attempt to creat a best of all shells
  • extends the bourne shell (and korn shell) plus
    borrows from c shell
  • Because of availability on Linux, may be
    displacing csh and ksh as most used and well
    known

8
bash
  • More on programming in bash

9
Start-up
  • Bash is a Unix program.
  • When a bash shell starts, it executes commands in
    the file .bashrc in your home directory
  • except when started as a login shell, then it
    runs .bash_profile

10
Variables
  • bash has environment variables, as weve
    discussed, to access

dharter_at_nisl echo PATH /usr/kerberos/bin/us
r/java/j2sdk1.4.2_09/bin/usr/local/bin/bin/usr/
bin/usr/sbin/usr/X11R6/bin/opt/pbs/bin/home/dh
arter/bin/opt/kernel_picker/bin/opt/pvm3/lib/op
t/pvm3/lib/LINUX/opt/pvm3/bin/LINUX/opt/env-swit
cher/bin/opt/c3-4//opt/pbs/lib/xpbs/bin dharter
_at_nisl echo HOME /home/dharter dharter_at_nisl
echo USER dharter
dharter_at_nisl teamname"Pittsburgh
Stealers" dharter_at_nisl echo
teamname Pittsburgh Stealers
11
Exporting Variables
  • In all shells, variables are local to the
    specific shell and, unless otherwise specified,
    are not passed to subshells.
  • You must export a shell variable for its value to
    be still set in a subshell.
  • In bash (and bourne shell) use export

dharter_at_nisl export teamname
12
Command Shortcuts
  • As with C shell and Korn shell, bash allows you
    to define your own commands, using the alias
    built-in

dharter_at_nisl alias dls alh dharter_at_nisl
d total 14M drwx------ 46 dharter dharter
4.0K Feb 7 1541 . drwxr-xr-x 36 root root
4.0K Feb 1 1503 .. -rw-rw-r-- 1 dharter
dharter 24 Feb 1 1735 .aspell.en.prepl -rw-rw-
r-- 1 dharter dharter 167 Feb 1 1735
.aspell.en.pws -rw------- 1 dharter dharter
18K Feb 8 0921 .bash_history -rw-r--r-- 1
dharter dharter 24 Oct 19 2004
.bash_logout -rw-r--r-- 1 dharter dharter 191
Oct 19 2004 .bash_profile -rw------- 1 dharter
dharter 1.9K Oct 24 1828 .bashrc
13
Other bash features
  • If you have not discovered them, you should also
    exlore
  • command history (up arrow is simplest example)
  • command editing (can use emacs or vi style)
  • set o vi tells the shell to use vi style
    command editing
  • Autocompletion (use the tab key to complete
    commands and file names while typing, especially
    useful for long path names)

14
Programming the bash shell
  • arithmetic is a built-in in bash shell (unlike
    some previous shells)

dharter_at_nisl (( res 5 3 )) dharter_at_nisl
echo res 8
15
An example bash program, demonstrating arithmetic
and conditional expressions
dharter_at_nisl labs cat divisor.bash !/bin/bash
declare -i testval20 declare -i count2 while
(( count lt testval )) do (( result
testval count )) if (( result 0 ))
then evenly divisible echo " testval
is evenly divisible by count" fi ((
count )) done dharter_at_nisl labs chmod ux
divisor.bash dharter_at_nisl labs ./divisor.bash
20 is evenly divisible by 2 20 is evenly
divisible by 4 20 is evenly divisible by 5 20
is evenly divisible by 10 20 is evenly divisible
by 20
16
String comparisons
  • You may manipulate strings in bash, using these
    conditional operators

17
File-Oriented Expressions
  • Scripting languages are mainly useful for
    manipulating and processing text files. Usually
    have advanced file-oriented handling expressions.

dharter_at_nisl labs cat owner.bash !/bin/bash
if -O /etc/passwd then echo "you are the
owner of /etc/passwd." else echo "you are NOT
the owner of /etc/passwd." fi dharter_at_nisl
labs ./owner.bash you are NOT the owner of
/etc/passwd.
18
File-oriented Expressions
19
Control Structures
  • Control structures allow programming logic
  • Similar to those in Bourne and Korn shell

20
Control Structures
  • case .. in .. esac

case teamnameindex in Dallas Cowboys)
echo Dallas, TX Denver Broncos) echo
Denver, CO Ney York GiantsNew York
Jets) echo New York, NY ) echo Unknown
location esac
  • if .. then .. elif .. then .. else .. fi

if teamnameindex Minesota Vikings
then cat vikings.txt elif
teamnameindex Chicago Bears
then cat bears.txt else cat nfl.txt fi
21
Control Structures
  • for .. do .. done

for file in .txt do grep hello file
done
  • while/until .. do .. done

while (( count lt testval )) do (( result
testval count )) if (( result 0 ))
then echo testval is evenly divisible by
count fi (( count )) done
22
Functions
  • Syntatically the same as Korn shell functions.

function name list of commands or the
keyword function may be omitted name()
list of commands
23
Function Example
  • dharter_at_corvus cat funcex.bash
  • f () two-parameter function
  • (( returnValue 1 2 ))
  • return returnValue
  • f 3 4
  • result?
  • echo "return value from function was result"
  • dharter_at_corvus ./funcex.bash
  • return value from function was 12
Write a Comment
User Comments (0)
About PowerShow.com