<<

. 62
( 69 .)



>>

objects. You can define objects and assign attributes to these objects quite easily in
Python. To define a class, you can use the class command, such as class Farm. To
define an instance of this class, you can use a statement like my_farm = Farm ().
You can then define the attributes of your farm with lines such as my_farm.cows.

Example:
#!/usr/bin/python
# simple program using objects and attributes

class Farm:
pass

my_farm = Farm()
my_farm.acres = 45
my_farm.cows = 6
my_farm.crops = 2

print "My farm is ", my_farm.acres, "acres."
print "We have", my_farm.cows, "cows and"
if my_farm.crops:
crop_text = "we grow " my_farm.crops
else:
crop_text = "no"
print "crops."




– Your script runs.
‹ Type out_file.write("Testing, Á Type chmod a+x write2file
WRITE TO A FILE USING PYTHON
and press Return.
testing, 1, 2, three\nThat's all\n")
„ Start Pico to create a file – The system displays the file
and press Return.
‡ Type ./write2file and press
name write2file. your script creates.
› Type out_file.close(). Return.
¤ Type #!/usr/bin/python and
ˇ Save your file, and exit ° Type cat mytest and press
press Return twice, then type
out_file = open("mytest","w") Pico. Return.
and press Return.
– Pico saves your script.
305
UNIX FOR MAC




DEVELOP RUBY APPLICATIONS
command string.gsub!(/this/,"that") replaces this

Y
ou can quickly develop object-oriented applications
with that. If you do not find a built-in type that meets your
in Ruby. Like Python, Ruby is an object-oriented
needs, you can define your own using the def command.
scripting language that installs with the Mac OS X
developer tools. In fact, there is considerable debate about
Because Ruby is interpreted, you can run scripts by using
which of the two is the better language.
them as arguments to the interpreter ” such as ruby
HelloWorld.rb ” or by including the shebang line in
The interpreter is /usr/bin/ruby, and the syntax is relatively
your script and making the script executable. The command
easy to use and, like Perl, extensible. The language is similar
ruby “v tells you what version of Ruby you are using.
to SmallTalk, a programming language that is object-oriented.
For example, in Ruby, you can use the command 3.times
Print commands in Ruby look much like print commands in
{ print "Hello, World!" } to print "Hello, World!"
Perl or C, but do not require the use of a semicolon at the
three times in a row.
end of the line. New lines are represented with \n. While
file extensions are not critical on Unix systems, Ruby scripts
Because the language has many built-in types, you can
are generally given the file extension .rb to indicate that
accomplish a lot with just a few lines of code. Some of
they are Ruby programs.
these types include match and replace commands that are
similar to commands that you use in Perl. For example, the


DEVELOP RUBY APPLICATIONS




‹ Type print "Hello, World!" ˇ Type ruby ./HelloWorld.rb ‡ Type ./HelloWorld.rb and
CREATE HELLO WORLD IN RUBY
and press Return. and press Return. press Return.
„ Start Pico to edit a file
– Your script runs.
› Save your file, and exit Á Type chmod a+x
named HelloWorld.rb.
Pico. HelloWorld.rb and press
¤ Type #!/usr/bin/ruby and Return.
– Pico saves your script.
press Return twice.


306
18
DEVELOP UNIX APPLICATIONS


You can print a string in Ruby with or without a carriage return at the
end, just as you can in Perl and Python. The syntax that each language
uses is different, as shown in the comparison below.

PERL RUBY PYTHON
print "Hello World"; print "Hello World" print "Hello World",
print "Hello World\n"; puts "Hello World" print "Hello World"

You can iterate through a sequence of values in Ruby by defining an
iterate method such as that shown below.
RESULT:
TYPE THIS:
0: Hello World
def iterate(max)
1: Hello World
i=0
2: Hello World
while i < max
3: Hello World
yield i
4: Hello World
i += 1
end
end

iterate(5{|val| puts "#{val}: Hello World"}




‹ Type n * fact(n-1) and ˇ Save your file using the ‡ Type ./factorial 4 and press
DEFINE A METHOD IN RUBY
press Return, then type end name factorial, and exit the Return.
„ In a text editor, type def and press Return, then type text editor.
– Your program prints the
fact(n) and press Return.
end again, and press Return.
Á Type ruby followed by a factorial of 4.
¤ Type if n == 0 and press
› Type print space.
Return, then type 1 and press
fact(ARGV[0].to_i), "\n".
Return, then type else and
enter a space.
307
UNIX FOR MAC



RUN A DATABASE ON MAC OS X
Installing the PostgreSQL software does not automatically

Y
ou can install and run a database on your Mac OS X
enable you to start issuing SQL commands. You must follow
system. In fact, you can choose between two free
a series of steps to create your database.
relational database packages ” MySQL and
PostgreSQL.
The first thing you must do is to add a special database user
to your system. For PostgreSQL, this user is usually called
When you install PostgreSQL, you first download a gzipped
postgres. Your database software runs under this account.
TAR file and go through the steps of unzipping and
untarring this file. You must read the INSTALL file that
The next thing you must do is to log onto the postgres
comes with the installation files before you run the
account and enter the initdb command to create a
configure command, as it contains many options that you
database. You must not use this command as root or with
can use to modify your installation. These options include
the sudo command.
the location where you want to install the software and
whether you want to include readline support if you have If you install into the default location, your binaries ”
already installed readline. You can then run the configure including initdb ” go into this /usr/local/pgsql/bin directory.
command with or without options, followed by the make You must therefore add this directory to your path so that
command. The make install command installs the you can easily invoke any of the PostgreSQL commands.
software on your system. By default, the software installs in
the directory/usr/local/pgsql. After you run initdb from the postgres account, you can
create tables, add data, and enter queries.

RUN A DATABASE ON MAC OS X




– Your software configures
„ Type gunzip followed by ¤ Type tar xf followed by › Type ./configure if you
the name of your postgres the name of your TAR file, have installed, or ./configure for compilation.
install file, and press Return. and press Return. --without-readline if you have
not, and press Return.
– Your install file – Your TAR file is extracted.
uncompresses.
‹ Type cd followed by the
name of your install directory,
and press Return.
308
18
DEVELOP UNIX APPLICATIONS




To add the man pages for your new
To allow other users to use your new database
database software to your search list,
software, you must enable them from the postgres
append /usr/local/pgsql/man to your
account. Otherwise, the users will get an error of this
MANPATH environment variable. This path
type: psql: FATAL: user "kbartlett"
may contain a number of directories
does not exist. To enable a new user, use the
separated by colons. The following
su command to access the postgres account by typing
example adds all the man directories
su - postgres. Then connect to the database using the
shown to your search path for man pages.
psql testdb command. After you establish the
connection, add the user with a create user
Example:
username; command, where you replace username
setenv MANPATH /sw/share/man:/usr/local/
with the username of the user you want to enable.
man:/usr/share/man:/usr/local/pgsql/man
After connecting to PostgreSQL to create tables or
run queries, you need to disconnect from the
database. To do this, type \q. This command returns
you to your normal system prompt.




– The software begins to – Your software compiles
ˇ Type make and press Á Type sudo make install and
Return. compile. press Return. and installs.




CONTINUED

309
UNIX FOR MAC


RUN A DATABASE ON MAC OS X (CONTINUED)
The basic query command is select. When you issue a query

W
hile it might not be immediately obvious, a
that selects phone from contacts, phone is a column in a
database is a form of server “ a process that
table named contacts. If you select phone from contacts
supplies information to clients. After a database is
where areacode is equal to 415, you are listing your
created with createdb, initiated with initdb, and
contacts in San Francisco. In this case, you are asking to
populated with tables and data, the database is ready to
view one particular column from particular rows in your
start answering queries. Most databases run all the time
table of contacts.
whether or not anyone is using them.

<<

. 62
( 69 .)



>>