Chapter 11. Shell Scripts

The mailing list project presented in Chapter 3 demonstrates how PHP can be used for more than just generating web pages. Certainly that is the area where PHP shines the brightest, but PHP really is a capable language flexible enough for tackling most any programming task. In this chapter, I will discuss using PHP to write shell scripts.

The impetus for this project comes from the realization that many applications share the same basic layout. For example, the directory structure and shared code files first discussed in Chapter 1 have appeared in other projects throughout the entire book. Directories such as lib, public_files and sql keep the project organized and shared code such as common.php and db.php provide the project's configuration.

The Rails framework that has gained widespread acceptance in the Ruby programming community and really put Ruby on the map offers more functionality than just setting up a common skeleton for a project, but this is often the first service it performs for the developer. Similarly, Django initially assists a Python programmer to create an application structure. Sure you're using PHP, but there's no reason why you can't "borrow" good ideas just because it's not the same language.

So in this chapter, you will be writing a shell script to help set up a well organized directory layout and copy basic code files into it to start a new application. It will run from the command line and prompt the developer for important values such as the database connection information and use them to fill in the relevant parts of the shared code. Along the way you will code a library useful for writing future shell scripts to assist reading in user input.

To give you a better idea of how the script will be used, here's a sample execution. The script is named startproject and is called with a target path provided as the argument.

$ ./startproject -o /srv/apache/example.com/test

Database host [localhost]:
Database schema [TEST]: WROX_DATABASE
Database user [TESTUSR]: username
Database password: password
Database table prefix []:

The following information will be used to generate the site layout:
-------------------------------------------------------------------
Database host:          localhost
Database schema:        WROX_DATABASE
Database user:          username
Database password:      password
Database table prefix:  WROX_

Is this correct? [yes]:
Congratulations!  Your project layout has been deployed to
/srv/apache/example.com/test.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.213.111