Archive for August, 2007

get_return() { echo -e Press return c read (Web site templates)

Friday, August 24th, 2007

get_return() { echo -e Press return c read xreturn 0} get_confirm() { echo -e Are you sure? c while truedoread xcase $x iny | yes | Y | Yes | YES ) return 0;; n | no | N | No | NO ) echo echo Cancelled return 1;; *) echo Please enter yes or no ;; esacdone} 4.Here we come to the main menu function, set_menu_choice. The contents of the menu varydynamically, with extra options being added if a CD entry has been selected. set_menu_choice() { clearecho Options :- echoecho a) Add new CD echo f) Find CD echo c) Count the CDs and tracks in the catalog if [ $cdcatnum != ]; thenecho l) List tracks on $cdtitle echo r) Remove $cdtitle echo u) Update track information for $cdtitle fiecho q) Quit echoecho -e Please enter choice then press return c read menu_choicereturn} 5.Here are two more very short functions, insert_titleand insert_track, for adding tothe database files. Though some people hate one-liners like these, they help make other func- tions clearer. Note that echo -emay not be portable to some shells. 84Chapter

In the following Try It Out section, just (Web design course)

Friday, August 24th, 2007

In the following Try It Out section, just so you don t get totally lost, we ll be using the following functions: get_return() get_confirm() set_menu_choice() insert_title() insert_track() add_record_tracks() add_records() find_cd() update_cd() count_cds() remove_records() list_tracks() Try It Out A CD Application1.First in our sample script is, as always, a line ensuring that it s executed as a shell script, fol- lowed by some copyright information: #!/bin/sh# Very simple example shell script for managing a CD collection. # Copyright (C) 1996-2003 Wrox Press. # This program is free software; you can redistribute it and/or modify it# under the terms of the GNU General Public License as published by the# Free Software Foundation; either version 2 of the License, or (at your# option) any later version. # This program is distributed in the hopes that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General# Public License for more details. # You should have received a copy of the GNU General Public License along# with this program; if not, write to the Free Software Foundation, Inc. # 675 Mass Ave, Cambridge, MA 02139, USA. 2.The first thing to do is to ensure that some global variables that we ll be using throughout are set up. We set the title and track files and a temporary file. We also trap Ctrl+C,so temporary file will be removed if the user interrupts the script. menu_choice= current_cd= title_file= title.cdb tracks_file= tracks.cdb temp_file=/tmp/cdb.$$ trap rm -f $temp_file EXIT3.Now we define our functions, so that the script, executing from the top line, can find all thefunction definitions before we attempt to call any of them for the first time. To avoid rewritingthe same code in several places, the first two functions are simple utilities.

Web hosting plans - The next decision is what to put in

Thursday, August 23rd, 2007

The next decision is what to put in the files. Initially, for each CD title, we ll choose to store .The CD catalog number .The title .The type (classical, rock, pop, jazz, etc.) .The composer or artistFor the tracks, we ll store simply .Track number .Track nameIn order to join the two files, we must relate the track information to the rest of the CD information. To dothis, we ll use the CD catalog number. Since this is unique for each CD, it will appear only once in the titlesfile and once per track in the tracks file. Let s look at an example titles file: CatalogTitleTypeComposerCD123Cool saxJazzBixCD234Classic violinClassicalBachCD345Hits99PopVariousIts corresponding tracks file will look like this: CatalogTrack No.TitleCD1231Some jazzCD1232More jazzCD3451DizzyCD2341Sonata in D minorThe two files join using the Catalog field. Remember, there are normally multiple rows in the tracks filefor a single entry in the titles file. The last thing we need to decide is how to separate the entries. Fixed-width fields are normal in a rela- tional database, but are not always the most convenient. Another common method is a comma, whichwe ll use here (i.e., a comma-separated variable, or CSV, file). 82Chapter

Throughout this book, we re going to be building (Web hosting solutions)

Wednesday, August 22nd, 2007

Throughout this book, we re going to be building a CD database application to show the techniqueswe ve been learning. We start with a shell script, but pretty soon we ll do it again in C, add a database, and so on. Let s start. RequirementsSuppose we have an extensive CD collection. To make our lives easier, we re going to design and imple- ment a program for managing CDs. An electronic catalogue seems an ideal project to implement as about programming Linux. We want, at least initially, to store some basic information about each CD, such as the label, type ofmusic, and artist or composer. We would also like to store some simple track information. We want to be able to search on any of the per CD items, but not on any of the track details. To make the miniapplication complete, we would also like to be able to enter, update, and delete any information from within the application. DesignThe three requirements updating, searching, and displaying the data suggest that a simple menu adequate. All the data we need to store is textual and, assuming our CD collection isn t too big, no need for a complex database, so some simple text files will do. Storing information in text keep our application simple, and if our requirements change, it s almost always easier to manipu- late a text file than any other sort of file. As a last resort, we could even use an editor to manually enterand delete data, rather than write a program to do it. We need to make an important design decision about our data storage: Will a single file suffice and, what format should it have? Most of the information we expect to store occurs only once per CD (we llskip lightly over the fact that some CDs contain the work of many composers or artists), except trackinformation. Just about all CDs have more than one track. Should we fix a limit on the number of tracks we can store per CD? That seems rather an arbitrary restriction, so let s reject that idea right away! If we allow a flexible number of tracks, we have three options: .Use a single file, with one line for the title type information and then nlines for the trackinformation for that CD. .Put all the information for each CD on a single line, allowing the line to continue until no moretrack information needs to be stored. .Separate the title information from the track information and use a different file for each. Only the third option allows us to easily fix the format of the files, which we ll need to do if we everwish to convert our database into a relational form (more on this in Chapter 7), so that s the option

Web design programs - Here we show the menu item with four

Tuesday, August 21st, 2007

Here we show the menu item with four different options. Again we redirect the standard error streamand load it into a variable. if [ $Q_MUSIC == 1 ]; thendialog –msgbox Good choice! 12 25fiIf the user selects menu option 1, this will be stored in the temporary file _1.txt, which we havegrabbed in to the variable Q_MUSICso that we can test the result. sleep 5dialog –clearexit 0Finally, we clear the last dialog box and exit the program. Figure 2-5 shows is what it looks like onscreen. Figure 2-5Now you have a way, providing you need to use only the Linux console, of writing simple GUI pro- grams from a shell script. Putting It All TogetherNow that we ve seen the main features of the shell as a programming language, it s time to write anexample program to put some of what we have learned to use. 80Chapter

Web hosting colocation - The options look a little tricky, but all

Monday, August 20th, 2007

The options look a little tricky, but all you have to remember is that each menu item has three values: .Bullet number .Text .StatusSo the first item has a number of 1, and text display of one and is set to off . We then start on menu item, which is 2, two and selected. This continues until you run out of menu items. Easy, wasn t it? Just try some out on the command line and see how easy it is to use. In order to put this together in a program, we need to be able to access the results of the user input. quite easy; we simply redirect the standard error stream for text input, or check the environment able $?, which you will recall is the exit status of the previous command. Try It OutLet s look at a simple program called questions, which takes note of user responses. #!/bin/sh# Ask some questions and collect the answerdialog –title Questionnaire –msgbox Welcome to my simple survey 9 18We start off by displaying a simple dialog box to tell the user what is happening. You don t need to result or obtain any user input, so this is nice and simple. dialog –title Confirm –yesno Are you willing to take part? 9 18if [ $? != 0 ]; thendialog –infobox Thank you anyway 5 20sleep 2dialog –clearexit 0fiNow we ask the user if he or she wants to proceed, using a simple yes/no dialog box. We use the environ- ment variable $?to check if the user selected yes (result code 0) or not. If they didn t want to proceed, a simple infobox that requires no user input before exiting. dialog –title Questionnaire –inputbox Please enter your name 9 30 2>_1.txtQ_NAME=$(cat _1.txt) We now ask the user his name, using an input box. We redirect the standard error stream, 2, into a tempo- rary file, _1.txt, which we can then process into the variable QNAME. dialog –menu $Q_NAME, what music do you like best? 15 30 4 1 Classical 2 Jazz 3 Country 4 Other 2>_1.txtQ_MUSIC=$(cat _1.txt)

In addition, all the dialogtypes take several options. (Web design careers)

Monday, August 20th, 2007

In addition, all the dialogtypes take several options. We will not list them all here, except to mentiontwo: –title, which allows you to specify a title for the box, and –clear, which is used on its own forclearing the screen. As ever, check the manual page for the full list of options. Try It Out Using the dialog UtilityLet s leap straight in with a nice complex example. Once you understand this example, all the others willbe easy! This example will create a checklist-type box, with a title Check meand the instructions PickNumbers. The checklist box will be 15 characters high by 25 characters wide, and each option willoccupy 3 characters of height. Last, but not least, we list the options to be displayed, along with a defaulton/off selection. dialog –title Check me –checklist Pick Numbers 15 25 3 1 one off 2 two on 3 three off Figure 2-4 shows the result onscreen. Figure 2-4How It WorksIn this example, the –checklistparameter tells us we are to create a checklist-type dialog. We use the–titleoption to set the title to Check me, and the next parameter is the prompt message of PickNumbers. We then move on to set the size of the dialog. It will be 15 lines high by 25 characters wide, and 3 lineswill be used for the menu. It s not a perfect sizing, but it does allow you to see how things are laid out. 78Chapter

The principal types of dialogs we can create (Web design)

Sunday, August 19th, 2007

The principal types of dialogs we can create are in the following table: TypeOption Used to MeaningCreate TypeCheck boxes–checklistAllows you to display a list of items, each of whichmay be individually selected. Info boxes–infoboxAsimple display in a box that returns immediately, without clearing the screen. Input boxes–inputboxAllows the user to type in text. Menu boxes–menuAllow the user to pick a single item from a list. Message boxes–msgboxDisplays a message to the user with an OK buttonwhen they wish to continue. Radio selection boxes–radiolistAllows the user to select an option from a list. Text boxes–textboxAllows you to display a file in a scrolling box. Yes/No boxes–yesnoAllows you to ask a question, to which the usercan select either yesor no. There are some additional dialogbox types available (for example, a gauge and a password-entry If you want to know more about the more unusual dialogtypes, details can be found, as ever, in theonline manual pages. To get the output of any type of box that allows textual input, or selections, you have to capture the dard error stream, usually by directing it to a temporary file, which you can then process afterward. the result of Yes/No type questions, just look at the exit code, which, like all well-behaved programs, returns 0 for success (i.e., a yes section) or 1 for failure. All dialogtypes have various additional parameters to control, such as the size and shape of the dialogpresented. We will list the different parameters required for each type first, and then we will demonstratesome of them on the command line. Finally, we ll write a simple program to combine several dialogs single program. Dialog TypeParameters–checklisttext height width list-height [tag text status] … –infoboxtext height width–inputboxtext height width [initial string] –menutext height width menu-height [tag item ] … –msgboxtext height width–radiolisttext height width list-height [tag text status] … –textboxfilename height width–yesnotext height width77ShellProgrammingb544977

Going Graphical The Dialog UtilityBefore we finish discussing shell (My web site)

Saturday, August 18th, 2007

Going Graphical The Dialog UtilityBefore we finish discussing shell scripts, there is one more feature, which, although it is not strictly partof the shell, is generally useful only from shell programs, so we will cover it here. If you know that your script will only ever need to run on the Linux console, there is a rather neat wayto brighten up your scripts using a utility command called dialog. This command uses text modegraphics and color, but it still looks pleasantly graphically oriented. The whole idea of dialogis beautifully simple a single program with a variety of parameters andoptions that allows you to display various types of graphical boxes, ranging from simple Yes/No boxesto input boxes and even menu selections. The utility generally returns when the user has made somesort of input, and the result can be found either from the exit status or if text was entered by retrievingthe standard error stream. Before we move on to more detail, let s look at a very simple use of dialogin operation. We can usedialogdirectly from the command line, which is great for prototyping, so let s create a simple messagebox to display the traditional first program: dialog –msgbox Hello World 9 18On the screen appears a graphical information box, complete with OK dialog (see Figure 2-3). Figure 2-3Now that we have seen how easy dialogis, let s look at the possibilities in more detail. 76Chapter

Web design company - Debugging ScriptsDebugging shell scripts is usually quite easy,

Friday, August 17th, 2007

Debugging ScriptsDebugging shell scripts is usually quite easy, but there are no specific tools to help. In this section we llquickly summarize the common methods. When an error occurs, the shell will normally print out the line number of the line containing the error. error isn t immediately apparent, we can add some extra echostatements to display the contents and test code fragments by simply typing them into the shell interactively. Since scripts are interpreted, there s no compilation overhead in modifying and retrying a script. Themain way to trace more complicated errors is to set various shell options. To do this, you can either line options after invoking the shell or use the setcommand. We summarize the options following table. Command Line OptionsetOptionDescriptionsh -n