Programming in Unix

coordinatedcapableSoftware and s/w Development

Nov 4, 2013 (3 years and 9 months ago)

112 views

ITSW 2436/Kenneth R. Frazer
1

Programming in Unix

An Overview of the Significant
Programming Tools in Common Use

ITSW 2436/Kenneth R. Frazer
2

Many Tools Are Available


Many tools are available for programming in Unix


Some of the more common are


Traditional compiled languages


C, C++


Fortran


Pascal


Ada


Assembler (not strictly compiled but…)


Interpreted Languages


Java

ITSW 2436/Kenneth R. Frazer
3




Scripting Languages


Shell (various flavors)


Perl


Python


Tcl/Tk


Expect


Scheme

ITSW 2436/Kenneth R. Frazer
4

Compiled Languages


Most major vendors provide some, if not all, of the
compiled languages


Most are also available from GNU


cc and as are the C compiler and assembler
provided by most Unix vendors


GNU provides:


gcc/g++


gas


F2C


P2C


And also has an Ada front end

ITSW 2436/Kenneth R. Frazer
5




These languages are good for large, production
code developments or system level programming


C is the lingua franca of Unix


The majority of Unix is written in C


A very mature library for system calls is available


Fortran is the language of scientific and numeric
programming


Pascal was a major teaching environment that
has fallen somewhat out of favor


Ada was the language of the defense industry for
a number of years but is also falling out of favor

ITSW 2436/Kenneth R. Frazer
6

Java


Developed by James Gosling of Sun
MicroSystems


Originally designed for "
internet appliances
"


Java is also supported by several vendors
(primarily Sun)


The JDK is freely available from the Web


Borland/Inprise/Corel has also announced Java
support for Unix platforms based on the Sun JDK


Java is an object
-
oriented language that produces
byte codes
, which are then interpreted by a
Java
Virtual Machine

(JVM) that is specific to the
hardware/OS platform it is executing on


ITSW 2436/Kenneth R. Frazer
7




Java was designed for security


Common phrase is "the Java sandbox"


Pointers are not allowed


Byte codes are validated prior to execution


Memory is managed by the JVM using
garbage
collection

to eliminate memory leaks


Although original design was for internet
appliances, Sun has since tried to promote Java
as the "write once, run anywhere" Holy Grail of
portable code

ITSW 2436/Kenneth R. Frazer
8

Perl


Perl was designed by Larry Wall to help automate
reporting tasks common to a system admin


It is also freely available from the Web


"Practical Extraction and Report Language"


Pathologically Eclectic Rubbish Lister


As time went on, Larry added bits and pieces to
the language and finally released the source to
the Web


There it became immensely popular with Sys
Admins and other scriptors.


It was enhanced and ported to other systems

ITSW 2436/Kenneth R. Frazer
9




Perl is now available on most flavors of Unix,
Windows, Mac, Amiga, Atari ST, VMS, OS/2, and
MS
-
DOS


Perl looks like a combination of sed, shell, and
awk


It is extremely useful for scripting sys admin tasks
but can be hard to read


Perl is also one of the most popular languages for
writing CGI scripts


Since one of its properties is "many ways to do
anything" it can also be hard to learn

ITSW 2436/Kenneth R. Frazer
10

Python


Python is another scripting language, written by
Guido van Rossum in 1990


It is a portable, interpreted, object
-
oriented
programming language influenced by C and
Modula
-
3


It has a small core language that includes basic
data types and flow control along with higher level
types such as:


Strings


Lists


Tuples


Associative arrays


This makes it fairly easy to learn

ITSW 2436/Kenneth R. Frazer
11




Object
-
oriented programming is supported with
classes and multiple
-
inheritance


Exception handling is provided for robust
error/fault handling


The real power in Python lies in its extensibility


Python can be extended by writing
modules

in
either Python or in compiled languages such as C
or C++


These modules can define variables, functions,
new data types and their methods, or simply
provide a link to existing code libraries

ITSW 2436/Kenneth R. Frazer
12




The standard Python library includes modules for
debuggers, profilers, Internet services, and
graphical user interfaces


Python is also portable


It is available for:


Unix


Windows 3.x


Windows 95/98/NT


Mac


OS/2

ITSW 2436/Kenneth R. Frazer
13




Python is well suited for:


System administration tasks


Developing graphical user interfaces


Database programming


Integrating other pre
-
existing components


Rapid Application Development


Python is NOT well suited for time critical
components


Python is freely available from the Web and has a
broad support base

ITSW 2436/Kenneth R. Frazer
14

Tcl/Tk


Tool Command Language/Tool Kit


Developed in late '80s by John Ousterhout at UC
Berkeley


Born out of a frustration with building (generally
bad) command languages over and over for
different design tools


Tk evolved about the same way


Driven by desire to build interactive graphical interfaces
without a huge monolithic underpinning


Took component approach


Allows construction if interfaces by assembling
groups of small components

ITSW 2436/Kenneth R. Frazer
15




Tcl is a scripting language designed for providing
command language "front ends" for either existing
tools or new developments


Consists of a library of C function calls


Available for free via the Web and supported by
tens of thousands of developers


Even though it is a library of C function calls, it is
possible to create useable graphical interfaces
consisting only of Tcl script


ITSW 2436/Kenneth R. Frazer
16




Tcl has many advantages


Since it can be interpreted by the Tcl shell
tclsh
, it
provides a rapid development environment


No edit
-
compile
-
execute
-
debug cycle


It also makes it easy for your apps to provide a
powerful scripting language without having to
write a lot of code


It makes an excellent "glue language" for
integrating multiple existing packages into one
seamless application

ITSW 2436/Kenneth R. Frazer
17




Tk is a toolkit for the X window System


It extends the Tcl core language with commands
for building Motif
-
like graphical user interfaces


Like Tcl, Tk is implemented as a library of C
functions


Individual programmers can extend the base Tk
features with new user interface
widgets

written in
C


Tk can also be interpreted by the windowing shell
wish


ITSW 2436/Kenneth R. Frazer
18

Expect


Expect is a Tcl based scripting language for
automating interactive applications such as:


telnet


ftp


passwd


fsck


Expect makes it almost trivial to automate these


Expect is also useful for testing interactive
applications


By using Tk with Expect, you can also wrap
interactive applications with an X Window GUI

ITSW 2436/Kenneth R. Frazer
19




Expect was written by Don Libes in the late '80s
and early '90s


Expect is freely available from the Web


It supports various flavors of Unix and Windows


Expect is primarily useful for those situations
where you "
expect
" something from a program
and then provide a response


ITSW 2436/Kenneth R. Frazer
20

Example Expect Script

# minimal script to read a web page

match_max 100000

set timeout
-
1

spawn telnet $host 80

expect "Escape character is *
\
n"

send "GET $page
\
r
\
n"

expect

puts "$expect_out(buffer)"

ITSW 2436/Kenneth R. Frazer
21

Scheme Shell (scsh)


Scheme shell (not to be confused with Scheme,
the full language) was written by a very strange
gentleman named Olin Shivers in the early '90s


It came about from a flame fest in which Shivers
made the claim it would be easier to do shell
programming from Scheme that to continue using
shell


The serious Unix folks kindly expressed their
doubts so Shivers decided to prove them wrong!


Probably only take a week or two to add some OS
interfaces and a few macros


26 months later, Scheme shell was released

ITSW 2436/Kenneth R. Frazer
22




According to its author, scsh is pronounced
"skishhhh"


scsh is freely available via the Web as open
source


It runs on most Unix platforms without problem



ITSW 2436/Kenneth R. Frazer
23




scsh is a Unix shell that uses the Scheme
language as its scripting language


Provides the following features:


A complete POSIX interface


Very complete support for networking, with high and
low level interfaces


An additional network package containing an HTTP
server and SMTP support is also available


Powerful string manipulation functions:


Pattern matching


Filename manipulations


Awk
-
like macros


Regular expressions

ITSW 2436/Kenneth R. Frazer
24




Additionally, since scsh is based upon a "real"
programming language, it has serious data
structures and control structures


ITSW 2436/Kenneth R. Frazer
25

Comparison of Several Scripts


The following scripts, in sh, Perl, Python, and
scsh, all print a list of all the executables in the
current PATH to stdout

ITSW 2436/Kenneth R. Frazer
26

sh


#!/bin/sh




IFS=':'


for d in $PATH; do


for f in $d/*; do


[
-
x $f
-
a !
-
d $f ] && echo $f


done


done


ITSW 2436/Kenneth R. Frazer
27

Perl

#!/usr/local/bin/perl



foreach $dir (split(/:/, $ENV{'PATH'})) {


opendir(DIR, $dir) or die "can't opendir $dir: $!";


map {
-
x "$dir/$_" && !
-
d "$dir/$_" && print "$_
\
n"
} readdir(DIR);


closedir DIR;


}


ITSW 2436/Kenneth R. Frazer
28

Python


#!/usr/local/bin/python




import os, string, stat


for d in string.split(os.environ['PATH'], ':'):


for f in os.listdir(d):


mode = os.lstat(d + '/' + f)[stat.ST_MODE]


if not stat.S_ISDIR(mode):


print f


ITSW 2436/Kenneth R. Frazer
29

scsh

#!/usr/local/bin/scsh
-
s

!#



(map (lambda (f) (display f) (newline))


(apply append


(map directory
-
files ((infix
-
splitter ":") (getenv "PATH")))))