CSCE 2004 - Laboratory Assignment 9

    The objective of this lab is to help students familiarize more with the Linux environment and practice some common commands. This lab assignment has the following steps:

    Part 1 - Linux commands

    1. Clean up your directory
    2. Login to a linux machine. Start a Console window. You should be prompted with a window containing the following:

      username@turing:~$

      When you first login, your current working directory is your home directory. Your home directory has the same name as your username, and it is where your personal files and subdirectories are saved. Your home directory can also be referred to by the tilde ~ character. So typing:

      ~$ cd ~

      will move you to your home directory.

      Exercise:The hierarchy that you should have in your home directory is as follows:

      
        |--2004
          |--labs
            |--lab1
            |--lab2
            .
            .
            .
          |--homeworks
            |--hw1
            |--hw2
            |--hw3
      

      Checkout the hierarchy of your files.

      If you have a hierarchy other than the above hierarchy make the necessary changes using the commands we learned in previous labs (ls, mv, cp, mkdir, rm). Check out the man pages or previous labs if you need a review.

      Copy and paste the result of the command ls -R after you have tidied up your files so they match this folder hierarchy.

    3. Redirecting input to a file and from a file.
    4. In Linux, to redirect the output to a file, you can use the > character like this: ls > file_name. The above redirects the output of the ls command to a file called file_name. Because the output is redirected to a file, you don't see any results of ls on your screen.

      Now, in your home directory, do ls -alt > myfiles. This should create a new file called myfiles that contains all files in your directory, including several you have never seen before. Type: more myfiles to see this list. The a flag shows all files, including hidden files. In linux, hidden files and directories begin with a period. These files are not normally displayed when you do an ls. The l flag to ls displays a long listing for each file, will details like the file permissions, creation time, and size. Finally, the t flag sorts the files by time with the most recently modified file first.

      Copy and paste the contents of myfiles into the text box below.

    5. Redirecting input and output to/from a program
    6. To work in this section write a simple c++ program that will read two numbers as inputs and it will then calculate and print the sum of those two numbers. Compile the program and name the executable file sum.exe.

      Sometimes you will need to do some processing on the information a specific program is going to generate. To have the output of a program redirected to some file, do the following:

      ./sum.exe > results.txt

      The previous example will type the results of the program in the results.txt file.

      Also, you can read the inputs from a data file. Create an ascii file called data.txt using nano and store the values 2 and 4 in the file. Now execute the following command:

      ./sum.exe < data.txt

      What Happened?

      Finally, you can combine the previous two steps into one to make the program reads it's inputs from a file and store the output in another file:

      ./sum.exe < data.txt > results.txt

      The previous approach is used to capture the output of a program for later analysis. Also, if the program takes a lot of inputs and the user wants to run the program to many times, he/she can read the values from a file which will save time and effort.<\p>

    7. Script
    8. The script command is usually used to capture what the user does in a specific period of time. The capture of screen will start the moment the user types script and will continue until the user types Cntl D. When the user types Cntl D, the screen capture will be saved in a file called typescript.

      To work in this section you will need to use the previously compiled program sum.exe.

      script
      ls
      ./sum.exe
      ./sum.exe
      ls
      Ctrl D
      more typescript
      

      The Ctrl D in the above sequence means to press the control key and the D key on the keyboard simultaneously, the same motion as when you hit shift D to type an uppercase letter.

      Copy the content of the typescript file into the text box below.

    9. Grep
    10. The grep command is widely used to search for some strings in files or in the output of some other commands.

      Try the following series of command and observe what the output looks like:

      cd ~
      ls -lR | grep cpp
      

      What did you see? Type your output in the textbox below.

      Now try to look for string patterns in a file. Create your file called text using the nano editor and store some text in the file. Make sure to add your name in the file. Make sure to include more than one line of text in the file.

      Type command grep 'your name' text and print your output in the text box below

    11. Access Permission
    12. Before starting this section, create a new directory called test, and inside that directory create a file called information.txt by just typing something (anything) in using nano.

      • Access Rights on Files
        Permissions are useful to a user to protect a file from being corrupted or destroyed. The permission given to a file exist on three actions:

        r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to read and copy the file.

        w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to change a file.

        x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file, where appropriate.

        Furthermore, each action can be assigned to a specific group of users - the current user (u), everyone in your group (g), and/or everyone on the network (a). For example, go to the folder test and run the command:

        ls -l Information.txt

        A complete permission set looks as follows:

        -rwxrw-r-- 1 Information.txt

        If the object is a file the first position will be a '-'. If it is a folder it will be a 'd'. The 'rwx' seen in the next three positions represents the user who has all three actions available (read, write, execute). The next set is for the group and in this case the group is given read and write permissions. Finally, the third set 'r--' is for all - everyone else on the network.

        The permissions granted to a file also depend on the permissions of the directory in which the file is located. For example, even if a file is set to -rwxrwxrwx, other users cannot access the file unless they have read and execute access to the directory in which the file is located.

        Type ls -l in the command line to view the current directory and file permissions.

        What permissions does your Information.txt have?

      • Changing Access Rights on Files

        Option 1: Only the owner of a file can use chmod to change the permissions of a file. The syntax of chmod is as follows:

        chmod {a,u,g,o}{+,-}{r,w,x} file_name

        with {a(all), u(user), g(group), o(other)}, {r(read), w(write (and delete)), {+ ( add permission), -(take away permission)}, x(execute (and access directory))}.

        Briefly, you supply one or more of all, user, group, or other. Then you specify whether you are adding rights (+) or taking them away (-). Finally, you specify one or more of read, write, and execute. For example, the command

        username@turing:~$ chmod u-r Information.txt will remove the read permission from the user's right.

        What is command to add all permissions (read, write and execute) for the user of the file Information.txt



        Option 2: Permissions may be granted using human readable assignments "rwx" or octal codes. Use of octal assignment does not add or remove permission, but assigns the permission explicitly.

        DescriptionAbbreviationOctal code
        Read accessr4
        Write (change) permissionw2
        Execute script of binary executablex1
        Read and Executerx5
        Read and Writerw6
        Read, Write and Executerwx7

        If we view rwx as options that can be turned on or off, we can do the chmod more quickly. For example, r-- can be viewed as the binary string 100. That is the representation in binary for the value 6. Similarly, --- is the binary string 000 and the binary number 0. So, to do rwx------, we can view that as: 111 000 000 or: 700. The most commonly used permissions are:

        ~$ chmod 600 file_name allows only the owner to read or write a file. (i.e., rw-------)

        ~$ chmod 700 file_name allows only the owner to read, write, or execute a file (including access to entering a directory). (i.e., rwx------)

        ~$ chmod 644 file_name allows only the owner to write a file, but allows everyone read access. (i.e., rw-r--r--)

        ~$ chmod 755 file_name allows only the owner to write a file, but allows everyone to execute it (including access to entering a directory). (i.e., rwxr-xr-x)

    13. Creating a tar file
    14. To work with a collection of files and directories using linux commands as a single unit you will need to compress then into a single entity. We will use the current directory (Labs) and the file within (Information.txt) for this example. Navigate to the root folder (cd .., or use cd ~/Desktop/2004 if you are in a different directory). If you do an ls here you should see your directory 'Labs' highlighted in blue. Now you can use the following command to compress the 'Labs' folder and its contents.

      tar -cvf Labs.tar Labs/

      Once that is done you can do another ls and you should see a Labs.tar file highlighted in red.

      But how to read a file with the .tar extension. Try using the next set of commands to decompress the tar file you created previously.

      tar -xvf labs.tar

      The previous command will extract the contents of the labs.tar file and create a new directory with the name labs. If there is another directory with the same name, the old directory will be replaced to keep both directories you need to rename the output of the decompression process. Try using the following commands

      mkdir decompression
      cd decompression
      tar -xvf ../labs.tar
      

      The previous command will create a new directory and it will store the content of the decompression process into that new directory. Now make sure to delete the already created tar file and the new directory that you created.

      cd ..
      rm labs.tar
      rm -r decompression
      

    15. Creating and running a shell script
    16. A shell is a program that acts as an intermediary between you and the guts of the operating system. In a DOS environment, command.com acts as your shell. Linux shells have more interesting names (like bash, pdksh, and tcsh), but they do pretty much the same thing. In addition to translating your commands into something the kernel can understand and act upon, the shell adds some important functions that the base operating system doesn't supply. Here below is an example of a shell script:

      #!/bin/bash
      clear
      echo "Good morning everyone!"
      

      You may have to give yourself executable permission on the script file (chmod u+x testshell). Copy and paste this above script and save to the file testshell. Try to run this script by the command ./testshell and explain what does this script do.

      You can also create a shell script to compile and run your homework. For example, if your homework source file is hw3a.cpp, copy and paste this below script to gohw in the same folder with your homework. Try to run this script ./gohw and see the result.

      #!/bin/bash
      clear
      echo "Compiling and running my homework"
      hw3a.cpp
      g++ hw3a.cpp -o hw3a.exe
      ./hw3a.exe
      

    Part 2 - Creating a web page in HTML

    In this part, you'll learn how to create a simple web page and display it on your own UoA account. Here is a sample web page that you will be modifying: lab9sample.html

    First, go to your home directory. Execute:
    cd ..
    to move to your parent directory. Then, execute:
    chmod a+x username
    to allow the web server access to your home directory.
    Then, do:
    cd to move back in to your home directory and then:
    ls -l
    to see if you have a public_html directory. If you do not, create one with:
    mkdir public_html
    Also, be sure that the permissions are rwxr-xr-x on the public_html directory. If they are not, use chmod to fix it.

    To download the sample page into your public_html directory run:
    cd public_html
    wget http://www.csce.uark.edu/~jgauch/2004/F12/labs/lab9sample.html
    wget http://www.csce.uark.edu/~jgauch/2004/F12/labs/picture.jpg
    mv lab9sample.html index.html

    Change permissions on both these files so that they can be viewed by others (i.e., rw-r--r--) by moving into your public_html directory and doing:

    chmod a+r *.*
    or chmod 644 *.*

    Try to view it from your web browser by entering the link http://www.csce.uark.edu/~your_account/index.html (for example, http://www.csce.uark.edu/~sgauch/index.html). You can customize this page for your own web homepage.

    Now, open your web homepage in nano and see what you can customize for your own web page.

    1. Change your name in this page.
    2. You can see in your page a link to the CSCE department, try to create the link to the University of Arkansas (www.uark.edu) in the same phrase "I am currently student at the University of Arkansas..."
    3. Insert your courses for this semester into the course table.
    4. You can also display your own picture. Copy your picture to the same directory with the index.html file (i.e., public_html). Now, try to display your picture by changing the source file src="your_picture.jpg"

    Copy and paste your web page url here.



    The Linux Manual

      The Linux manual is a good source for more information on various functions and commands. There are two ways to use the manual.

      1. Using the man command followed by the function or command (ie. man gcc). This will bring up information concerning the C-compiler.
      2. You can also search by keyword for functions or commands by using the -k parameter. (ie. man -k print). This will output all the functions or commands available in Linux related to printing.

      Note: You type the single character q to quit man.

      A printable sheet of quick Linux commands is available on the notes.

    Other Resources

      HTML tutorial

      There are many resources on the web for learning linux commands. You may want to visit these pages.

      Linuxdoc.org - A site that has many HOWTOs for linux. Every linux hacker has visited this site on multiple occasions

      A site with many beginner level tutorials on linux

      Do not forget about the "man" command for learning about linux commands, use the man pages extensively: "man" followed by the name of the command.

  1. Submit Work
  2. This lab assignment will be submitted electronically to the TAs once you fill in the fields below and click on the "submit" button. You do NOT need to print a copy of this document to hand in.

    Your UAID number:
    Your website PASSWORD: