Archive for December, 2007

printERROR “Insufficient Arguments.” return 1 fi DEF_ARG=”" YESNO=”"

Friday, December 21st, 2007

printERROR “Insufficient Arguments.” return 1 fi DEF_ARG=”" YESNO=”" case “$2″ in [yY]|[yY][eE][sS]) DEF_ARG=y ;; [nN]|[nN][oO]) DEF_ARG=n ;; esac while : do printf “$1 (y/n)? ” if [ -n “$DEF_ARG” ] ; then printf “[$DEF_ARG] ” fi read YESNO if [ -z “$YESNO” ] ; then YESNO=”$DEF_ARG” fi case “$YESNO” in [yY]|[yY][eE][sS]) YESNO=y ; break ;; [nN]|[nN][oO]) YESNO=n ; break ;; *) YESNO=”" ;; esac done export YESNO unset DEF_ARG return 0 } Before you look at an example of this function in use, examine how it works. As indicated by the comments, this function can handle up to two arguments. It treats the first argument as the prompt and the second argument as the default answer to the prompt. First, this function checks that at least one argument is given. If no arguments are given, you return from the function with the error message ERROR: Insufficient Arguments. Next, set the variables DEF_ARG and YESNO to null, in order to avoid using the values stored in them from a
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Free web space - echo “ERROR: File $MYFILE was not found.” >&2

Thursday, December 20th, 2007

echo “ERROR: File $MYFILE was not found.” >&2 can be written printERROR “File $MYFILE was not found.” Because the function printERROR always prefixes the message you want to display with the word ERROR and redirects the output to STDERR, you don’t have to worry about forgetting these things when displaying an error message. The third function, printUSAGE, displays a usage message and then exits. It informs the user that the script was invoked incorrectly. This type of message was discussed in depth in Chapter 12 . Asking a Question In interactive shell scripts, you need to obtain input from the user. Sometimes this involves asking simple yes or no questions. In other instances, you need to ask the user a question that requires a more complicated response. For example, many scripts need to retrieve the name of a file on which to operate. In this section, I present two functions that help you to prompt the user and get a response: l promptYESNO l promptRESPONSE Asking a Yes or No Question One of the most common types of questions asked by shell scripts is a yes or no question. For example, this shell script Make backup (y/n)? asks whether you want to make a backup. The function, promptYESNO, provides you with a reusable method of asking a yes or no question and getting a response. The user’s response, y indicating yes or n indicating no, is stored in the variable YESNO after the function completes. ################################################ # Name: promptYESNO # Desc: ask a yes/no question # Args: $1 -> The prompt # $2 -> The default answer (optional) # Vars: YESNO -> set to the users response # y for yes, n for no ################################################ promptYESNO() { if [ $# -lt 1 ] ; then
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Most of (Ftp web hosting) the messages that shell scripts display

Thursday, December 20th, 2007

Most of the messages that shell scripts display do not need special handling, such as prefixing the message with a description or having the output redirected to STDERR. The exceptions are error, warning, and usage messages. These messages require some extra handling. The following functions take care of this handling for you: ################################################ # Name: printERROR # Desc: prints an message to STDERR # Args: $@ -> message to print ################################################ printERROR() { echo “ERROR:” $@ >&2 } ################################################ # Name: printWARNING # Desc: prints an message to STDERR # Args: $@ -> message to print ################################################ printWARNING() { echo “WARNING:” $@ >&2 } ################################################ # Name: printUSAGE # Desc: prints a USAGE message and then exits # Args: $@ -> message to print ################################################ printUSAGE() { echo “USAGE:” $@ exit } All these commands work by calling the echo command and passing it two arguments: l The message prefix, either ERROR, WARNING, or USAGE. l The arguments are specified to these functions by the user. The arguments are stored in $@, as explained in Chapter 12, “Parameters.” The first two functions, printERROR and printWARNING, display error and warning messages. Both messages indicate to a user that something has gone wrong. Thus they redirect the output to STDERR, which is reserved for error reporting. Usually an error indicates the occurrence of something unexpected that is difficult to recover from, such as a command failure. A warning message usually indicates that something unexpected occurred, but the script was able to recover from this. The advantage of using these functions is that they provide a standard output format for errors and warning throughout you script. As an example, the following error message
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Sams Teach Yourself Shell Programming in 24 Hours (Simple web server)

Wednesday, December 19th, 2007

Sams Teach Yourself Shell Programming in 24 Hours Contents Index Hour 21: Problem Solving with Functions Previous Chapter Next Chapter Sections in this Chapter: Creating a Library of Functions Questions Useful Functions Terms Summary Previous Section Next Section Useful Functions Displaying Messages Obtaining the Process ID by Name Asking a Question Getting a User’s Numeric User ID Checking Disk Space The Complete Library Now that I have covered the background knowledge needed to create and use a library of shell functions, look at a shell library that provides you with the capability to perform common scripting tasks easily. A complete listing of the library is available at the end of this chapter. Some of the functions you will look at are l printERROR l printWARNING l printUSAGE l promptYESNO l promptRESPONSE l getSpaceFree l getSpaceUsed l getPID l getUID In addition, you will be asked to develop four additional functions as part of the “Questions” section in this chapter: l toUpper l toLower l isSpaceAvailable l isUserRoot By developing these functions, you can gain experience in working with a library of shell functions. In the following sections, I will first present a brief description of each function or group of functions, followed by the implementation of the functions. At the end of each section is a discussion of the function’s implementation along with caveats regarding its use. Some of these functions need to be modified to work properly on all versions of UNIX. In this chapter, I will note the differences. In Chapter 23, “Scripting for Portability,” I will show you how to modify these functions to account for the differences between different versions of UNIX. Displaying Messages
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

For functions, use the following naming scheme: l (Free php web host)

Tuesday, December 18th, 2007

For functions, use the following naming scheme: l printString for functions that display a message. Here String describes the type of message that is displayed. l promptString for functions that prompt the user for input. Here String is the name of a variable set by the function after reading input from the user. l getString for functions that retrieve some type of data. Here String describes the information that is retrieved. Sams Teach Yourself Shell Programming in 24 Hours Contents Index Hour 21: Problem Solving with Functions Previous Chapter Next Chapter Sections in this Chapter: Creating a Library of Functions Questions Useful Functions Terms Summary Previous Section Next Section Copyright Macmillan Computer Publishing. All rights reserved.
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

3 MSG=”hello” 4 echo_error $MSG Here you are (Web site designers)

Monday, December 17th, 2007

3 MSG=”hello” 4 echo_error $MSG Here you are assuming that the file messages.sh is stored in the directory $HOME/lib/sh. If this directory did not contain messages.sh, an error message similar to the following would be displayed: sh: /home/ranga/lib/sh/messages.sh: No such file or directory In most versions of the shell, the shell script exits at this point without executing any other commands. For this reason, most shell scripts include all their function libraries before executing any commands. Naming Conventions Unlike other languages, there are no widespread naming conventions for shell libraries or shell functions. Many programmers feel that descriptive names are best for both functions and libraries, whereas others feel that some structure such as that found in the C programming language should be used. In reality, both are good ideas. Library Naming For the purposes of this chapter, I assume that the shell functions that are covered are stored in the file $HOME/lib/sh/libTYSP.sh This naming scheme provides double redundancy and can be explained as follows: l The lib in libTYSP.sh indicates that this file is a library. This is similar to the convention used in the C language. l The .sh in libTYSP.sh indicates that this file contains Bourne shell code. l The directory $HOME/lib indicates that this file is a library because it resides in the lib ( lib as in library) directory. l The directory $HOME/lib/sh indicates that this file is a Bourne Shell library because it resides in the sh directory under the lib directory. To use this library in your scripts, you need to include it as follows: . $HOME/lib/sh/libTYSP.sh If you put the library in a different directory, say /usr/local/lib/sh/libTYSP.sh, your scripts need to access it as follows: . /usr/local/lib/sh/libTYSP.sh Function Naming
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Main code consists of all the commands in (Web hosting packages)

Saturday, December 15th, 2007

Main code consists of all the commands in a shell script that are not contained within a function. In the following shell script, lines 1, 2, and 4 are considered main code: 1 #!/bin/sh 2 MSG=”hello” 3 echo_error() { echo “ERROR:” $@ >&2 ; } 4 echo_error $MSG Line 3, which contains a function definition, is not considered main code. In comparison, a library of shell functions does not contain any main code. It contains only functions. For example, the following would be considered a library: #!/bin/sh echo_error() { echo “ERROR:” $@ >&2 ; } echo_warning() { echo “WARNING:” $@ >&2 ; } Notice that this file contains only function definitions. Strictly speaking, nothing is preventing a library from containing main code. The distinction between a script and a library is purely a conceptual one. To make it simpler for maintenance purposes, you should avoid having anything other than function definitions in a library script. Including Functions from a Library To use a set of functions defined in a library, you need to be able to include or require these functions in shell scripts. You can do this by using the . command. Its syntax is as follows: . file Here file is the name of a file that contains shell commands. If the shell functions given in the previous example were stored in a file called messages.sh, the command . messages.sh can be used to include the functions echo_error and echo_warning into a shell script. As an example, you can rewrite the script 1 #!/bin/sh 2 MSG=”hello” 3 echo_error() { echo “ERROR:” $@ >&2 ; } 4 echo_error $MSG to use messages.sh as follows: 1 #!/bin/sh 2 . $HOME/lib/sh/messages.sh
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Sams Teach Yourself Shell Programming in 24 Hours (Web design programs)

Friday, December 14th, 2007

Sams Teach Yourself Shell Programming in 24 Hours Contents Index Hour 21: Problem Solving with Functions Previous Chapter Next Chapter Sections in this Chapter: Creating a Library of Functions Questions Useful Functions Terms Summary Previous Section Next Section Hour 21 Problem Solving with Functions In previous chapters you looked at writing short shell scripts that perform a specific task. In each shell script, you needed to perform a set of common tasks. Some examples of the required tasks are l Displaying , ERROR, WARNING, and USAGE messages l Prompting the user for input In some cases you needed to repeat these tasks, so you used shell functions. You were able to tailor the output of these functions to suit your needs by using arguments. Many of your scripts reused functions developed for other shell scripts. In this chapter, I will present a library of shell functions that you can use in your shell scripts to perform some common UNIX task. By using and improving on these implementations, you can avoid having to reinvent the wheel when faced with a particular problem. Creating a Library of Functions Creating the Library Naming Conventions Including Functions from a Library In previous chapters, when you wrote shell scripts that required the use of a function, you added that function to the shell scripts file. In that model, whenever you wanted to use a function in a script, you had to copy it from a different file. When you have two or three scripts, this is fine, but as the number of scripts you write increases, so do the number of copies of the functions. Say you locate a bug in one of your functions. Imagine how hard it would be to fix every copy of that function if the function is used in ten or more shell scripts. o reduce the complexity involved in maintaining shell functions, it would be ideal to create a central repository of functions that you could access from your shell script. In other programming languages, a central repository of functions is called a library. Creating the Library Creating a library of shell functions is exactly like creating a shell script. The main difference between the two is that a library contains only functions, whereas a script contains both functions and main code.
We recommend high quality webhost to host and run your jsp application: christian web host services.

Sams Teach Yourself Shell (Web hosting top) Programming in 24 Hours

Thursday, December 13th, 2007

Sams Teach Yourself Shell Programming in 24 Hours Contents Index Hour 20: Debugging Previous Chapter Next Chapter Sections in this Chapter: Enabling Debugging Summary Syntax Checking Questions Shell Tracing Previous Section Next Section Questions 1. What are the three main forms of enabling debugging in a shell script? 2. Enhance the Debug() function given in this chapter so that the programmer has to press Enter after deactivating the debugging mode. When you debug scripts that have several dozen functions, this feature enables you to study the debugging output of a particular function in detail before the script proceeds to the next function. Sams Teach Yourself Shell Programming in 24 Hours Contents Index Hour 20: Debugging Previous Chapter Next Chapter Sections in this Chapter: Enabling Debugging Summary Syntax Checking Questions Shell Tracing Previous Section Next Section Copyright Macmillan Computer Publishing. All rights reserved.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Sams Teach Yourself Shell Programming (Web server type) in 24 Hours

Wednesday, December 12th, 2007

Sams Teach Yourself Shell Programming in 24 Hours Contents Index Hour 20: Debugging Previous Chapter Next Chapter Sections in this Chapter: Enabling Debugging Summary Syntax Checking Questions Shell Tracing Previous Section Next Section Summary In the process of developing or maintaining large shell scripts, you need to find and fix bugs that occur in them. In this chapter you looked at the tools provided by the shell to ease the task of debugging shell scripts. Some of the topics you covered are l Enabling debugging l Syntax checking using sh -n and sh -nv l Using shell tracing to find syntax and logic bugs l Embedding debugging hooks in your shell scripts By learning the techniques used in debugging shell scripts, you can fix your own scripts as well as maintain scripts written by other programmers. Sams Teach Yourself Shell Programming in 24 Hours Contents Index Hour 20: Debugging Previous Chapter Next Chapter Sections in this Chapter: Enabling Debugging Summary Syntax Checking Questions Shell Tracing Previous Section Next Section Copyright Macmillan Computer Publishing. All rights reserved.
We recommend high quality webhost to host and run your jsp application: christian web host services.