Copy of CS2406

Embed Size (px)

Citation preview

  • 7/29/2019 Copy of CS2406

    1/831

    Ex.No: 01Kernel Configuration, Compilation and Installation

    Date :

    Aim:

    Download / access the latest kernel source code from kernel.org,compile the kernel andinstall it in the local system. Try to view the source code of the kernel.

    Procedure:

    Linux Kernel 3.0/3.01 configuration, compilation and installation in Fedora 14 or 15 :You can also follow the same steps to compile and install Linux Kernel 3.0.1 in your system.

    Dependencies:

    To compile Linux Kernel the following are required to be installed.

    gcc latest version,ncurses development package andsystem packages should be up-to date

    To install the dependencies run the following commands in terminal and type the password for

    the user, when prompted.

    For gcc

    $ sudo yum install gcc

    For ncurses development package

    $ sudo yum install ncurses-devel

    After installing the above packages then update your system by running the following command

    $ sudo yum update

    Now download the Linux Kernel 3.0 from kernel.org or by using the below command. If your trying with Linux Kernel 3.0.1 then just replace the name linux-3.0 with linux-3.0.1 in allcommands below.$ wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.0.tar.bz2

    Once the download complete move to the directory where you have downloaded the kernel package linux- 3.0.tar.bz2. Now extract the tar file to the location /usr/src/.

    To move to the directory, for example if the downloaded package is in your Downloadsdirectory. Use the below command.$ cd Downloads/To extract the tar file run the following command. Type the password for the user when

    prompted.$ sudo tar -xvf linux-3.0.tar.bz2 -C /usr/src/

  • 7/29/2019 Copy of CS2406

    2/832

    Now move to the directory where the extracted file is, or copy and paste the below command interminal.$ cd /usr/src/linux-3.0/

    Now you can configure, compile and install Linux Kernel 3.0 in your system. Run the commands

    one by one and type the password for the user, when prompted.To Configure$ sudo make menuconfig

    The above command is used to configure the Linux kernel. Once you execute the command, youwill get a pop up window with the list of menus and you can select the items for the newconfiguration. If your unfamiliar with the configuration just check for the file systems menu andcheck whether ext4 is chosen or not, if not select it and save the configuration. Check the screenshot below for menuconfig.

    If you like to have your existing configuration then run the below command.$ sudo make oldconfig

    There are other alternate configuration commands are available and you can find them inREADME file under linux-3.0 directory.To Compile$ sudo make

    The above command is used to compile the Linux Kernel. It will take some time to complete it,approximately 40 min to 50 min it depends on your system configuration.To Install$ sudo make modules_install install

    The above command will install the Linux Kernel 3.0 into your system. It will create some filesunder /boot/ directory and it will automatically make a entry in your grub.conf.The files are,System.map-3.0.0vmlinuz-3.0.0initramfs-3.0.0.imgTo make it de fault just modify the grub.conf or menu.lst under /boot/grub/ directory. Open thefile using below command.$ sudo gedit /boot/grub/menu.lst

    Just check the entry for fedora 3.0.0, if it is in first place then change default=0, if not check for the exact position and give the corresponding value in default.Restart your system by default it will boot in fedora 3.0.0, you can able to see while booting if not press any key to change it.To check after booting open a terminal and type uname -r. Check the screen shot below.

  • 7/29/2019 Copy of CS2406

    3/833

    Ex.No: 02Virtualisation Environment

    Date :

    Aim:

    Virtualisation environment (e.g., xen, kqemu or lguest) to test an applications, new kernels andisolate applications. It could also be used to expose students to other alternate OSs like *BSD

    Procedure:

    What is Virtualisation?Virtualisation, in computing, is the creation of a virtual (rather than actual) version of

    something, such as a hardware platform, operating system, a storage device or network resources.

    So what we are going to do is to install another operating system say 'Ubuntu' within our host operating system 'Fedora'. Now the 'Ubuntu' contained within the virtual machine is called

    as the guest Operating System. The software that we are going to use to perform this is the opensource software called 'Qemu'

    Installing Ubuntu within Fedora using QEMU :

    1) The first step involved here is to download the QEMU source code (if it is not already present). You can perform this download by clicking the following link Download QEMUSource code

    2) Next task is to build the emulator from the source code. Go the folder where you havedownloaded the source code in the terminal and enter the following two commands:$ tar -zxvf qemu-0.14.0.tar.gz

    $ cd qemu-0.14.0

    3) Next we are going to configure the qemu script such that it is built for i386 architecture.However it supports various other architectures like ARM, PPC, SPARC etc. Enter the followingcommand:$ ./configure --target-list=i386-softmmu(Before doing this make sure you have installed Header files and libraries for zlib development.

    If you haven't then go to 'Add/Remove software' app in Fedora and do it and then configure your script)

    4) Once you are done with configuring now you have to install it. For doing so, you must login

    as root in your terminal using the 'su' command. After that use the 'make' command to install it.$ suPassword: (This is not a command, but will come when you enter su)# make install

    5) Now you have installed qemu. Next step is to create the virtual machine. The first step in thatis to create a Virtual Hard disk image for that. For our Ubuntu OS let us a create an Hard disk of size 10 GB. To create that go to a folder where you want to create the hard disk image and enter the following:$ qemu-img create ubuntu.img 10G

  • 7/29/2019 Copy of CS2406

    4/834

    $ ls -lh ubuntu.img (To check)

    6)So you have created the hard disk. What next??? Yes..To install Ubuntu into it. You have todownload ubuntu and place its .iso image in the current working directory.Then execute the following commands

    qemu -hda ubuntu.img --boot d -cdrom ./ubuntu-10.10-desktop-i386.iso -m 512In the above command, '-hda' option specifies the disk image file, '-cdrom' is the CD-ROM or

    iso image to be used and '-m' option specifies the memory to be allocated for the guest OS. Inthis case it is 512 MB of RAM.

    Now our Virtual machine is ready. Just got to install ubuntu in it. The screenshot is given below:

  • 7/29/2019 Copy of CS2406

    5/835

    Ex. No: 03Compiling from source

    Date :

    Aim:

    Compiling from source: learn about the various build systems used like the auto* family,cmake, ant etc. instead of just running the commands. This could involve the full process likefetching from a cvs and also include autoconf, automake etc.

    Procedure:

    How to download, compile, and install CMake on Linux

    CMake is a cross-platform, open-source build system. CMake is a family of tools designed to build, test and package software.

    Here are the simple steps to download, configure, compile, and install CMake on a Linuxmachine. I have tested these instructions on Debian/Ubuntu Linux distributions but they shouldwork on all Linux machines where compilers and make utilities are installed. Please take a look at configuring Ubuntu Linux after installation to configure your Debian/Ubuntu Linux box for the required tools.

    Please make sure you check the downloaded file's integrity before unzipping it. '#' (w/o quotes)in front of commands below means you have to run the command as root user (or use sudoinstead if your system supports that).

    Download:$ wget http://www.cmake.org/files/v2.8/cmake-2.8.3.tar.gz

    Extration of cmake source code from downloaded file:$ tar xzf cmake-2.8.3.tar.gz$ cd cmake-2.8.3

    Configuration:

    If you want to see the available conifuration options, run command below.

    $ ./configure --help

    In order to configure cmake before installation, run command below.

    $ ./configure --prefix=/opt/cmake

    We basically instructed the install script to install CMake in /opt/cmake.

    Compilation:$ make

    http://www.geeksww.com/tutorials/operating_systems/linux/configuration/configuring_ubuntu_linux_after_installation.phphttp://www.geeksww.com/tutorials/operating_systems/linux/configuration/configuring_ubuntu_linux_after_installation.phphttp://www.geeksww.com/tutorials/operating_systems/linux/configuration/configuring_ubuntu_linux_after_installation.phphttp://www.geeksww.com/tutorials/operating_systems/linux/configuration/configuring_ubuntu_linux_after_installation.php
  • 7/29/2019 Copy of CS2406

    6/836

    Installation:# make install

    Verification:

    After installation without any errors you can verify the installation by running the command below:

    $ /opt/cmake/bin/cmake -version

    The output should look something like below (depending upon cmake version you areinstalling).

    cmake version 2.8.3

    Installing ant in Linux

    Step 1: Download ant from http://ant.apache.org/bindownload.cgi. I have downloaded apache-ant-1.7.1-

    bin.zip for this tutorial.

    Step 2:

    Login to your Linux box and create a directory "ant" under /usr/local.

    [root@RoseIndiaLinux local]# mkdir ant[root@RoseIndiaLinux local]# cd ant

    [root@RoseIndiaLinux ant]# pwd

    /usr/local/ant

    [root@RoseIndiaLinux ant]#

    Step 3:

    Copy apache-ant-1.7.1-bin.zip onto your Linux box in /usr/local/ant directory.

    Step 4:

    Extract the zip file apache-ant-1.7.1-bin.zip) using unzip command.

    [root@RoseIndiaLinux ant]# unzip apache-ant-1.7.1-bin.zip

    Above command will extract the content of the zip file and will create a new directory apache-

    ant-1.7.1

    Step 5:

    Set path in the .bash_profile

    http://ant.apache.org/bindownload.cgihttp://ant.apache.org/bindownload.cgi
  • 7/29/2019 Copy of CS2406

    7/837

    Open the file /root/.bash_profile and add the following codes:

    export ANT_HOME=/usr/local/ant/apache-ant-1.7.1

    export JAVA_HOME=/opt/java/jdk1.6.0_06

    export PATH=${PATH}:${ANT_HOME}/bin

    Step 6:

    Logout and login again to your Linux box. Now ant available on your box.

    Introduction to Autoconf

    Autoconf is an extensible package of M4 macros that produce shell scripts to automaticallyconfigure software source code packages. These scripts can adapt the packages to many kinds of UNIX-like systems without manual user intervention. Autoconf creates a configuration script for a package from a template file that lists the operating system features that the package can use, in

    the form of M4 macro calls.Producing configuration scripts using Autoconf requires GNU M4 . You should install GNU M4(at least version 1.4.6, although 1.4.13 or later is recommended) before configuring Autoconf, sothat Autoconf's configure script can find it. The configuration scripts produced by Autoconf areself-contained, so their users do not need to have Autoconf (or GNU M4).

    Autoconf is a tool for producing shell scripts that automatically configure software source code packages to adapt to many kinds of Posix-like systems. The configuration scripts produced byAutoconf are independent of Autoconf when they are run, so their users do not need to haveAutoconf.

    The configuration scripts produced by Autoconf require no manual user intervention when run;they do not normally even need an argument specifying the system type. Instead, theyindividually test for the presence of each feature that the software package they are for mightneed. (Before each check, they print a one-line message stating what they are checking for, sothe user doesn't get too bored while waiting for the script to finish.) As a result, they deal wellwith systems that are hybrids or customized from the more common Posix variants. There is noneed to maintain files that list the features supported by each release of each variant of Posix.

    For each software package that Autoconf is used with, it creates a configuration script from atemplate file that lists the system features that the package needs or can use. After the shell codeto recognize and respond to a system feature has been written, Autoconf allows it to be shared bymany software packages that can use (or need) that feature. If it later turns out that the shell codeneeds adjustment for some reason, it needs to be changed in only one place; all of theconfiguration scripts can be regenerated automatically to take advantage of the updated code.Those who do not understand Autoconf are condemned to reinvent it, poorly. The primary goalof Autoconf is making the user's life easier; making the maintainer's life easier is only asecondary goal. Put another way, the primary goal is not to make the generation of configureautomatic for package maintainers (although patches along that front are welcome, since packagemaintainers form the user base of Autoconf); rather, the goal is to make configure painless,

    portable, and predictable for the end user of each autoconfiscated package. And to this degree,

    http://www.gnu.org/software/autoconf/#TOCintroductionhttp://www.gnu.org/software/m4/m4.htmlhttp://www.gnu.org/software/m4/m4.htmlhttp://www.gnu.org/software/m4/m4.htmlhttp://www.gnu.org/software/m4/m4.htmlhttp://www.gnu.org/software/autoconf/#TOCintroduction
  • 7/29/2019 Copy of CS2406

    8/838

    Autoconf is highly successful at its goal most complaints to the Autoconf list are aboutdifficulties in writing Autoconf input, and not in the behavior of the resulting configure. Even

    packages that don't use Autoconf will generally provide a configure script, and the most commoncomplaint about these alternative home-grown scripts is that they fail to meet one or more of theGNU Coding Standars (see Configuration ) that users have come to expect from Autoconf-

    generated configure scripts.

    The Metaconfig package is similar in purpose to Autoconf, but the scripts it produces requiremanual user intervention, which is quite inconvenient when configuring large source trees.Unlike Metaconfig scripts, Autoconf scripts can support cross-compiling, if some care is taken inwriting them.

    Autoconf does not solve all problems related to making portable software packages for a morecomplete solution, it should be used in concert with other GNU build tools like Automake andLibtool. These other tools take on jobs like the creation of a portable, recursive makefile with allof the standard targets, linking of shared libraries, and so on. See The GNU Build System , for

    more information.Autoconf imposes some restrictions on the names of macros used with #if in C programs (seePreprocessor Symbol Index ).

    Autoconf's development tree is accessible via git ; see the Autoconf Summary for details, or view the actual repository . Anonymous CVS access is also available, see README for moredetails. Patches relative to the current git version can be sent for review to the Autoconf Patches mailing list , with discussion on prior patches archived ; and all commits are posted in theread-only Autoconf Commit mailing list , which is also archived .

    Downloading Autoconf

    GNU Autoconf releases can be found on http://ftp.gnu.org/gnu/autoconf/ [via http] andftp://ftp.gnu.org/gnu/autoconf/ [via FTP]. It can also be found on one of our FTP mirrors . Youcan also order a CD-ROM from the FSF or use other methods to obtain a copy.

    Third party macros can be downloaded from the Autoconf Macro Archive .

    Alpha/beta releases of Autoconf can be found in ftp://alpha.gnu.org/pub/gnu/autoconf/ , and thelatest in-development sources for Autoconf can always be fetched through git, using either of:

    git clone git://git.sv.gnu.org/autoconfgit clone http://git.sv.gnu.org/r/autoconf.git

    You can also view the git tree on the web.

    Alternatively, there is a read-only CVS mirror here:

    cvs -d :pserver:[email protected]:/autoconf.git \co -d autoconf HEAD

    DO NOT use Autoconf sources from these locations for production use.

    http://www.gnu.org/software/autoconf/manual/standards/Configuration.html#Configurationhttp://www.gnu.org/software/autoconf/manual/standards/Configuration.html#Configurationhttp://www.gnu.org/software/autoconf/manual/html_node/The-GNU-Build-System.html#The-GNU-Build-Systemhttp://www.gnu.org/software/autoconf/manual/html_node/The-GNU-Build-System.html#The-GNU-Build-Systemhttp://www.gnu.org/software/autoconf/manual/html_node/Preprocessor-Symbol-Index.html#Preprocessor-Symbol-Indexhttp://www.gnu.org/software/autoconf/manual/html_node/Preprocessor-Symbol-Index.html#Preprocessor-Symbol-Indexhttp://savannah.gnu.org/projects/autoconf/http://savannah.gnu.org/projects/autoconf/http://savannah.gnu.org/projects/autoconf/http://git.sv.gnu.org/gitweb/?p=autoconf.githttp://git.sv.gnu.org/gitweb/?p=autoconf.githttp://git.sv.gnu.org/gitweb/?p=autoconf.gitmailto:[email protected]:[email protected]:[email protected]:[email protected]://lists.gnu.org/archive/html/autoconf-patches/http://lists.gnu.org/archive/html/autoconf-patches/http://lists.gnu.org/archive/html/autoconf-patches/mailto:[email protected]:[email protected]:[email protected]://lists.gnu.org/archive/html/autoconf-commit/http://lists.gnu.org/archive/html/autoconf-commit/http://lists.gnu.org/archive/html/autoconf-commit/http://www.gnu.org/software/autoconf/#TOCdownloadinghttp://ftp.gnu.org/gnu/autoconf/http://ftp.gnu.org/gnu/autoconf/http://ftp.gnu.org/gnu/autoconf/ftp://ftp.gnu.org/gnu/autoconf/ftp://ftp.gnu.org/gnu/autoconf/http://www.gnu.org/order/ftp.htmlhttp://www.gnu.org/order/ftp.htmlhttp://www.gnu.org/order/ftp.htmlhttp://order.fsf.org/http://order.fsf.org/http://order.fsf.org/http://www.gnu.org/software/software.html#getgnuhttp://www.gnu.org/software/software.html#getgnuhttp://www.gnu.org/software/software.html#getgnuhttp://www.gnu.org/software/autoconf-archive/http://www.gnu.org/software/autoconf-archive/http://www.gnu.org/software/autoconf-archive/ftp://alpha.gnu.org/pub/gnu/autoconf/ftp://alpha.gnu.org/pub/gnu/autoconf/ftp://alpha.gnu.org/pub/gnu/autoconf/http://git.sv.gnu.org/gitweb/?p=autoconf.githttp://git.sv.gnu.org/gitweb/?p=autoconf.githttp://git.sv.gnu.org/gitweb/?p=autoconf.githttp://git.sv.gnu.org/gitweb/?p=autoconf.gitftp://alpha.gnu.org/pub/gnu/autoconf/http://www.gnu.org/software/autoconf-archive/http://www.gnu.org/software/software.html#getgnuhttp://order.fsf.org/http://www.gnu.org/order/ftp.htmlftp://ftp.gnu.org/gnu/autoconf/http://ftp.gnu.org/gnu/autoconf/http://www.gnu.org/software/autoconf/#TOCdownloadinghttp://lists.gnu.org/archive/html/autoconf-commit/mailto:[email protected]://lists.gnu.org/archive/html/autoconf-patches/mailto:[email protected]:[email protected]://git.sv.gnu.org/gitweb/?p=autoconf.githttp://savannah.gnu.org/projects/autoconf/http://www.gnu.org/software/autoconf/manual/html_node/Preprocessor-Symbol-Index.html#Preprocessor-Symbol-Indexhttp://www.gnu.org/software/autoconf/manual/html_node/The-GNU-Build-System.html#The-GNU-Build-Systemhttp://www.gnu.org/software/autoconf/manual/standards/Configuration.html#Configuration
  • 7/29/2019 Copy of CS2406

    9/83

  • 7/29/2019 Copy of CS2406

    10/8310

    Ex. No: 04Introduction to packet management system

    Date :

    Aim:

    Introduction to packet management system : Given a set of RPM or DEB, how to buildand maintain, serve packages over http or ftp. and also how do you configure client systems toaccess the package repository

    Procedure:The RPM Package Manager (RPM) is an open packaging system, which runs on Fedora as wellas other Linux and UNIX systems. Red Hat, Inc. and the Fedora Project encourage other vendorsto use RPM for their own products. RPM is distributed under the terms of the GPL (GNU General Public License ).The RPM Package Manager only works with packages built to work with the RPM format . RPMis itself provided as a pre-installed rpm package. For the end user, RPM makes system updateseasy. Installing, uninstalling and upgrading RPM packages can be accomplished with shortcommands. RPM maintains a database of installed packages and their files, so you can invoke

    powerful queries and verifications on your system.The RPM package format has been improved for Fedora 14.

    RPM stands for Red Hat Package Manager. However, these days RPM isn't only Red Hatspecific because many other Linux distros use RPM for managing their software. For example,

    both Mandriva and SuSE use RPM for software management. With RPM, you can install,upgrade and uninstall software on Linux, as well as keep track of already installed RPM

    packages on your system. This can be done because RPM keeps a database of all software thatwas installed with it.

    RPM uses software packages that have (surprise) the .rpm extension. An RPM package containsthe actual software that gets installed, maybe some additional files for the software, informationon where the software and its files get installed, and a list of other files you need to have on your system in order to run this specific piece of software.

    When you use RPM for installing the software package, RPM checks if your system is suitablefor the software the RPM package contains, figures out where to install the files the package

    provides, installs them on your system, and adds that piece of software into its database of installed RPM packages.

    Note that different Linux distros may keep their software and the files related to that software indifferent directories. That's why it's important to use the RPM package that was made for your distribution. For example, if you install a SuSE specific software package on a Red Hat system,RPM may put the files from that package into wrong directories. In the worst case the result isthat the program doesn't find all the files it needs and doesn't work properly.

  • 7/29/2019 Copy of CS2406

    11/8311

    There are some good graphical programs for installing RPM packages, but in this tuXfile I'lldiscuss the fool-proof command line method for installing software. Note that you need to beroot when installing software in Linux. When you've got the root privileges, you use the rpmcommand with appropriate options to manage your RPM software packages.

    For installing a software package, you use the rpm command with -i option (which stands for "install"). For example, to install an RPM package called software-2.3.4.rpm:# rpm -i software-2.3.4.rpm

    If you already have some version installed on your system and want to upgrade it to the newversion, you use -U option instead (which stands for "upgrade"). For example, if you havesoftware-2.3.3.rpm installed and want to upgrade it:# rpm -U software-2.3.4.rpm

    If all goes well, the files in your package will get installed into your system and you can happilyrun your new program. But where is your new program? Note that rpm doesn't usually create aspecial directory for the software package's files. Instead, the different files from the package get

    placed into appropriate existing directories on your Linux system. Executable programs gousually into /bin, /usr/bin, /usr/X11/bin, or /usr/X11R6/bin after installing with rpm.

    But how can you run your new program if you don't know where the executable is? Sometimesthe program gets automatically added into your menu, but usually you can just run the program

    by typing its name at the command prompt. In most cases you don't have to know where the program was installed because you don't have to type the whole path when running the program,only the program's name is needed

    To remove software that was installed with RPM, you use the -e option (which stands for "erase"):# rpm -e software-2.3.4

    Note that when installing software, you have to type the name of the RPM package. But whenremoving software, you don't have to type the whole name of the package that contained thesoftware. You don't have to type the .rpm extension when removing software. Probably you don'thave to type the version number, either, so this would do exactly the same as the above:# rpm -e software

    This rpm -e command uses the RPM database to check where all the files related to this softwarewere installed and then automatically removes all of those files. After removing the programfiles, it also removes the program from the database of installed software.

    The principal benefit of open source software is, as its name implies, access to the inner workings of an application. Given the source, you can study how an application works; change,improve, and extend its operation; borrow and repurpose code (per the limits of the application'slicense); and port the application to novel and emergent platforms.

  • 7/29/2019 Copy of CS2406

    12/8312

    However, such liberal access is not always wanted. For instance, a user may not want the onus of building from source code. Instead, he or she may simply want to install the software much like atraditional "shrink-wrapped" application: insert media, run setup, answer a few prompts, and go.Indeed, for most computer users, such pre-built software is preferred. Pre-built code is lesssensitive to system vagaries and thus more uniform and predictable.

    In general, a pre-built, open source application is called a package and bundles all the binary,data, and configuration files required to run the application. A package also includes all the stepsrequired to deploy the application on a system, typically in the form of a script. The script mightgenerate data, start and stop system services, or manipulate files and directories. A script mightalso perform operations to upgrade existing software to a new version.

    Because each operating system has its idiosyncrasies, a package is typically tailored to a specificsystem. Moreover, each operating system provides its own package manager, a special utility toadd and remove packages from the system. For example, Debian Linux-based systems use theAdvanced Package Tool (APT), while Fedora Linux systems use the RPM Package Manager.

    The package manager precludes partial and faulty installations and "uninstalls" by adding andremoving the files in a package atomically. The package manager also maintains a manifest of all packages installed on the system and can validate the existence of prerequisites and co-requisites beforehand.

    If you're a software developer or a systems administrator, providing your application as a package makes installations, upgrades, and maintenance much easier. Here, you learn how to usethe popular RPM Package Manager to bundle a utility. For purposes of demonstration, you'll

    bundle the networking utility wget , which downloads files from the Internet. The wget utility isuseful but isn't commonly found standard in distributions. (An analog, curl , is often included indistributions.) Be aware that you can use RPM to distribute most anything scripts,

    documentation, and data and perform nearly any maintenance task.Building wget manually

    The wget utility, like many other open source applications, can be built manually. Understandingthat process is the starting point for bundling wget in a package. Per the general convention,

    building wget requires four steps:

    1. Download and unpack the source.2. Configure the build.3. Build the code.

    4.

    Install the software.

    You can download the latest version of the wget source code from ftp.gnu.org . The rest of thesteps require the command line, as shown in Listing 1 .

    Listing 1. Installing wget

    $ tar xzf wget-latest.tar.gz$ cd wget-1.12$ ./configureconfigure: configuring for GNU Wget 1.12

    ftp://ftp.gnu.org/ftp://ftp.gnu.org/ftp://ftp.gnu.org/http://www.ibm.com/developerworks/library/l-rpm1/#list1http://www.ibm.com/developerworks/library/l-rpm1/#list1http://www.ibm.com/developerworks/library/l-rpm1/#list1http://www.ibm.com/developerworks/library/l-rpm1/#list1ftp://ftp.gnu.org/
  • 7/29/2019 Copy of CS2406

    13/8313

    checking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... build-aux/install-sh -c -dchecking for gawk... nochecking for mawk... nochecking for nawk... no...$ make$ sudo make install

    ./configure queries the system and sets compilation options suitable for the hardware andsoftware detected. make compiles the code, and sudo make install installs the code in systemdirectories. By default, the directories are rooted at /usr/local, although you can change the targetroot with the --prefix= /some/full/path/name option to ./configure .

    To convert this process to RPM, you place the source in a repository and write a configurationfile to dictate where to find the source to be compiled and how to build and install the code. Theconfiguration file, called a spec file, is the input to a utility called rpmbuild . The spec file andthe binaries are packaged by rpmbuild into an RPM. When another user downloads your RPM,the rpm utility reads the spec file and installs the package per your prewritten instructions.

    Building your first RPM

    Before you continue, one word of caution. In the past, packages were built by root, the superuser, because root was the only user able to access the system source code repository. However, thisapproach was potentially hazardous. Because root can alter any file on the system, it was easy toinadvertently alter a running system by adding extraneous files or removing important filesduring interim builds of an RPM. More recently, the RPM system changed to allow any user to

    build RPMs in a home directory. Building an RPM without the privileges of root preventschanges to core system files. Here, the more modern approach is shown.

    To build an RPM, you must:

    Set up a directory hierarchy per the rpmbuild specifications. Place your source code and supplemental files in the proper locations in the hierarchy. Create your spec file. Build the RPM. You can optionally build a source RPM to share your source code with

    others.

    To begin, build the hierarchy. In a directory in your home directory say, $HOME/mywget creates five subdirectories:

    BUILD. BUILD is used as scratch space to actually compile the software. RPMS. RPMS contains the binary RPM that rpmbuild builds. SOURCES. SOURCES is for source code. SPECS. SPECS contains your spec file or files one spec file per RPM you want to

    build.

  • 7/29/2019 Copy of CS2406

    14/8314

    SRPMS. SRPMS contains the source RPM built during the process.

    At a minimum, you need source code in SOURCES and a spec file in SPECS.

    Copy your source, ideally bundled as a tarball, into the SOURCES directory, as shown in Listing

    2. If necessary, rename the tarball to include the version number of the application todifferentiate it from others. The naming convention is package-version .tar.gz. In the case of wget , you would use:

    Listing 2. Copying your source

    $ cd ~$ mkdir mywget$ cd mywget$ mkdir BUILD RPMS SOURCES SPECS SRPMS$ cd SOURCES$ cp wget-latest.tar.gz .$ mv wget-latest.tar.gz wget-1.12.tar.gz$ cd ..

    Next, create the spec file. A spec file is nothing more than a text file with a special syntax.Listing 3 shows an example of a spec file.

    Listing 3. Sample spec file

    # This is a sample spec file for wget

    %define _topdir /home/strike/mywget%define name wget

    %define release 1%define version 1.12%define buildroot %{_topdir}/%{name}-%{version}-root

    BuildRoot: %{buildroot}Summary: GNU wgetLicense: GPLName: %{name}Version: %{version}Release: %{release}Source: %{name}-%{version}.tar.gzPrefix: /usrGroup: Development/Tools

    %descriptionThe GNU wget program downloads files from the Internet using the command-line.

    %prep%setup -q

    %build./configuremake

    http://www.ibm.com/developerworks/library/l-rpm1/#list2http://www.ibm.com/developerworks/library/l-rpm1/#list2http://www.ibm.com/developerworks/library/l-rpm1/#list2http://www.ibm.com/developerworks/library/l-rpm1/#list2http://www.ibm.com/developerworks/library/l-rpm1/#list3http://www.ibm.com/developerworks/library/l-rpm1/#list3http://www.ibm.com/developerworks/library/l-rpm1/#list3http://www.ibm.com/developerworks/library/l-rpm1/#list2http://www.ibm.com/developerworks/library/l-rpm1/#list2
  • 7/29/2019 Copy of CS2406

    15/83

  • 7/29/2019 Copy of CS2406

    16/8316

    However, while the target of the manual build was the actual /usr/local directory of your system,the target of the %install instruction is ~/mywget/BUILD.

    %files lists the files that should be bundled into the RPM and optionally sets permissions andother information. Within %files , you can use the %defattr macro to define the default

    permissions, owner, and group of files in the RPM; in this example, %defattr(-,root,root)installs all the files owned by root, using whatever permissions found when RPM bundled themup from the build system.

    You can include multiple files per line in %files . You can tag files by adding %doc or %config to the line. %doc tells RPM that the file is a documentation file, so that if a user installs the

    package using --excludedocs , the file is not be installed. %config tells RPM that this is aconfiguration file. During upgrades, RPM will attempt to avoid overwriting a user's carefullymodified configuration with an RPM-packaged default configuration file.

    Be aware that if you list a directory name under %files , RPM includes every file under that

    directory.

    Revving the RPM

    Now that your files are in place and your spec file is defined, you are ready to build the actualRPM file. To build it, use the aptly named rpmbuild utility:

    $ rpmbuild -v -bb --clean SPECS/wget.spec

    This command uses the named spec file to build a binary package ( -bb for "build binary") withverbose output ( -v ). The build utility removes the build tree after the packages are made ( --clean ). If you also wanted to build the source RPM, specify -ba ("build all") instead of -bb .(See the rpmbuild man page for a complete list of options.)

    rpmbuild performs these steps:

    Reads and parses the wget.spec file.

    Runs the %prep section to unpack the source code into a temporary directory. Here, thetemporary directory is BUILD. Runs the %build section to compile the code. Runs the %install section to install the code into directories on the build machine. Reads the list of files from the %files section, gathers them up, and creates a binary RPM

    (and source RPM files, if you elect).

    If you examine your $HOME/mywget directory, you should find a new directory named wget-1.12-root. This directory is the proxy for the target destination. You should also find a new

  • 7/29/2019 Copy of CS2406

    17/8317

    directory named RPMS/i386, which should in turn contain your RPM, named wget-1.12-1.i386.rpm. The name of the RPM reflects that this is wget version 1.12 for the i386 processor.

    To verify that the RPM contains the proper files, you can use the rpm command, as shown inListing 4 .

    Listing 4. Verifying the RPM contents

    $ rpm -Vp RPMS/i386/wget-1.12-1.i386.rpmmissing /usr/local/bin/wget.M....G. /usr/local/etcmissing c /usr/local/etc/wgetrc.M....G. /usr/local/sharemissing /usr/local/share/infomissing d /usr/local/share/info/wget. infomissing /usr/local/share/localemissing /usr/local/share/locale/be

    missing /usr/local/share/locale/be/LC_MESSAGESmissing d /usr/local/share/locale/be/LC_MESSAGES/wget.mo...

    The command rpm -Vp RPMS/i386/wget-1.12-1.i386.rpm verifies the package against thefiles on the system. Although there are seemingly lots of errors, each is a clue that the contents of the RPM file are correct. If you are expecting a file to be installed and it does not appear in theoutput, it was not included in the package. In that event, review the spec file and make sure thefile is enumerated in the %files section.

    After you've verified the RPM, you can distribute the file to coworkers. Once your colleaguesreceive the file, they should run rpm to install wget on their own systems:

    $ sudo rpm -i wget-1.12-1.i386.rpm

    Other uses for RPM

    This brief introduction merely scratches the surface of what's possible with RPM. Although it is

    most often used to install software and attendant files, you can package most anything, fromsystem scripts to source code to documentation. And as you'll see in the second installment of this series, you can also use RPM to patch source code as well as rebuild and reinstall software.The RPM distribution format is found on many Linux systems and is the preferred method toinstall binary software on Red Hat and Fedora systems, among others.

    If you build it and package it with RPM, they will come.

    http://www.ibm.com/developerworks/library/l-rpm1/#list4http://www.ibm.com/developerworks/library/l-rpm1/#list4http://www.ibm.com/developerworks/library/l-rpm1/#list4
  • 7/29/2019 Copy of CS2406

    18/8318

    Ex. No: 05Installing various software packages

    Date :

    Aim:

    Installing various software packages, either the package is yet to be installed or an older versionis existing. The student can practice installing the latest version. Of course, this might needinternet access. Install samba and share files to windows Install Common Unix Printing System(CUPS)

    Procedure:

    There are basically two ways to install software from the Fedora repositories; from the GUIusing PackageKit and from the command line using yum. Because the yum approach is simpler,throughout this document we describe that method. However, there are a number of details withrespect to yum that are useful to know, and some users feel more comfortable with a graphicaluser interface. Hence, this appendix

    A normal user on Linux can't install much software, you need to become the root user to gain therights to do so. This goes a long way towards protecting you from accidents (just about anythingthat does go wrong, you've done deliberately).

    Sometimes, you can temporarily get root-user rights by typing in the root password, when prompted, when you try to do something that requires those rights (such as picking systemadministration configuration items from a menu, or installing something directly from awebpage). Other times you open a command line terminal, and do it all in there, manually.

    You can see an example of the first situation by picking an administration item from the systemmenu. Try one, and then cancel the requester that pops up (i.e. do nothing to reconfigure your system, just look at how it asks you to enter a password).

    A screenshot of menus & sub-menus

  • 7/29/2019 Copy of CS2406

    19/83

  • 7/29/2019 Copy of CS2406

    20/8320

    A screenshot of installing pidgin in a terminal window

    If there's nothing to do, perhaps because it's already installed, it'll go through the motionsand drop back to the command line prompt, waiting for your next command. If it can't do it, you'll get some sort of message about why, and get dropped back at the

    command line prompt for you to enter your next command. If it can do it, you'll get some progress reports as it goes about its business, then you'll get

    dropped back to the command line prompt, at the end, for your next command.

    You can install several packages at once, just by listing them one after another (e.g. yum install pidgin firefox evolution ). And you can install software that you'vealready downloaded with a few variations on the prior instructions you use a localinstalloption with yum, instead of install, and you write the whole filename, inste ad of just the

    package name (e.g. yum localinstall pidgin-2.0.0-0.1.fc5.rpm ). You need to beworking in the directory that you saved the file to, or write the filepath (to where you saved it)along with the filename.

    Tip: You can use the tab key to finish some of your typing. e.g. Once I'd got as far as typing thefile name, I'd start typing in pidg and hit the tab key, and it'd complete the rest of the filename.

    Occasionally, you might encounter problems. Two common ones are not being able to use arepo, or not being able to find a package file that's needed. Yum will try to use several differentrepos, automatically, before giving up. You don't have to do anything to resolve this issue. If it

  • 7/29/2019 Copy of CS2406

    21/8321

    doesn't manage to get a response, or can't find some of the package files it needs, the simplestsolution is to try again, later on. You might have struck a time when the file repositories aren'tready for use.

    If yum gets stuck trying to use repo after repo, or one takes ages apparently doing nothing, you

    can break out of it by holding down the CTRL key and tapping the c key. You might have todo that several times.

    Installing Software with the GUI

    Launch the PackageKit application by selecting Administration->Add/Remove Software from the System menu:

    Figure A.1. Launching PackageKit

    There may be a delay before the controls on the window that appears may be used. This delaymay be brief or lengthy, depending on what you have done earlier. The data used by PackageKitis cached, and if it is stale, new data will be downloaded, which may take several minutes.

    Type in the name of the package you wish to install in the text box at the upper left, and selectthe Find button. There may be a delay, and you may need to click the Find button a second time.The package (perhaps with several others) will appear to the right. The closed box indicates thatthe package is not installed on your system.

  • 7/29/2019 Copy of CS2406

    22/8322

    Figure A.2. Searching for Software

    Click on the check box next to the package you wish to install. A blue plus sign will appear over the box indicating that is has been selected to install.

    Figure A.3. Selecting a package to install

    You may repeat the process if you wish to install additional packages. When you have selectedthe packages you wish, click the Apply button at the lower right to install the packages.Depending on how your system has been configured, you may be prompted to enter the

    password for the administrative user. If the package is unsigned, or is from a repository you havenot used before, you may be prompted for this password again. As a general rule, onlyadministrators may install software.

  • 7/29/2019 Copy of CS2406

    23/8323

    Figure A.4. Authorizing the installation

    Enter the root password and click Authenticate .The package will then be installed. When the installation is complete, the package wll be shownas an open box, indicating that the package is installed. For some applications, the application'sicon may be displayed instead of the open box. The Apply button will be disabled, indicatingthat there are no pending actions to apply.

    Figure A.5. Package is installed

    The Find button searches not only the name of the package, but also the package description.Some common words may reveal several packages, some perhaps not those intended. For example, if you typed in "circuit" with the intent of finding circuit design applications, you mayalso find applications referring to wired communications circuits as well as racing circuits!

  • 7/29/2019 Copy of CS2406

    24/8324

    Figure A.6. Searching the description

    Sometimes you may want to try several variations of a word or phrase. If, for example, you arelooking for an SSTV application, trying "slowscan" or "slow scan" will fail:

    Figure A.7. Searching the description - failure

    The developer of the slow scan application used the word "slow-scan" in his description. PackageKit is not smart enough to guess what you meant or what the original developer wasthinking.

    Figure A.8. Searching the description - success

  • 7/29/2019 Copy of CS2406

    25/8325

    SAMBA - Windows Linux file Sharing Notes

    Some basic notes on using SAMBA on a home network with linux and windows machines, likemy home network setup.

    Running SAMBA on a Linux machine

    To start, stop or restart samba,

    /etc/init.d/smd start/etc/init.d/smd stop/etc/init.d/smd restart

    All options for the SAMBA goes to /etc/samba/smb.conf This is my smb.conf

    Notable changes I made are:

    My linux machine is a gateway server with two ethernet interfaces eth0 and eth1. For securityreasons I set up SAMBA to run only on the internal interface

    interfaces = 192.168.0.1/24 127.0.0.1/24bind interfaces only = Yes

    Also set hosts allow to only allow internal and localhoist

    hosts allow = 127. 192.168.0.hosts deny = *

    SAMBA authentification

    For the file access permissions for the shares to work, you have to set up the SAMBAusername/password by running smbpasswd program on the linux box.

    Then on the Windows machine login with the username/password created above withsmbpasswd. Otherwise the file sharing will not work.

    SAMBA Linux to Windows Sharing

    Set up the shares in /etc/samba/smb.conf as follows

    [musak]comment = music from linux boxenpath = /var/stuff/media/musakread only = Yes

    On the windows machine access it as \\192.168.0.1\musak where 192.168.0.1 is the IP address of the linux machine.

    If you want to share CD/DVD drive

    http://www.hackorama.com/network/http://www.hackorama.com/network/http://www.hackorama.com/network/http://www.hackorama.com/network/smb.conf.htmlhttp://www.hackorama.com/network/smb.conf.htmlhttp://www.hackorama.com/network/smb.conf.htmlhttp://www.hackorama.com/network/smb.conf.htmlhttp://www.hackorama.com/network/smb.conf.htmlhttp://www.hackorama.com/network/smb.conf.htmlhttp://www.hackorama.com/network/smb.conf.htmlhttp://www.hackorama.com/network/smb.conf.htmlhttp://www.hackorama.com/network/
  • 7/29/2019 Copy of CS2406

    26/8326

    [dvd]comment = dvd drive on linux boxenwritable = Nolocking = Nopath = /mnt/cdrom2

    SAMBA Windows to Linux SharingFirst turn on file sharing. Go to Start->Setting->Control Panel and open "Network" and in thefirst tab called "Configuration" click on "File and Pring Sharing..." and om the Dialog Box thatcomes up check "I want to be able to give others access to my files"

    Now go to the file explorer and right click on the folder you want to sgare, and from the menuthat pops up slect "Sharing..." and from the "Manual Properties" Dialog Box that comes up selectthe second tab "Sharing" and select "Shared as" which will enable all the text boxes, fill in the"Share Name" "Comments" and selct "Read Only" "Full" or "Depends on Password" and enter a

    password. This is the password that will give access to this share. Also note the "Share Name",

    lets name it "MYSHARE".

    Also find out the hostname of the Windows machine. Go to Start->Setting->Control Panel andOpen "Network" and in the second tab "Identification" note the "Computer Name", This is thename to use from Linux to access shares on this machine. Let it be "MYWINBOX"

    Now go to the Linux machine. Crete mount point for the share.

    mkdir /mnt/winstuff

    Mount the share named "MYSHARE" on Windows hos namedt "MYWINBOX"smbmount \\MYWINBOX\MYSHARE /mnt/winstuff

    This will prompt you for the password for the share, enter the password and after that if you goto /mnt/winstuff you can access all files on the Windows folder.

    Printer Configuration Tool allows users to configure a printer. This tool helps maintain the printer configuration file, print spool directories, print filters, and printer classes.Fedora 12 uses the Common Unix Printing System (CUPS). If a system was upgraded from a

    previous Fedora version that used CUPS, the upgrade process preserves the configured queues.Using Printer Configuration Tool requires root privileges. To start the application, selectSystem (on the panel) > Administration >Printing , or type the command system-config-printer at a shell prompt.

  • 7/29/2019 Copy of CS2406

    27/8327

    Figure 23.1. Printer Configuration Tool

    The following types of print queues can be configured:AppSocket/HP JetDirect a printer connected directly to the network through HP

    JetDirect or Appsocket interface instead of a computer.

    Internet Printing Protocol (IPP) a printer that can be accessed over a TCP/IPnetwork via the Internet Printing Protocol (for example, a printer attached to another Fedorasystem running CUPS on the network).

    LPD/LPR Host or Printer a printer attached to a different UNIX system that can be

    accessed over a TCP/IP network (for example, a printer attached to another Fedora systemrunning LPD on the network).

    Networked Windows (SMB) a printer attached to a different system which is sharinga printer over an SMB network (for example, a printer attached to a Microsoft Windows machine).

    Networked JetDirect a printer connected directly to the network through HPJetDirect instead of a computer.

    ImportantIf you add a new print queue or modify an existing one, you must apply the changes for them to

    take effect.

    Clicking the Apply button prompts the printer daemon to restart with the changes you haveconfigured. Clicking the Revert button discards unapplied changes.

    23.1. Adding a Local PrinterTo add a local printer, such as one attached through a parallel port or USB port on your computer, click the New Printer button in the main Printer Configuration Tool window todisplay the window in Figure 23.2, Adding a Printer .

    http://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-addhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-addhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-addhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-addhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-addhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-addhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-addhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-add
  • 7/29/2019 Copy of CS2406

    28/8328

    Figure 23.2. Adding a Printer

    Click Forward to proceed.

    Enter a unique name for the printer in the Printer Name field. The printer name can containletters, numbers, dashes (-), and underscores (_); it must not contain any spaces. You can also usethe Description and Location fields to further distinguish this printer from others that may beconfigured on your system. Both of these fields are optional, and may contain spaces.

    Click Forward to open the New Printer dialogue (refer to Figure 23.3, Adding a LocalPrinter ). If the printer has been automatically detected, the printer model appears in SelectConnection . Select the printer model and click Forward to continue. If the device does not

    automatically appear, select the device to which the printer is connected (such as LPT #1 or Serial Port #1 ) in Select Connection .

    Figure 23.3. Adding a Local Printer

    Next, select the printer type. Refer to Section 23.5, Selecting the Printer Model and Finishing for details.

    http://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-localhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-localhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-localhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-localhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/s1-printing-select-model.htmlhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/s1-printing-select-model.htmlhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/s1-printing-select-model.htmlhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/s1-printing-select-model.htmlhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/s1-printing-select-model.htmlhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-localhttp://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-printing.html#fig-printconf-local
  • 7/29/2019 Copy of CS2406

    29/8329

    Ex. No: 06Write userspace drivers using fuse

    Date :

    Aim:

    GUI programming: a sample programme using Gambas since the students have VBknowledge. However, one should try using GTK or QT

    Procedure:

    Filesystem in Userspace (FUSE ) is a loadable kernel module for Unix-likecomputeroperatingsystems that lets non-privileged users create their own file systems without editing kernel code.This is achieved by running file system code in user space while the FUSE module provides onlya "bridge" to the actual kernel interfaces.

    Filesystem in Userspace

    A flow-chart diagram which shows how FUSEworks

    FUSE is a three-part system. The first of those parts is a kernel module which hooks into theVFS code and looks like a filesystem module. It also implements a special-purpose device whichcan be opened by a user-space process. It then spends its time accepting filesystem requests,translating them into its own protocol, and sending them out via the device interface. Responsesto requests come back from user space via the FUSE device, and are translated back into the

    form expected by the kernel.

    In user space, FUSE implements a library which manages communications with the kernelmodule. It accepts filesystem requests from the FUSE device and translates them into a set of function calls which look similar (but not identical) to the kernel's VFS interface. Thesefunctions have names like open() , read() , write() , rename() , symlink() , etc.

    Finally, there is a user-supplied component which actually implements the filesystem of interest.It fills a fuse_operations structure with pointers to its functions which implement the requiredoperations in whatever way makes sense. This interface is not well documented, but the example

    http://lwn.net/Articles/68106/http://lwn.net/Articles/68106/http://en.wikipedia.org/wiki/File:FUSE_structure.svghttp://lwn.net/Articles/68106/
  • 7/29/2019 Copy of CS2406

    30/8330

    filesystem provided with FUSE (which implements a simple sort of loopback filesystem) isreasonably easy to follow.

    FUSE Filesystem in User Space

    #rpm qa | grep fuse// checking file system

    Libconfuse using this we can develop fuse.

    #wget http://192.168.1.172/fosslab/samples/fuse.gz

    #tar zxvf fuse.gz // using for extracting

    #cd fuse

    #pwd/home/fosslab/fuse

    #ls

    Select fuse_tutorial.tgz

    #tar zxvf fuse_tutorial.tgz

    #firefox index.html

    #cd src

    #ls

    (src) #pkg-config fuse-_cflags

    -D FILE_OFFSET_BITS=64 I/usr/include/fuse

    #pkg-config fuse libs

    `-pthread L/lib lfuse lrt ldl

    #make // we will get bbfs obj file in green color

    Make: bbfs is up to date

    #cd .../example/

    #ls l..

    http://lwn.net/Articles/68106/http://lwn.net/Articles/68106/http://192.168.1.172/fosslab/samples/fuse.gzhttp://192.168.1.172/fosslab/samples/fuse.gzhttp://192.168.1.172/fosslab/samples/fuse.gzhttp://192.168.1.172/fosslab/samples/fuse.gzhttp://lwn.net/Articles/68106/
  • 7/29/2019 Copy of CS2406

    31/8331

    Eg dir

    #ls rootdir/

    #ls mountdir/

    #../src/bbfs rootdir/ mountdir/

    About to call fuse_main

    #fusermount u mountdir

    #mount...

    .#ll rootdir/

    #ll mountdir/

    #touch rootdit/myfile.txt

    #ll mountdir/

  • 7/29/2019 Copy of CS2406

    32/8332

    Ex. No: 07GUI Programming

    Date :

    Aim:

    GUI programming: a sample programme using Gambas since the students have VBknowledge. However, one should try using GTK or QT

    Procedure:

    Gambas is a GUI-based programming environment that brings the ease and charm of earlyversions of Visual Basic into the Linux environment.

    Gambas programming

    Now we need to make the program DO something. Let's start with the menus. Click on the"Quit" entry in the File menu and a text area should appear with the cursor in the middle of a pre-named subroutine called PUBLIC SUB Quit_Click() . Whatever code we put in here will runwhen the user clicks the Quit item (easy, isn't it?). Enter the following code:

    FMain.Close

    and save the project. Run the project now and the Quit menu option should close the form for us.

    Gambas Basic is an object-oriented language, which means that all our controls, including theform itself, carry baggage around with them in the form of properties and methods. Propertiesare settings that we can change, while methods are things that can be made to happen. One of thethings that can be made to happen to the form is closing, and we just made it happen. Object-oriented programming can either be incredibly easy or horrendously difficult, depending on howfar you want to go with it, but friendly user interfaces like this would be virtually impossiblewithout it.

    Add the following code to the other menu items:

    Clear -- TextArea1.Text = " " Copy -- Clipboard .Copy(TextArea1.Text) Paste --TextArea1.Text = Clipboard.Paste() In this case, Text is a property of the control calledTextArea1, and Copy and Paste are methods associated with the system object Clipboard.Run the program and check that your code works so far.

    Programming the word count

    Programming our word count button is going to be a little more difficult, but not much. If youknow some Basic or Visual Basic already you will find that most of it works in a predictable waywithin Gambas. If not, check the Web or hunt up a good book. There are a variety of ways wecan get Basic to count the words in our box, but for the sake of simplicity I will do it by runningthrough all the text one character at a time, counting the number of spaces, and adding one. Theresult won't be perfect, but it should be close enough. Click on the Count Words! button and

  • 7/29/2019 Copy of CS2406

    33/8333

    enter the following code after PUBLIC SUB Button1_Click() . Gambas will take care of thespaces and capitalization.

    DIM i, w AS Integer

    FOR i = 1 TO Len(TextArea1.Text)

    IF Mid(TextArea1.Text, i, 1) = " " THEN

    w = w + 1

    NEXT

    Label1.Text = w + 1

    Here we are defining two variables as integers (whole numbers) and calling them i and w. As

    newly defined variables they start off with a value of zero. The Len() function gives us thenumber of characters in our text area, and the Mid() function pulls out each new character inturn. If it is a space character, the variable w is increased by one. Having looked through all thecharacters, we add 1 to the value of w for the last word (which isn't followed by a space) and putthe final value in the label.Check if your program is working, make any corrections that arenecessary, and save it.

    Packaging and distribution

    At this stage we have a working Gambas project file, but the only people who can use it are thosewho already have Gambas installed. That leaves out a lot of users! To make your program more

    portable for distribution, there are two steps you can carry out:

    1. Make an executable file2. Package the file for distribution as a .deb

    To make an executable file, go to the Project menu and select "Make Executable." Within a fewseconds this will create a file called XXX.gambas, where XXX is your project name, inside your

    project directory. This can be copied and distributed to anyone who has a runtime version of Gambas installed, which includes by default most of the major Linux distributions. A secondoption, "Project/Make Source Archive' will just bundle together all the files associated with the

    program into a single compressed .gz file.

    To make the application more portable still, it can be bundled into an installation package."Project/Make Installation Package" gives the user the option of making installation packages for any or all of the major distros: Debian, Fedora, Mandriva, OpenSUSE, and of course Ubuntu. Awizard then takes the user through the necessary steps: the creator's name and email address areadded, the appropriate group is chosen for repository storage, and a default Applications menucategory and position is specified for each distro package. Gambas then compiles the necessaryfiles into a distributable which can be uploaded to a repository.

  • 7/29/2019 Copy of CS2406

    34/8334

    Packaging and distribution

    What next?

    Now you know how to do simple Gambas programming, what next? There are any number of easy projects you can cut your teeth on. Unfortunately Gambas support on the web is a littlesparse, and the built-in help is little more than a list of commands, but a Google search for 'Visual Basic' will bring up many tutorials and books that you should be able to adapt to Gambas

    relatively easily. Later, when you want to extend your skills, here are some topics to look into: Saving and opening text files Graphics -- using Gambas to make patterns and designs in a drawing area Connecting with and modifying a mySQL database Control groups and arrays: connecting one piece of code with many different controls Multimedia: using Gambas to create sound, music and video players Games: from simple text games with random numbers and a timer through to complex

    movements and designs.

    http://www.brighthub.com/guides/mysql.aspxhttp://www.brighthub.com/guides/mysql.aspxhttp://www.brighthub.com/guides/mysql.aspxhttp://www.brighthub.com/computing/linux/articles/31624/image/50521/http://www.brighthub.com/computing/linux/articles/31624/image/50520/http://www.brighthub.com/guides/mysql.aspx
  • 7/29/2019 Copy of CS2406

    35/8335

    Ex. No: 08Version Control System setup and usage using RCS, CVS, SVN Date :

    Aim:

    Version Control System setup and usage using RCS, CVS, SVN

    Procedure:

    Keeping a version history of files under /etc is essential for maintaining a healthy system. The benefits of tracking changes to /etc include:

    Documentation: The log messages that are attached to configuration changes serve asdocumentation. These log messages record who made the change, when and why. Understandingthe contents of a config file becomes much easier if you have a full history of the changes thatwere made to this file.

    Troubleshooting: Misconfiguration can result in a variety of problems. When a servicestarts to misbehave, one of the things you can do to troubleshoot the issue is to check the versionhistory of its config file. There, you can see if any changes were made around the time frame inwhich the problem happened. If you spot a change that may be causing the issue, you can easilyrevert it to fix the problem.

    You can set up your own repository to track changes to /etc , or you can use a tool calledetckeeper to handle the setup for you. This tool supports multiple version control systems,including Git, Mercurial and Bazaar. It integrates with the package management systems of anumber of Linux distros, including APT (used by Debian, Ubuntu), YUM (RedHat, CentOS,Fedora), Pacman (Arch Linux). Using etckeeper instead of rolling your own has some

    advantages: etckeeper integration with package managers means than you don't need to manually

    commit changes in /etc after installing packages. etckeeper comes pre-configured with a list of files that live in /etc but usually do not

    benefit from version control (like some cache files).

    Installing etckeeper

    To install etckeeper on Debian or Ubuntu, run:

    $ sudo apt-get install etckeeper

    If you use another Linux distro, search the packcage list in your package manager. If etckeeper supports your system, you will probably find it there. Otherwise, you can download the sourcefrom the official site of etckeeper .

    Configuring etckeeper

    Next, let's configure etckeeper. Open /etc/etckeeper/etckeeper.conf in your favorite editor.The first option that you need to look at is VCS, which is the version control system you want to

    http://kitenet.net/~joey/code/etckeeper/http://kitenet.net/~joey/code/etckeeper/http://kitenet.net/~joey/code/etckeeper/http://kitenet.net/~joey/code/etckeeper/http://kitenet.net/~joey/code/etckeeper/http://kitenet.net/~joey/code/etckeeper/http://kitenet.net/~joey/code/etckeeper/
  • 7/29/2019 Copy of CS2406

    36/8336

    use. By default it's set to git , but you can change it to hg or bzr depending on your preference. Iuse git myself, but most of this article should apply to etckeeper regardless of the version controlsystem you choose.

    Another option that I recommend looking at is AVOID_COMMIT_BEFORE_INSTALL . By default,

    etckeeper will automatically commit any pending changes when you install packages. I find this behavior undesirable, as it may commit unfinished changes. I disable it by settingAVOID_COMMIT_BEFORE_INSTALL to 1 .

    If you installed etckeeper from source, have a look at HIGHLEVEL_PACKAGE_MANAGERandLOWLEVEL_PACKAGE_MANAGERand change them depending on the package manager of your system.

    Using etckeeper

    With this, we are done with configuring etckeeper. Let's create a repository now:

    $ cd /etc$ sudo etckeeper init$ sudo etckeeper commit "Initial import"

    This will create an empty repository and then commit the contents of your /etc directory to it.

    From here, using etckeeper is not different from using the version control system you selected.Let's say you want to change your MySQL config; you can edit the file and then commit it asusual:

    $ cd /etc$ sudo vi /etc/mysql/my.cnf$ sudo git commit -m "Commit message"

  • 7/29/2019 Copy of CS2406

    37/8337

    Ex. No: 09Text processing with Perl

    Date :

    Aim:

    Text processing with Perl: simple programs, connecting with database e.g., MYSQL

    Procedure:

    1. Sample PERL #!usr/bin/perl # the above line is shebang directive

    $name=;

    chomp($name);

    print "$name\n";

    Output:[linuxpert@localhost ~]$ perl first.pl

    thamarai

    thamarai

    2. Scalar variables in PERL#!/usr/bin/perl

    my $animal="Camel"; # this variable is lexically scoped ie local

    my $ans=42_243; #this is similar to 42243

    print "$animal\n";

    print "$ans\n";

    print "The square of &ans",$ans*$ans,"\n";

    Output:

    [linuxpert@localhost ~]$ perl scalar.pl

    Camel

    42243

    80

    3. Array in PERL#!usr/bin/perl

    my @animal=("cow","Buffalo","Camel");

    print "@animal\n"; # list all elements in array

    print "$#animal\n"; # list last element position

  • 7/29/2019 Copy of CS2406

    38/8338

    print "$animal[0]\n"; #list 0th position element

    $count=@animal;

    print "$count"; # count no of elements in array

    Output:

    [linuxpert@localhost ~]$ perl array.pl

    cow Buffalo Camel

    2

    cow

    3

    4. Hash in PERL #!usr/bin/perl

    %color=('apple'=>"red",'banana'=>"yellow",'orange'=>"orange");

    print "$color{'apple'}\n";

    Output:

    [linuxpert@localhost ~]$ perl hash.pl

    red

    81

    5. To display all values in hash#!usr/bin/perl

    %color=('apple'=>"red",'banana'=>"yellow",'orange'=>"orange");

    print "$color{'apple'}\n";

    # to display all the values in hash

    @keys=keys %color;

    foreach $key (@keys)

    {

    print "$color{$key}\n";

    }

    Output:

    [linuxpert@localhost ~]$ perl hashvalues.pl

    red

    yellow

  • 7/29/2019 Copy of CS2406

    39/8339

    red

    orange

    6. for deletion of Hash values #!usr/bin/perl

    %color=('apple'=>"red",'banana'=>"yellow",'orange'=>"orange");

    print "$color{'apple'}\n";

    # to display all the values in hash

    @keys=keys %color;

    82

    @values=values %color; # it ill store the array values

    foreach $key (@keys)

    {

    print "$color{$key}\n";

    }

    delete $color{'apple'}; # to remove specific key

    7. If loop in perl#!usr/bin/perl

    my $a=10;

    $condition=1;

    if($condition)

    {

    my $y=100;

    print "$a\n";

    print "$y\n";

    } print "$a\n";

    print "$y\n";

  • 7/29/2019 Copy of CS2406

    40/8340

    Output:

    [linuxpert@localhost ~]$ perl ifloop.pl

    10

    100

    10

    83

    8. If not loop (unless)in perl

    #!usr/bin/perl

    $a=5;

    unless($a>10) #is equal to if not

    {

    print "a less than 10";

    }

    Output:

    [linuxpert@localhost ~]$ perl unless.pl

    a less than 10

    9. While loop (until) in perl

    #!usr/bin/perl

    $a=0;

    until($a>10) #is equal to while

    {

    print "$a\n";

    $a++;

    }

    Output:

    [linuxpert@localhost ~]$ perl unless.pla less than 10[linuxpert@localhost ~]$ perl until.pl018423456

  • 7/29/2019 Copy of CS2406

    41/8341

    78910

    10. For each loop (upper limit is not fixed )in pe rl#!usr/bin/perl

    my @animals=("cow","buffalo","camel",123,100,243,300);

    foreach $key(@animals)

    {

    print "$key\n";

    }

    Output:[linuxpert@localhost ~]$ perl foreach.pl

    cow

    buffalo

    camel

    123

    100

    243

    300

    85

    11. String operation:

    #!usr/bin/perl

    $a="hello";

    $b="world";

    print $a.$b,"\n";

    $str="-";

    print $str x 80,"/n";

    @a=(10..25);

    print "@a\n";

    Output:

    [linuxpert@localhost ~]$ perl string.pl

    helloworld

  • 7/29/2019 Copy of CS2406

    42/8342

    --------------------------------------------------------------------------------/n10 11 12 13 14 15 16 17 18

    19 20 21 22 23 24 25

    12. Hard reference variable: 12.1 Reference variable (scalar)

    #!usr/bin/perl

    $a=100;

    $b=\$a;

    print "$b\n";

    Output:

    [linuxpert@localhost ~]$ perl reference.plSCALAR(0x9506a68)

    86

    12.2 De reference the variable(scalar variable)

    #!usr/bin/perl

    $a=100;

    $b=\$a;

    print "$$b\n";

    Output:

    [linuxpert@localhost ~]$ perl reference1.pl

    100

    12.3 Array reference variable

    #!usr/bin/perl

    @arr=('hello','122','200','thamarai');

    $c=\@arr;

    print "$c\n"; # print the address

    print "@$c\n"; # print the values of array

    print "$c->[1]\n"; #point the specified location in array, to access the single element

    Output:

    [linuxpert@localhost ~]$ perl arrayvariable.pl

  • 7/29/2019 Copy of CS2406

    43/8343

    ARRAY(0x8358a70)

    hello 122 200 thamarai

    122

    87

    12.4 Hash variable in perl

    #!usr/bin/perl

    %color=('apple'=>"red",'banana'=>"yellow",'orange'=>"orange");

    $h=\%color;

    print "$h\n";#print the reference address

    @keys=keys %$h;

    print "@keys\n"; #it contains the key value

    print "$h->{'apple'}\n"; # it print the the apple alone

    Output:

    [linuxpert@localhost ~]$ perl hashvariable.pl

    HASH(0x830da70)

    banana apple orange

    red

    12.5 Magic variable in perl

    #!usr/bin/perl

    %color=('apple'=>"red",'banana'=>"yellow",'orange'=>"orange");

    $h=\%color;

    print "$h\n";#print the reference address

    @keys=keys %$h;

    print "$h->{'apple'}\n"; # it print the the apple alone

    foreach (@keys)

    {

    print "$h->($_}\n"; # to display all key values we use $_ instead of keys

    }

    88

  • 7/29/2019 Copy of CS2406

    44/8344

    Output:

    [linuxpert@localhost ~]$ perl hashvariable1.pl

    HASH(0x8df4a70)

    red

    HASH(0x8df4a70)->(banana}

    HASH(0x8df4a70)->(apple}

    HASH(0x8df4a70)->(orange}

    13 .Function

    #!usr/bin/perl

    sub sayHello()

    {

    print "Hello\n";

    }

    &sayHello();

    Output:

    [linuxpert@localhost ~]$ perl function.pl

    Hello

    89

    13.1 function with parameter

    #!usr/bin/perl

    sub add()

    {

    my ($a,$b)=@_; #the values will be stored in @_ array variable for dynamically

    print $a+$b;

    }

    &add(10,15);Output:

    [linuxpert@localhost ~]$ perl functionparam.pl

    25

  • 7/29/2019 Copy of CS2406

    45/8345

    13.2 Find Maximum value using function

    #!usr/bin/perl

    sub max()

    {

    $max=shift @_; # shift function remove the first element in array and the remaining element will

    be stored in $max

    foreach $v (@_) # $v is the second values in the @_

    {

    90

    $max=$v if $max

  • 7/29/2019 Copy of CS2406

    46/8346

    Output:

    91

    [linuxpert@localhost ~]$ perl symbolicref.pl

    the product is 600

    14. Program to check greater among 3 numbers

    #Greatest among 3 number

    print "Enter A value : ";

    $a=;

    print "Enter b value : ";

    $b=;

    print "Enter c value : ";$c=;

    if(($a > $b)&&($a > $c))

    {

    print "A is greater";

    }

    elsif(($b > $c)&&($b > $a))

    {

    print "B is greater";

    }

    else

    {

    print "C is greater";

    }

    92

  • 7/29/2019 Copy of CS2406

    47/8347

    15.Program to get details in run time

    #! usr/bin/perl

    print "Enter the Student Information";

    print "\n enter name";

    chomp($name = );

    print "\nenter dob";

    chomp($dob=);

    print "\n enter registerno.";

    chomp($registern0.=);

    print "\n enter education";

    chomp($Education=);

    print "\n Student Information";

    print "\n Name: $name";

    print "\n Date Of Birth :$dob";

    print "\n Register no.:$registern0.";

    print "\nQualification :$Education";

    16. Program to perform array releted function

    #!/usr/bin/perl

    # Define an array

    @coins = ("aaa","bbb","ccc");

    #Array can be represent using qw format

    #Display all Elements in Array

    93

    print "Array Elements";

    print "\n First Statement : @coins";

    print "\n";# Add elements at the end of the array

    print "\nPush Function";

    push(@coins,qw/ddd eee fff ggg/);

    push(@coins, "Penny","priya");

    print "\n Second Statement : @coins";

    print "\n";

  • 7/29/2019 Copy of CS2406

    48/8348

    # Add one element at the beginning of the array

    print "\n Unshift Function";

    unshift(@coins, "Dollar");

    print "\n Third Statement : @coins";

    print "\n";

    # Remove one element from the last of the array.

    print "\n Pop Function";

    pop(@coins);

    print "\nFourth Statement : @coins";

    print "\n";

    # Remove one element from the beginning of the array.

    print "\n Shift Function";shift(@coins);

    print "\nFifth Statement : @coins";

    print "\n";

    print "@coins";

    #splice function,you can delete or replace elements within the array.

    #delete one element at index 1

    94

    print "\n Using Splice Function";

    @browser = ("System", "Logic", "Solutions");

    print "\n @browser";

    print "\n";

    #replacing elements at index 1,2

    @browser = ("System", "Logic", "Solutions");

    splice(@browser, 1, 2, "Helios", "Matheson");

    print "\n @browser";

  • 7/29/2019 Copy of CS2406

    49/8349

    17. Program to perform Sub-routine to check biggest number

    Example 1:

    sub maximum

    {

    if ($_[0] > $_[1])

    {

    print $_[0];

    }

    else

    {

    print $_[1];

    }

    }

    $biggest = &maximum(37, 24);

    Example 2:

    sub maximum

    95

    {

    if ($_[0] > $_[1]){

    $

    _

    [

    0

    ]

    ;

    }

    e

    l

    s

    e

    {

    $_[1];

    }

    }

  • 7/29/2019 Copy of CS2406

    50/83

    50

    $biggest = &maximum(37, 24);

    $biggest2 =&maximum(100,123);

    print "\nThe biggest amomg 37 and 24 is $biggest";

    print "\nthe biggest among 100 and 123 is $biggest2";

    Example3: Program to return the values

    sub get_list

    {

    return 5..10;

    }

    @list = &get_list;

    print "@list";96

    18. Program to Open the File

    # < indicate that file has to be opened in read-only mode

    # > sign indicates that file has to be opened in writing mode

    EntitiesDefinition

    < or rRead Only Access

    > or wCreates, Writes, and Truncates

    >> or aWrites, Appends, and Creates

    +< or r+Reads and Writes

    +> or w+Reads, Writes, Creates, and Truncates

    +>> or a+Reads, Writes, Appends, and Creates

    Sample: #!/usr/bin/perl

    open(DATA, "

  • 7/29/2019 Copy of CS2406

    51/83

    51

    19. Example which opens an existing file file1.txt and read it line by line andgenerate another copy file2.txt #!/usr/bin/perl

    # Open file to read

    open(DATA1, "file2.txt");

    # Copy data from one file to another.

    while()

    {

    print DATA2 $_;

    }

    close( DATA1 );

    close( DATA2 );

    #delete a file file3.txt using unlink function

    unlink ("/strawberry/perl/file3.txt");

    20.Program to returns the key intersection of two hashes

    sub intersection

    {

    my ($hasha, $hashb) = @_;

    98

    my %newhash;

    foreach my $key (keys %{$hasha})

    {

    $newhash{$key} = $$hasha{$key} if (exists $$hashb{$key});

    }

    return %newhash;}

    %hasha = ('a' => 'b',

    'b' => 'b',

    'c' => 'b');

    print %hasha;

  • 7/29/2019 Copy of CS2406

    52/83

    52

    print "\n";

    %hashb = ('b' => 'b',

    'c' => 'b',

    'd' => 'b');

    print %hashb;

    print "\n";

    %newhash = intersection(\%hasha, \%hashb);

    print "\n";

    print %newhash;

    21. Print out lines of other files with the use of arrays

    #!/usr/bin/perl print "content-type: text/html \n\n"; #The header

    $text = "sample.txt";

    open (text) or die "Can't open the file!";

    99

    @fileinput = ;

    print $fileinput[0];

    print $fileinput[1];

    print $fileinput[2];

    print $fileinput[3];

    print $fileinput[4];

    print $fileinput[5];

    print $fileinput[6];

    print $fileinput[7];

    print $fileinput[8];

    print $fileinput[9];

    close (text);

  • 7/29/2019 Copy of CS2406

    53/83

    53

    22. PERL - Copying File

    We can duplicate a file using the copy function. Copy takes two values the URL of the file to be

    copied and the URL of the new file. Since we want to duplicate the file in this example

    22.1 Example for copying and moving and delete function

    #!/usr/bin/perl

    use File::Copy;

    print "\n Copying the content present from file1 to file2";

    $filetobecopied = "file1.txt";

    $newfile = "file2.txt";

    copy($filetobecopied, $newfile) or die "File cannot be copied.";

    print "\n Now open the new file";

    print "\n Move function";100

    $oldlocation = "sample.txt";

    $newlocation = "f:/keerthi/sample.txt";

    move($oldlocation, $newlocation);

    # unlink function to delete specific files from your web server.

    print "\n Delete function";

    $file = "F:/keerthi/sample.txt";

    if (unlink($file) == 0)

    {

    print "\nFile deleted successfully.";

    }

    else

    {

    print "\nFile was not deleted.";

    }

    101

  • 7/29/2019 Copy of CS2406

    54/83

    54

    APPLICATION USING PERL

    1. Create the table in perl that will be automatically updated in database using mysql

    1.1 First create the database as follows in mysql

    [linuxpert@localhost ~]$ mysql -u root -p

    Enter password:

    Welcome to the MySQL monitor. Commands end with ; or \g.

    Your MySQL connection id is 2

    Server version: 5.1.45 Source distribution

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql> show databases;

    +--------------------+| Database |

    +--------------------+

    | information_schema |

    | mysql |

    | test |

    102

    +--------------------+

    3 rows in set (0.25 sec)

    mysql> create database student;

    Query OK, 1 row affected (0.02 sec)

    mysql> show databases;

    +--------------------+

    | Database |

    +--------------------+

    | information_schema |

    | mysql |

    | student |

    | test |

    +--------------------+

  • 7/29/2019 Copy of CS2406

    55/83

    55

    4 rows in set (0.00 sec)

    mysql> use student;

    Database changed

    mysql> connect;

    Connection id: 3

    Current database: student

    mysql> show tables;

    Empty set (0.00 sec)

    now the table is empty . To create the table we use the following procedure

    103

    1.2 The following packages should be used to connect PERL with Mysql (use new terminal) [linuxpert@localhost ~]$ rpm -q perl-DBI

    perl-DBI-1.609-4.fc13.i686

    [linuxpert@localhost ~]$ rpm -q perl-DBD-MySQL

    perl-DBD-MySQL-4.013-3.fc13.i686

    1.3 Write the PERL script to connect with mysql as follows

    #!usr/bin/perl

    use DBI; #to use the build in package we use "Use", DBI is the build in package in perl

    my $dbh=DBI->connect("dbi:mysql:student","root",""); #connect to database

    if(!$dbh)

    {

    die("error:$!");

    }

    $sth=$dbh->prepare("create table students(rollno int,sname varchar(50))"); # create the table

    $sth->execute();104

    $dbh->disconnect;

  • 7/29/2019 Copy of CS2406

    56/83

    56

    1.4 Run the Perl script

    [linuxpert@localhost ~]$ perl connect.pl

    now see the tables in database (student)

    mysql> show tables;

    +-------------------+

    | Tables_in_student |

    +-------------------+

    | students |

    +-------------------+

    1 row in set (0.00 sec)

    2. insert the values in perl that will be automatically updated in database using mysql as

    follows #!usr/bin/perl

    use DBI; #to use the build in package we use "Use", DBI is the build in package in perl

    my $dbh=DBI->connect("dbi:mysql:student","root",""); #connect to database

    if(!$dbh)

    {

    105

    die("error:$!");

    }

    $sth=$dbh->prepare("insert into students values(100,'thamarai')"); # create the table

    $sth->execute();

    $dbh->disconnect;

    2.1 compile the perl

    [linuxpert@localhost ~]$ perl dbinsert.pl

    now the output is

    mysql> select * from students;+--------+----------+

    | rollno | sname |

    +--------+----------+

    | 100 | thamarai |

  • 7/29/2019 Copy of CS2406

    57/83

    57

    +--------+----------+

    1 row in set (0.00 sec)

    2.2 insert the values in perl using execute statement

    #!usr/bin/perl

    use DBI; #to use the build in package we use "Use", DBI is the build in package in perl

    $rollno=200;

    106

    $sname="selvi";

    my $dbh=DBI->connect("dbi:mysql:student","root",""); #connect to database

    if(!$dbh)

    {

    die("error:$!");}

    $sth=$dbh->prepare("insert into students values(?,?)"); # create the table

    $sth->execute($rollno,$sname);

    $dbh->disconnect;

    compile the program as

    [linuxpert@localhost ~]$ perl dbinsert1.pl

    now the output is

    mysql> select * from students;

    +--------+----------+| rollno | sname |+--------+----------+| 100 | thamarai || 200 | selvi |+--------+----------+2 rows in set (0.08 sec)

    107

  • 7/29/2019 Copy of CS2406

    58/83

    58

    CGI PROGRAMMING

    EXAMPLE -1

    Type the following in terminal

    [linuxpert@localhost ~]$ su

    Password:

    [root@localhost linuxpert]# cd /var/www/cgi-bin

    [root@localhost cgi-bin]# gedit

    Type the following in gedit

    #!/usr/bin/perl

    use CGI;

    $cgi=new CGI;

    print $cgi->header,$cgi->start_html,

    $cgi->h1("A simple Example"),

    $cgi->end_html;

    Type the following in the same terminal

    [root@localhost cgi-bin]# chmod +x form.cgi

    Go to browser and type the following URL

    http://localhost/cgi-bin/form.cgi

    U will get the output as

  • 7/29/2019 Copy of CS2406

    59/83

    59

    EXAMPLE -2

    In terminal

    [linuxpert@localhost ~]$ su

    Password:

    [root@localhost linuxpert]# cd /var/www/html

    [root@localhost html]# gedit

    Type the following in gedit editor

    LOGIN

    "Enter student roll no"

    "enter the student name"

    "click here to submit"

    Save the page as form.html & close it.Type the following URL in browser

    http://localhost/form.html

    Then type the following in the terminal

    [root@localhost html]# cd /var/www/cgi-bin

    [root@localhost cgi-bin]# gedit

    Type the following in gedit

    #!/usr/bin/perluse CGI;

    $cgi=new CGI;

    use DBI;

    $rollno=$cgi->param('rollno');

    $name=$cgi->param('sname');

    http://localhost/form.htmlhttp://localhost/form.htmlhttp://localhost/form.html
  • 7/29/2019 Copy of CS2406

    60/83

    60

    my $dbh=DBI->connect("dbi:mysql:student","root","");

    my $sth=$dbh->prepare("insert into students values(?,?)");

    $res=$sth->execute($rollno,$name);

    $dbh->disconnect;

    if($res)

    {

    print $cgi->header,

    $cgi->start_html,

    $cgi->h1("Record created"),

    $cgi->end_html;

    }

    Save that file as form2.cgiClose that file & open a same terminal type as

    [root@localhost cgi-bin]# chmod +x form2.cgi

    In new browser type the following

    http://localhost/form.html

    we get the screen as

  • 7/29/2019 Copy of CS2406

    61/83

    61

    Now check the database as mysql> select * from students;+--------+---------------+| rollno | sname |+--------+---------------+| 100 | thamarai || 200 | selvi || NULL | abcd || 71 | thamaraiselvi |+--------+---------------+4 rows in set (0.00 sec)

  • 7/29/2019 Copy of CS2406

    62/83

    62

    Ex. No: 10Running PHP

    Date :

    Aim:Running PHP: simple applications like login forms after setting up a LAMP stack

    Procedure:

    CONNECT PHP WITH MYSQL[linuxpert@localhost ~]$ su

    Password: admin123

    [root@localhost linuxpert]# cd /var/www/html

    [root@localhost html]# gedit

    Type the following in gedit and save it as form.html

    LOGIN

    "Enter course no"

    "enter the coursename"

    "click here to submit"

    59

    Type the following in gedit and save it as connect.php

  • 7/29/2019 Copy of CS2406

    63/83

    63

    echo "selected database";

    $str="insert into courses values($cid,'$cname')";

    $res=@mysql_query($str) or die(mysql_error());

    if($res>0)

    {

    echo "Record created";

    }

    ?>

    TYPE THE FOLLOWING IN THE BROWSER

    http://localhost/form.html

    When u press the submit button

    http://localhost/form.htmlhttp://localhost/form.htmlhtt