Archive for August, 2007

/dev/nullThis is the null device. All (Com web hosting) output written

Friday, August 31st, 2007

/dev/nullThis is the null device. All output written to this device is discarded. An immediate end of file isreturned when the device is read, and it can be used as a source of empty files by using the cpcom- mand. Unwanted output is often redirected to /dev/null. $ echo do not want to see this >/dev/null$ cp /dev/null empty_fileOther devices found in /devinclude hard and floppy disks, communications ports, tape drives, CD-ROMs, sound cards, and some devices representing the system s internal state. There s even a/dev/zero, which acts as a source of nullbytes to create files of zeros. You need superuser permissionsto access some of these devices; normal users can t write programs to directly access low-level deviceslike hard disks. The names of the device files may vary from system to system. Linux distributions usu- ally have applications that run as superuser to manage the devices that would be otherwise inaccessible, for example, mountfor user-mountable file systems. Devices are classified as either character devicesor block devices. The difference refers to the fact that somedevices need to be accessed a block at a time. Typically, the only block devices are those that supportsome type of random access file system, like hard disks. In this chapter, we concentrate on disk files and directories. We ll cover another device, the user s termi- nal, in Chapter 5. System Calls and Device DriversWe can access and control files and devices using a small number of functions. These functions, knownas system calls, are provided by UNIX (and Linux) directly, and are the interface to the operating systemitself. At the heart of the operating system, the kernel, are a number of device drivers. These are a collection oflow-level interfaces for controlling system hardware, which we will cover in detail in Chapter 18. Forexample, there will be a device driver for a tape drive, which knows how to start the tape, wind it for- ward and backward, read and write to it, and so on. It will also know that tapes have to be written toinblocks of a certain size. Because tapes are sequential in nature, the driver can t access tape blocksdirectly, but must wind the tape to the right place. Similarly, a low-level hard disk device driver will only write whole numbers of disk sectors at a time, but will be able to access any desired disk block directly, because the disk is a random access device. To provide a similar interface, device drivers encapsulate all of the hardware-dependent features. Idiosyncratic features of the hardware are usually available through ioctl. Another way of creating empty files is to use the touch command, which changes the modification time of a file or creates a new file if none existswith the given name. It won t empty it of its contents, though. 94Chapter

Figure 3-1Files and DevicesEven hardware devices (Web hosting colocation) are very

Thursday, August 30th, 2007

Figure 3-1Files and DevicesEven hardware devices are very often represented (mapped) by files. For example, as the superuser, a CD-ROM drive as a file: # mount -t iso9660 /dev/hdc /mnt/cdrom# cd /mnt/cdromwhich takes the CD-ROM device (loaded as /dev/hdcduring boot-up) and mounts its current contentsas the file structure beneath /mnt/cdrom. You then move around within the CD-ROM s directories normal, except, of course, that the contents are read-only. Three important device files found in both UNIX and Linux are /dev/console, /dev/tty, and/dev/null. /dev/consoleThis device represents the system console. Error messages and diagnostics are often sent to this device. Each UNIX system has a designated terminal or screen to receive console messages. At one time, it been a dedicated printing terminal. On modern workstations, and on Linux, it s usually the `active virtual console, while under X, it will be a special console window on the screen. /dev/ttyThe special file /dev/ttyis an alias (logical device) for the controlling terminal (keyboard and screen, or window) of a process, if it has one. For instance, processes running from cronwon t have a control- ling terminal, and therefore won t be able to open /dev/tty. Where it can be used, /dev/ttyallows a program to write directly to the user, without regard to whichpseudo-terminal or hardware terminal the user is using. It is useful when the standard output has One example is the command ls R | more, where the program morehas to prompt for each new page of output. We ll see more of /dev/ttyin Chapter 5. Note that while there s only one /dev/consoledevice, there are effectively many different physicaldevices accessed through /dev/tty. programslettersneil/ rickhomedevbinmail93Working

Linux File Structure Why, you may be (Zeus web server) asking, are

Thursday, August 30th, 2007

Linux File Structure Why, you may be asking, are we covering file structure? I know about that already. Well, as withUNIX, files in the Linux environment are particularly important, as they provide a simple and consistentinterface to the operating system services and devices. In Linux,everything is a file. Well, almost! This means that, in general, programs can use disk files, serial ports, printers, and other devices inexactly the same way they would use a file. We ll cover some exceptions, such as network connections, in Chapter 15, but mainly you need to use only five basic functions: open, close, read, write, andioctl. Directories, too, are special sorts of files. In modern UNIX versions, including Linux, even the superusermay not write to them directly. All users ordinarily use the high-level opendir/readdirinterface toread directories without needing to know the system-specific details of directory implementation. We llreturn to special directory functions later in this chapter. Really, almost everything is represented as a file under Linux, or can be made available via special files. Even though there are, by necessity, subtle differences from the conventional files we know and love, thegeneral principle still holds. Let s look at the special cases we ve mentioned so far. DirectoriesAs well as its contents, a file has a name and some properties, or administrative information ; that is, thefile s creation/modification date and its permissions. The properties are stored in the file s inode, a specialblock of data in the file system that also contains the length of the file and where on the disk it s stored. The system uses the number of the file s inode; the directory structure just names the file for our benefit. Adirectory is a file that holds the inode numbers and names of other files. Each directory entry is a linkto a file s inode; remove the filename and you remove the link. (You can see the inode number for a fileby using ln i.) Using the lncommand, you can make links to the same file in different directories. Ifthe number of links to a file (the number after the permissions in ls -l) reaches zero, the inode and thedata it references are no longer in use and are marked as free. Files are arranged in directories, which may also contain subdirectories. These form the familiar file sys- tem hierarchy. Auser, say neil, usually has his files stored in a home directory, perhaps /home/neil, with subdirectories for e-mail, business letters, utility programs, and so on. Note that many commandshells for UNIX and Linux have an excellent notation for getting straight to your home directory: thetilde ~. For another user, type ~user. As you know, home directories for each user are usually subdirecto- ries of a higher-level directory created specifically for this purpose, in this case /home. Note that the standard library functions unfortunately do not understand the tilde notation in file nameparameters. The /homedirectory is itself a subdirectory of the root directory, /, which sits at the top of the hierarchyand contains all of the system s files in subdirectories. The root directory normally includes /binfor sys- tem programs (`binaries ), /etcfor system configuration files, and /libfor system libraries. Files thatrepresent physical devices and provide the interface to those devices are conventionally found in a direc- tory called /dev. See Figure 3-1 for an example of part of a typical Linux hierarchy. More information onthe Linux file system layout is available in the Linux File System Standard, or you can check out manhierfor a description of the directory hierarchy. 92Chapter

Web hosting servers - 3Working with FilesIn this chapter, we ll be looking

Wednesday, August 29th, 2007

3Working with FilesIn this chapter, we ll be looking at Linux files and directories and how to manipulate them. We lllearn how to create files, open them, read, write, and close them. We ll also learn how programscan manipulate directories (to create, scan, and delete them, for example). After the last chapter sdiversion into shells, we now start programming in C. Before proceeding to the way Linux handles file I/O, we ll review the concepts associated withfiles, directories, and devices. To manipulate files and directories, we need to make system calls(the UNIX and Linux parallel of the Windows API), but there also exists a whole range of libraryfunctions, the standard I/O library (stdio), to make file handling more efficient. We ll spend the majority of the chapter detailing the various calls to handle files and directories. So this chapter will cover .Files and devices .System calls .Library functions .Low-level file access .Managing files .The standard I/O library .Formatted input and output .File and directory maintenance .Scanning directories .Errors .The /procfile system .Advanced topics: fcntland mmapb544977

r) remove_records;; f) find_cd y;; u) (Web hosting solutions) update_cd;; c)

Tuesday, August 28th, 2007

r) remove_records;; f) find_cd y;; u) update_cd;; c) count_cds;; l) list_tracks;; b) echomore $title_fileechoget_return;; q | Q ) quit=y;; *) echo Sorry, choice not recognized ;; esacdone#Tidy up and leaverm -f $temp_fileecho Finished exit 0Notes on the ApplicationThe trapcommand at the start of the script is intended to trap the user s pressing of Ctrl+C. This maybe either the EXITor the INTsignal, depending on the terminal setup. There are other ways of implementing the menu selection, notably the selectconstruct in bash and ksh(which, isn t, however, specified in X/Open). This construct is a dedicated menu choice selector. Check itout if your script can afford to be slightly less portable. Multiline information given to users could alsomake use of here documents. You might have noticed that there s no validation of the primary key when a new record is started; thenew code just ignores the subsequent titles with the same code, but incorporates their tracks into the firsttitle s listing: 1 First CD Track 12 First CD Track 21 Another CD2 With the same CD keyWe ll leave this and other improvements to your imagination and creativity, as you can modify the codeunder the terms of the GPL. SummaryIn this chapter, we ve seen that the shell is a powerful programming language in its own right. Its abilityto call other programs easily and then process their output makes the shell an ideal tool for tasks involv- ing the processing of text and files. Next time you need a small utility program, consider whether you can solve your problem by combiningsome of the many Linux commands with a shell script. You ll be surprised just how many utility programsyou can write without a compiler. 90Chapter

11.list_tracksagain uses grepto extract the lines we want, (Top ten web hosting)

Tuesday, August 28th, 2007

11.list_tracksagain uses grepto extract the lines we want, cutto access the fields we and then moreto provide a paginated output. If you consider how many lines of C code itwould take to reimplement these 20-odd lines of code, you ll appreciate how powerful a tooltheshell can be. list_tracks() { if [ $cdcatnum = ]; thenecho no CD selected yetreturnelsegrep ^${cdcatnum}, $tracks_file > $temp_filenum_tracks=$(wc -l $temp_file) if [ $num_tracks = 0 ]; thenecho no tracks found for $cdtitleelse { echoecho $cdtitle :- echo cut -f 2- -d , $temp_file echo } | ${PAGER:-more} fifiget_returnreturn} 12.Now that all the functions have been defined, we can enter the main routine. The first few get the files into a known state; then we call the menu function, set_menu_choice, and act on the output. When quitis selected, we delete the temporary file, write a message, and exit with a successfulcompletion condition. rm -f $temp_fileif [ ! -f $title_file ]; thentouch $title_filefiif [ ! -f $tracks_file ]; thentouch $tracks_filefi# Now the application properclearechoechoecho Mini CD manager sleep 1quit=nwhile [ $quit != y ]; doset_menu_choicecase $menu_choice ina) add_records;;

echo Current tracks are :- list_tracksechoecho This will (Free web hosting services)

Monday, August 27th, 2007

echo Current tracks are :- list_tracksechoecho This will re-enter the tracks for $cdtitle get_confirm && { grep -v ^${cdcatnum}, $tracks_file > $temp_filemv $temp_file $tracks_fileechoadd_record_tracks} fireturn} 9.count_cdsgives us a quick count of the contents of our database. count_cds() { set $(wc -l $title_file) num_titles=$lset $(wc -l $tracks_file) num_tracks=$lecho found $num_titles CDs, with a total of $num_tracks tracksget_returnreturn} 10.remove_recordsstrips entries from the database files, using grep -vto remove all match- ing strings. Notice we must use a temporary file. If we tried to do this, grep -v ^$cdcatnum > $title_filethe $title_filewould be set to empty by the >output redirection before grephad thechance to execute, so grepwould read from an empty file. remove_records() { if [ -z $cdcatnum ]; thenecho You must select a CD firstfind_cd nfiif [ -n $cdcatnum ]; thenecho You are about to delete $cdtitle get_confirm && { grep -v ^${cdcatnum}, $title_file > $temp_filemv $temp_file $title_filegrep -v ^${cdcatnum}, $tracks_file > $temp_filemv $temp_file $tracks_filecdcatnum= echo Entry removed} get_returnfireturn} 88Chapter

Domain and web hosting - case $linesfound in0) echo Sorry, nothing found get_returnreturn

Sunday, August 26th, 2007

case $linesfound in0) echo Sorry, nothing found get_returnreturn 0;; 1) ;; 2) echo Sorry, not unique. echo Found the following cat $temp_fileget_returnreturn 0esacIFS= , read cdcatnum cdtitle cdtype cdac < $temp_fileIFS= if [ -z $cdcatnum ]; thenecho Sorry, could not extract catalog field from $temp_file get_return return 0fiechoecho Catalog number: $cdcatnumecho Title: $cdtitleecho Type: $cdtypeecho Artist/Composer: $cdacechoget_returnif [ $asklist = y ]; thenecho -e View tracks for this CD? c read xif [ $x = y ]; thenecholist_tracksechofifireturn 1} 8.update_cdallows us to re-enter information for a CD. Notice that we search (using grep) forlines that start (^) with the $cdcatnumfollowed by a ,and that we need to wrap theexpansion of $cdcatnumin {}so we can search for a ,with no whitespace between it and number. This function also uses {}to enclose multiple statements to be executed confirmreturns true. update_cd() { if [ -z $cdcatnum ]; thenecho You must select a CD first find_cd nfiif [ -n $cdcatnum ]; then87ShellProgrammingb544977

read tmpcdtype=${tmp%%,*} echo -e Enter artist/composer c read (Web site domain)

Sunday, August 26th, 2007

read tmpcdtype=${tmp%%,*} echo -e Enter artist/composer c read tmpcdac=${tmp%%,*} # Check that they want to enter the informationecho About to add new entryecho $cdcatnum $cdtitle $cdtype $cdac # If confirmed then append it to the titles fileif get_confirm ; theninsert_title $cdcatnum,$cdtitle,$cdtype,$cdacadd_record_trackselseremove_recordsfi return} 7.The find_cdfunction searches for the catalog name text in the CD title file, using the grepcommand. We need to know how many times the string was found, but grepreturns a valuetelling us only if it matched zero times or many. To get around this, we store the output in a file, which will have one line per match, then count the lines in the file. The word count command, wc, has white space in its output, separating the number of lines, words, and characters in the file. We use the $(wc -l $temp_file)notation to extract thefirst parameter from the output in order to set the linesfoundvariable. If we wanted another, later parameter, we would use the setcommand to set the shell s parameter variables to thecommand output. We change the IFS (Internal Field Separator) to a comma so we can separate the comma-delimitedfields. An alternative command is cut. find_cd() { if [ $1 = n ]; thenasklist=nelseasklist=yficdcatnum= echo -e Enter a string to search for in the CD titles c read searchstrif [ $searchstr = ]; thenreturn 0figrep $searchstr $title_file > $temp_fileset $(wc -l $temp_file) linesfound=$l86Chapter

They are followed by the larger (Vps web hosting) add_record_trackfunction that

Saturday, August 25th, 2007

They are followed by the larger add_record_trackfunction that uses them. This functionuses pattern matching to ensure that no commas are entered (since we re using commas as separator) and also arithmetic operations to increment the current track number as tracksare entered. insert_title() { echo $* >> $title_filereturn} insert_track() { echo $* >> $tracks_filereturn} add_record_tracks() { echo Enter track information for this CD echo When no more tracks enter q cdtrack=1cdttitle= while [ $cdttitle != q ] doecho -e Track $cdtrack, track title? c read tmpcdttitle=${tmp%%,*} if [ $tmp != $cdttitle ]; thenecho Sorry, no commas allowed continuefiif [ -n $cdttitle ] ; thenif [ $cdttitle != q ]; theninsert_track $cdcatnum,$cdtrack,$cdttitlefielsecdtrack=$((cdtrack-1)) ficdtrack=$((cdtrack+1)) done} 6.The add_recordsfunction allows entry of the main CD information for a new CD. add_records() { # Prompt for the initial informationecho -e Enter catalog name c read tmpcdcatnum=${tmp%%,*} echo -e Enter title c read tmpcdtitle=${tmp%%,*} echo -e Enter type c