CSC3320 Lab 3 Solved

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

Rate this product

Purpose: Learn how to set permissions for the files and directories. Practices on editing a file via the vi editor.

Note: Please follow the instructions below step by step, and then write a report by answering the questions and upload the report (named as Lab3_FirstNameLastName.pdf or Lab3_FirstNameLastName.doc) to Google Classroom, under the rubric Lab 3 Out-of-lab Assignemnt.

Please add the lab assignment NUMBER and your NAME at the top of your file sheet.

Part 1: VI Editing – Small file

Open your terminal and connect to snowball server. Change your directory to your home directory (cd ~ ), and then create a new directory named as “Lab3” (mkdir Lab3). After that, go to directory Lab3 (cd Lab3) and please download the file “Try.c” (content shown in table below) by the following command (internet access required):

cp /home/frondel1/Public/Try.c Try.c

Be sure it succeeds using “ls” to see the file name “Try.c” listed. //Try.c

#include <stdio.h>
#include <stdlib.h> /* For exit() function */ int main(int argc, char *argv[]) {

FILE *fptr; fptr=fopen(“program.txt”,”a+”); if(fptr==NULL){

                    printf("Error!");

exit(1); }

fprintf(fptr,”program is written”); printf(“program is written in program.txt”); fclose(fptr);
return 0;

}

Try the following steps by issuing some commands in your vi

editor 1) Open “Try.c” with vi editor $vi Try.c

1

  1. 2)  Move cursor to the beginning of “Error!”

    use UP DOWN LEFT RIGHT arrow to control cursor

  2. 3)  Insert “xxx”.

    i
    type “xxx” (hit x three times)

  3. 4)  Append a blank line after the current line.

    Hit Esc to command mode o (lowercase o)

  4. 5)  Delete “xxx”.

    Hit Esc to command mode.
    Move cursor to the beginning of “xxx”, press x three times or press 3s to delete “xxx”

  5. 6)  Copy the first 2 lines, move cursor to the beginning of file, and then paste it

    after current line

    :1,2y :0
    p

  6. 7)  Delete the first 2 lines

    :1,2d

  7. 8)  Save it

    :w

  8. 9)  Replace all “fptr” with “FPTR”
       :1,$s/fptr/FPTR/g
    

10) Save and exit.

:wq

2

Part 2: VI Editing – Large file

1) GointoyourLab3directory.

$cd ~/Lab3

2) Copy”RealEstate.csv”frominstructor’spublicdirectorytoyourLab3directory again.

$cp /home/frondel1/Public/RealEstate.csv .

Please write the commands you will issue to complete the following tasks and answering corresponding questions step by step in your report.

  1. 3)  Use vi to open “RealEstate.csv”.
  2. 4)  Move the cursor to the last line (without knowing the number of last line).
  3. 5)  Display line number.
  4. 6)  Search for the transaction for the estate located at “111 EAST”

    Which line is this string located? (Please just write down the line number)

    Delete this line.

  5. 7)  Move the cursor to the line 50.
  6. 8)  Substitute all comma “,” with colon “:” from line 50 to line 54.
  7. 9)  Copy line 50 to line 54 to the end of file.
  8. 10)  Remove line 50 to line 54.
  9. 11)  Describe how to enter the text mode and insert a new line “Recorded in year 2008” between line 1 and line 2.
  1. 12)  Switch back to command mode.
  2. 13)  Save the file and quit vi.

3

Part 3: Permissions for files

Follow the instructions step by step and finish the questions as required.

  1. 1)  Go into your Lab3 directory.

    $cd ~/Lab3

  2. 2)  Check the file permissions for file “Try.c” in your own Lab3 directory.

    $ls -l Try.c

  3. 3)  You may see similar output as below, in which rw-rw-r– of the first field is the file permission string for “Try.c”.
             [[email protected]@snowball Lab3]$ ls -l Try.c
             -rw-rw-r--. 1 [email protected] [email protected] 379 Jan 28 11:57
             Try.c
    
    •   The leftmost 3 characters rw- tells us that the user (owner of the file) can only read and write the file.
    •   The middle 3 characters rw- tells us the other users in the same group as the owner can only read and write the file.
    •   The last 3 characters r– tells us the other users in the other groups different from owner can only read the file.

      Note: once you copy a file from other directory or download a file from other resources, you are the owner of the new copied or downloaded file.

  4. 4)  Remove the read permission for the owner (yourself).

    $chmod u-r Try.c

  5. 5)  Check the file permissions for file “Try.c” again.

    $ ls -l Try.c

  6. 6)  You may see similar output as below, in which -w-rw-r– of the first field is the file permission string for “Try.c”.
             [[email protected]@snowball Lab3]$ ls -l Try.c
             --w-rw-r--. 1 [email protected] [email protected] 379 Jan 28 11:58
             Try.c
    

    So -w- in the leftmost 3 characters tells us that the user (owner of the file ) only has the permission to write something into the file.

  7. 7)  Try the vi editor again to modify the file.

    $vi Try.c

4

8) However, you may find following message displayed at the bottom of the screen which means you do not have the right to read “Try.c”.

         "Try.c" [Permission Denied]

9) Quit vi editor.

:q

10) Try read “Try.c” again using cat.

$cat Try.c

   Attach a screenshot of the output.

0,0-1 All

11) Use chmod with an octal number to let all the users only have read permission for “Try.c”.

   $chmod 444 Try.c

Note: The permission string to be set should be r–r–r–. Convert each group of three characters into decimal to form an octal number, which should be 444.

12) Check the file permissions for file “Try.c” again. And explain the meaning of each character in the file permission string.

13) Try the vi editor again to modify the file. Then remove one line by pressing dd

   $vi Try.c
   Move your cursor to some line and press dd

14) Try to save the file in the vi editor.

:w

15) Can you find some error message at the bottom of the screen? If yes, what is it and how to quit the vi editor without saving the modification.

   Write answer in your answer sheet.

16) Use chmod to add write the permission to all the users for “Try.c”.

   Write answer in your answer sheet.

17) Check the file permissions for file “Try.c” again. And explain the meaning

of each character in the file permission string.

   Write answer in your answer sheet.

5

Part 4: Permissions for directories

The permissions also work for the directories. However, the permissions for the directories may have different behaviors.
Let us learn the permissions for directories by only changing different permissions to the owner of the file.

1) Go to your home directory and then check the permissions for directory Lab3.

$cd ~

   $ls -ld ~/Lab3

Note: -d option will let you check the detailed information for the directory instead of its contents. 2) You may see similar output as below, in which rwxrwxr-x of the first field

is the permission string for directory Lab3.

         ls -ld ~/Lab3
         drwxrwxr-x. 4 [email protected] [email protected] 4096 Jan 28 11:58
         /home/frondel1/Lab3

3) Use chmod with octal number to forbid all permissions to all users.

   $chmod 000 ~/Lab3
  1. 4)  Check the permissions for directory Lab3. You may see similar output as below. The permission string is changed to ———.
             ls -ld ~/Lab3
             d---------. 4 [email protected] [email protected] 4096 Jan 28
             11:59 /home/frondel1/Lab3
    
  2. 5)  Finishing the following tasks and fill out the blanks in the row for owner’s permission “—” in the table below. If the task or command can be executed successfully, mark Y in the table, otherwise, mark N in the table. Please mark N/A if the task or command is not executed.
    1. Check the contents in directory Lab3.

      $ls ~/Lab3

    2. Create a directory named as “test” in Lab3.
                $mkdir ~/Lab3/test
      
    3. Create a file named as “test.txt” in Lab3.
                $cat>~/Lab3/test.txt
                A test
                ^D
      

6

  1. If (B) succeeds, remove the created directory “test” of Lab3.
              $rm -r ~/Lab3/test
    
  2. If (C) succeeds, copy “test.txt” from your Lab3 into your home directory.

    $cp ~/Lab3/test.txt .

  3. Go into directory Lab3.

    $cd ~/Lab3

The blanks in row “—” have been filled out in the table. Please compare it to your answers.

6) Fill out the blanks in other rows by repeating 3) to 5) when the owner is assigned different permissions as in the first column of the table. However, when setting the permissions, we still need to forbid all the permissions to all other users. So the last two bits in the octal number should always be kept as 00.

For example, since owner’s permissions is –x at next row, we should first set the file permission by issuing the command chmod 100 ~/Lab3. And then fill out the blanks in the row for

permissions –x by repeating 5).

Owner’s permissions

ls

mkdir

cat >

rm

cp

cd

A. Read contents

B. Create sub-directory

C. Create file

D. Remove contents

E. Copy contents from

F. Enter into directory

N

N

N

N/A

N/A

N

–x

-w-

-wx

r–

r-x

rw-

rwx

Note: Since you need to try at least 56 commands, to save the time, you can press upper arrow or down arrow to repeat previous or next command.

7

Appendix:

Table: The mapping of permissions of three characters to octal/decimal

Permissions

Binary

Octal/Decimal

000

0

–x

001

1

-w-

010

2

-wx

011

3

r–

100

4

r-x

101

5

rw-

110

6

rwx

111

7

8

  • Lab-3-3c0w2v.zip