Archive for April, 2007

Web hosting control panel - Notice that we have not used any filename

Saturday, April 28th, 2007

Notice that we have not used any filename extension, or suffix, on this example; Linux, and UNIX ingeneral, rarely makes use of the filename extension to determine the type of a file. We could have used.sh or added a different extension if we wished, but the shell doesn t care. Most preinstalled scripts willnot have any filename extension, and the best way to check if they are scripts or not is to use the filecommand, for example, file firstor file /bin/bash. Use whatever convention is applicable whereyou work, or suits you. Making a Script ExecutableNow that we have our script file, we can run it in two ways. The simpler way is to invoke the shell withthe name of the script file as a parameter, thus: $ /bin/sh firstThis should work, but it would be much better if we could simply invoke the script by typing its name, giving it the respectability of other Linux commands. We do this by changing the file mode to make the file executable for all users using the chmodcommand: $ chmod +x firstWe can then execute it using the command$ firstYou may get an error saying the command wasn t found. This is almost certainly because the shell envi- ronment variable PATHisn t set to look in the current directory for commands to execute. To change this, either type PATH=$PATH:.on the command line or edit your .bash_profilefile to add this commandto the end of the file; then log out and log back in again. Alternatively, type ./firstin the directory con- taining the script, to give the shell the full relative path to the file. Specifying the path prepended with ./does have one other advantage: It ensures that you don t acci- dentally execute another command on the system with the same name as your script file. You shouldn t change the PATHvariable like this for the superuser. It s a securityloophole, because the system administrator logged in as root can be tricked intoinvoking a fake version of a standard command. One of the authors admits to doingthis once, just to prove a point to the system administrator about security, of course! It s a slight risk on ordinary accounts to include the current directory in the path, soif you are particularly concerned, just get into the habit of prepending ./to all com- mands that are in the local directory. Of course, this isn t the only way to use chmodto make a file executable. Use manchmodto find out more about octal arguments and other options. 26Chapter
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

allowing us to glue several existing commands together (Web server logs)

Friday, April 27th, 2007

allowing us to glue several existing commands together in new and powerful ways. We will see wild- card expansion used many times in the following scripts and will look at the whole area of expansion detail when we look at regular expressions in the section on the grepcommand. Going through this long rigmarole every time we want to execute a sequence of commands is a bore. to store the commands in a file, conventionally referred to as a shell script, so we can execute themwhenever we like. Creating a ScriptFirst, using any text editor you need to create a file containing the commands, create a file called firstthat looks like this: #!/bin/sh# first# This file looks through all the files in the current# directory for the string POSIX, and then prints the names of# those files to the standard output. for file in * doif grep -q POSIX $file thenecho $file fidoneexit 0Comments start with a #and continue to the end of a line. Conventionally, though, #is usually kept first column. Having made such a sweeping statement, we next note that the first line, #!/bin/special form of comment; the #!characters tell the system that the argument that follows on the line program to be used to execute this file. In this case, /bin/shis the default shell program. Note the absolute path specified in the comment. It may be wise to keep this shorter than 32 characters, for backwards compatibility, because some older UNIX versions can only use this limited number when using #!, although Linux generally does not have this limitation. Since the script is essentially treated as standard input to the shell (something prepared earlier), it cancontain any Linux commands referenced by your PATHenvironment variable. The exitcommand ensures that the script returns a sensible exit code (more on this later in the chapter). This is rarely checked when programs are run interactively, but if you want to invoke this script fromanother script and check whether it succeeded, returning an appropriate exit code is very important. Even if you never intend to allow your script to be invoked from another, you should still exit with code. Go on and have faith in the usefulness of your script: Assume it may need to be reusedas part of another script one day. Azero denotes success in shell programming. Since the script as it stands can t detect any failures, return success. We ll come back to the reasons for using a zero exit code for success later in when we look at the exitcommand in more detail.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

Suppose we have a large number (Fedora web server) of C

Friday, April 27th, 2007

Suppose we have a large number of C files and we wish to examine the files that contain the stringPOSIX. Rather than search using the grepcommand for the string in the files and then list the files indi- vidually, we could perform the whole operation in an interactive script like this: $ for file in * > do> if grep -l POSIX $file> then> more $file> fi> doneposixThis is a file with POSIX in it - treat it well$ Note how the normal $ shell prompt changes to a > when you type shell commands. You can type away, letting the shell decide when you re finished, and the script will execute immediately. In this example, the grepcommand prints the files it finds containing POSIX and then moreprints thecontents of the file to the screen. Finally, the shell prompt returns. Note also that we ve called the shellvariable that deals with each of the files to self-document the script. We could equally well have used i, but fileis more meaningful for humans to read. The shell also performs wildcard expansion (often referred to as globbing ). You are almost certainlyaware of the use of * as a wildcard to match a string of characters. What you may not know is that youcan request single-character wildcards using ?, while [set] allows any of a number of single characters tobe checked. [^set] negates the set that is, it includes anything but the set you ve specified. Brace expan- sion using {} (available on some shells, including bash) allows you to group arbitrary strings together ina set that the shell will expand. For example, $ ls my_{finger,toe}swill list the files my_fingersand my_toes. We ve used the shell to check every file in the current direc- tory. We will come back to these rules for matching patterns near the end of the chapter when we look inmore detail at grepand the power of regular expressions. Experienced UNIX users would probably perform this simple operation in a much more efficient way, perhaps with a command such as$ more `grep -l POSIX *` or the synonymous construction$ more $(grep -l POSIX *) Also, $ grep -l POSIX * | morewill output the name of the file whose contents contained the string POSIX. In this script, we see theshell making use of other commands, such as grepand more, to do the hard work. The shell is simply24Chapter
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

PipesWe can connect processes using the pipe |operator. (Web hosting domain names)

Friday, April 27th, 2007

PipesWe can connect processes using the pipe |operator. In Linux, unlike in MS-DOS, processes connected can run simultaneously and are automatically rescheduled as data flows between them. As a ple example, we could use the sortcommand to sort the output from ps. If we don t use pipes, we must use several steps, like this: $ ps > psout.txt$ sort psout.txt > pssort.outAmuch more elegant solution is to connect the processes with a pipe, like this: $ ps | sort > pssort.outSince we probably want to see the output paginated on the screen, we could connect a third process, more, all on the same command line: $ ps | sort | moreThere s practically no limit to the permissible number of connected processes. Suppose we want to the different process names that are running excluding shells. We could use$ ps xo comm | sort | uniq | grep -v sh | moreThis takes the output of ps, sorts it into alphabetical order, extracts processes using uniq, uses grep remove the process named sh, and finally displays it paginated on the screen. As you can see, this is a much more elegant solution than a string of separate commands, each with temporary file. There is, however, one thing to be wary of here: If you have a string of commands, the output file is created or written to immediately as the set of commands is created, so never use filename twice in a string of commands. If you try to do something like the following: cat mydata.txt | sort | uniq | > mydata.txtyou will end up with an empty file, because you will overwrite the mydata.txtfile before you read The Shell as a Programming LanguageNow that we ve seen some basic shell operations, it s time to move on to scripts. There are two ways shell programs. You can type a sequence of commands and allow the shell to execute them actively, or you can store those commands in a file that you can then invoke as a program. Interactive ProgramsJust typing the shell script on the command line is a quick and easy way of trying out small code frag- ments and is very useful while you are learning or just testing things out.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

Web hosting companies - the command set -C, which sets the noclobber

Thursday, April 26th, 2007

the command set -C, which sets the noclobber option to prevent a file from being overwritten using redi- rection. We ll show you more options for the set command later in the chapter. To append to the file, we use the >>operator. For example, $ ps >> lsoutput.txtwill append the output of the ps command to the end of the specified file. To redirect the standard error output, we preface the >operator with the number of the file descriptorwe wish to redirect. Since the standard error is on file descriptor 2, we use the 2>operator. This is oftenuseful to discard error information and prevent it from appearing on the screen. Suppose we want to use the kill command to kill a process from a script. There is always a slight riskthat the process will die before the kill command is executed. If this happens, kill will write an errormessage to the standard error output, which, by default, will appear on the screen. By redirecting boththe standard output and the error, you can prevent the kill command from writing any text to the screen. The following command, $ kill -HUP 1234 >killout.txt 2>killerr.txtwill put the output and error information into separate files. If you prefer to capture both sets of output into a single file, you can use the >&operator to combine thetwo outputs. So$ kill -1 1234 >killouterr.txt 2>&1will put both the output and error outputs into the same file. Notice the order of the operators. Thisreads as redirect standard output to the file killouterr.txt, then direct standard error to the sameplace as the standard output. If you get the order wrong, the redirect won t work as you expect. Since you can discover the result of the killcommand using the return code (which we discuss in moredetail later in this chapter), we don t often want to save either standard output or standard error. We canuse the UNIX universal bit bucket of /dev/nullto efficiently discard the entire output, like this: $ kill -1 1234 >/dev/null 2>&1Redirecting InputRather like redirecting output, we can also redirect input. For example, $ more < killout.txtObviously, this is a rather trivial example under Linux; the Linux morecommand is quite happy toaccept filenames as parameters, unlike the Windows command-line equivalent. 22Chapter
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Web hosting packages - Many other shells are available, either free or

Thursday, April 26th, 2007

Many other shells are available, either free or commercially. Following is a brief summary of some common shells available: Shell NameABit of Historysh (Bourne)The original shell from early versions of UNIX. csh, tcsh, zshThe C shell, and its derivatives, originally created by Bill Joy of BerkeleyUNIX fame. The C shell is probably the third most popular type of shellafter bash and the Korn shell. ksh, pdkshThe Korn shell and its public domain cousin. Written by David Korn, this default shell on many commercial UNIX versions. bashThe Linux staple shell from the GNU project. bash, or Bourne Again SHell, has the advantage that the source code is freely available, and even if it snot currently running on your UNIX system, it has probably been ported bash has many similarities to the Korn shell. Except for the C shell and a small number of derivatives, all of these are very similar and are closelyaligned with the shell specified in the X/Open 4.2 and POSIX 1003.2 specifications. POSIX 1003.2 laysdown the minimum specification for a shell, but the extended specification in X/Open provides a morefriendly and powerful shell. X/Open is usually the more demanding specification, but it also yields system. Pipes and RedirectionBefore we get down to the details of shellprograms, we need to say a little about how inputs and puts of Linux programs (not just shell programs) can be redirected. Redirecting OutputYou may already be familiar with some redirection, such as$ ls -l > lsoutput.txtwhich saves the output of the lscommand into a file called lsoutput.txt. However, there is much more to redirection than this simple example reveals. We ll learn more about file descriptors in Chapter 3, but for now all we need to know is that file descriptor 0 is thestandard input to a program, file descriptor 1 is the standard output, and file descriptor 2 is the standarderror output. You can redirect each of these independently. In fact, you can also redirect other filedescriptors, but it s unusual to want to redirect any other than the standard ones: 0, 1, and 2. In the preceding example, we redirect the standard output into a file by using the >operator. By default, if the file already exists, it will be overwritten. If you want to change the default behavior, you can
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

You can check the version of bash (Web site translator) you

Thursday, April 26th, 2007

You can check the version of bash you have with the following command: $ /bin/sh versionGNU bash, version 2.05b.0(1)-release (i686-pc-linux-gnu) Copyright (C) 2002 Free Software Foundation, Inc. When you create a Linux user, you can set the shell that she will use. Figure 2-2 shows the default selec- tion available in Red Hat s user manager. Figure 2-2To change to a different shell if bash isn t the default on your system, for exam- ple just execute the desired shell s program (e.g., /bin/bash) to run the new shelland change the command prompt. If bash isn t installed on your UNIX system, youcan download it free from the GNU Web site at http://www.gnu.org. The sourcesare highly portable, and chances are good that it will compile on your version ofUNIX straight out of the box. 20Chapter
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Whether you ever reimplement the script depends on (Ipower web hosting)

Thursday, April 26th, 2007

Whether you ever reimplement the script depends on whether it needs optimizing, whether it needs portable, whether it should be easy to change, and whether (as usually happens) it outgrows its nal purpose. There are already loads of examples of shell scripts on your Linux system in case you re curious, includ- ing package installers, .xinitrcand startx, and the scripts in /etc/rc.dto configure thesystem on boot-up. What Is a Shell? Before jumping in and discussing how to program using a shell, let s review the shell s function and shells available for Linux. Ashellis a program that acts as the interface between you and the Linux system, allowing you to entercommands for the operating system to execute. In that respect, it resembles the Windows commandprompt, but as we mentioned earlier, Linux shells are much more powerful. For example, input and put can be redirected using , data piped between simultaneously executing programs using and output from a subprocess grabbed by using $(…). On Linux it s quite feasible to have multipleshells installed, with different users able to pick the one they prefer. In Figure 2-1 we show how the two shells actually, both bash and csh) and other programs sit around the Linux kernel. Figure 2-1Since Linux is so modular, you can slot in one of the many different shells in use, although most of derived from the original Bourne shell. On Linux, the standard shell that is always installed as/bin/shis called bash (the GNU Bourne-Again SHell), from the GNU suite of tools. Because this is shell that is always installed on Linux systems, is open source, and is portable to almost allUNIX variants, bash is the shell we will be using. In this chapter, we ll use bash version 2 and mostly features common to all POSIX-compatible shells. We ll also assume that the shell has been installedas /bin/shand that it is the default shell for your login. On most Linux distributions, the program/bin/sh, the default shell, is actually a link to the program /bin/bash. TheX WindowSystemOtherprogramsbashcshKernel19ShellProgrammingb544977
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

So, whether you re faced with a nightmare of (Web site development)

Wednesday, April 25th, 2007

So, whether you re faced with a nightmare of a shell script in your system administration, want to proto- type your latest big (but beautifully simple) idea, or just want to speed up some repetitive task, thischapter is for you. Why Program with a Shell? In UNIX, which is the parent operating system of Linux and the origin of many of the ideas and of thephilosophy of the operating system, a variety of different shell programs are available. The most commonon commercial versions of UNIX is probably the Korn shell, but there are many others. So why use a shellto program? Well, the shell leads a double life. Although it has superficial similarities to the Windowscommand prompt, it s much more powerful, capable of running reasonably complex programs in its ownright. You can not only execute commands and call Linux utilities; you can also write them. The shell usesan interpreted language, which generally makes debugging easier because you can execute single lines, and there s no recompile time. However, this can make the shell unsuitable for time-critical or processor- intensive tasks. One reason to use the shell for programming is that you can program the shell quickly and simply. Also, a shell is always available even on the most basic Linux installation. So for simple prototyping you canfind out if your idea works. The shell is also ideal for any small utilities that perform some relativelysimple task where efficiency is less important than easy configuration, maintenance, and portability. Youcan use the shell to organize process control, so that commands run in a predetermined sequence depen- dent on the successful completion of each stage. A Bit of PhilosophyHere we come to a bit of UNIX and of course Linux philosophy. UNIX is built on and depends onahigh level of code reuse. You build a small and simple utility and people use it as one link in a stringofothers to form a command. One of the pleasures of Linux is the variety of excellent tools available. Asimple example is$ ls -al | moreThis command uses the lsand moreutilities and pipes the output of the file listing to a screen-at-a-timedisplay. Each utility is one more building block. You can often use many small scripts together to createlarge and complex suites of programs. For example, if you want to print a reference copy of the bash manual pages, use$ man bash | col -b | lprFurthermore, because of Linux s automatic file type handling, the users of these utilities usually don tneed to know what language the utilities are written in. If the utility needs to run faster, it s quite com- mon to prototype utilities in the shell and reimplement them later in C or C++, Perl, Python, or someother language that executes more swiftly once an idea has proven its worth. Conversely, if the utilityworks well enough in the shell, you can leave well enough alone. 18Chapter
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

2Shell ProgrammingHaving just started this book on programming (Free php web host)

Wednesday, April 25th, 2007

2Shell ProgrammingHaving just started this book on programming Linux using C, we now take a detour into writingshell programs. Why? Well, Linux isn t like systems where the command-line interface is anafterthought to the graphical interface. UNIX, Linux s inspiration, originally had no graphical inter- face at all; everything was done from the command line. Consequently, the command line system ofUNIX had a lot of development and became a very powerful feature. This has been carried intoLinux, and some of the most powerful things that you can do are most easily done from the shell. Because the shell is so important to Linux, we thought we should cover shell programming early. Throughout this chapter, we ll be learning the syntax, structures, and commands available to youwhen you re programming the shell, usually making use of interactive (screen-based) examples. These should serve as a useful synopsis of most of the shell s features and their effects. We willalso sneak a look at a couple of particularly useful command line utilities often called from theshell: grepand find. While looking at grepwe will also cover the fundamentals of regularexpressions, which crop up in Linux utilities and also in programming languages such as Perl andPHP. At the end of the chapter, we show you how to program a real-life script, which is repro- grammed and extended in C throughout the book. In this chapter, we ll cover .What a shell is .Basic considerations .The subtleties of syntax: variables, conditions, and program control .Lists .Functions .Commands and command execution .Here documents .Debugging .grep and regular expressions .findb544977
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services