Assignment 1 - The Linux Command-Line Environment | CS 2213, Assignments of Computer Science

Material Type: Assignment; Class: Advanced Programming; Subject: Computer Science; University: University of Texas - San Antonio; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-hnd
koofers-user-hnd 🇺🇸

5

(1)

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2213, Spring 2009
Assignment 1: The Linux Command-Line Environment
Due: 5pm, Friday, January 30,2009 (email 1/27, see below)
Goal: The purpose of this assignment is to familiarize you with the command-
line Linux environment that you will be using this class.
This assignment covers only the most basic usages of Linux/UNIX command-
line. For more information, please read the appropriate sections of Sobell.
There is an also online UNIX tutorial from the Canadian Research Coun-
cil at http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/basic/node1.
html that might be useful.
Learning objectives: be able to login to the CS department machines; be able to
execute programs from a bash or tcsh shell; be able to use basic UNIX commands
to navigate the file-system (pwd, cd, ls), manage files and directories (cp, mv, rm,
mkdir, rmdir), and view the contents of text files (cat); be able to use vi to edit
text files; be able to use svn to download updates from a server repository (svn
update), and to record your additions/modifications in the server repository (svn
update, svn status, svn add, svn commit); be able to use gcc to compile single
source file C programs.
Other objectives: setup account password; prepare to receive email on your CS
account; checkout the svn repository used in this course.
Directions: Please read the entire assignment, and then follow the steps enumer-
ated below.
You will need to turn in the answers to the bolded questions in the text file
assgn1/questions.txt. A template for questions.txt will be retrieved from sub-
version as part of the section on subversion. Instructions on how to format/sub-
mit the questions.txt are found in the final section. It is suggested that you simply
record your answers to the bolded questions on a piece of paper until you get to
the final section of this assignment.
Except as noted with regard to the bonus question, you are allowed to use out-
side resources and get help from your classmates and other individuals without
penalty on this assignment as long as the help received is (1) completely disclosed
in the README file and (2) does not involve directly copying answers without doing
the prescribed activities or understanding what those answers mean.
Login information. Every registered CS student is given a Windows XP and a
UNIX account. The usernames are the same,1and they share space (users’ UNIX
home directory is available as Z: under windows).
What is your username?
1Usually, first initial + last name, truncated to 8characters. If there’s already an account with
that name, the username will be up to 7letters of the first name and the first letter of last name.
The username’s of new users should be posted in the labs. See also: http://www.cs.utsa.edu/
~javalab/lab/accountLogin.html
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Assignment 1 - The Linux Command-Line Environment | CS 2213 and more Assignments Computer Science in PDF only on Docsity!

CS 2213 , Spring 2009

Assignment 1 : The Linux Command-Line Environment

Due: 5 pm, Friday, January 30 , 2009 (email 1 / 27 , see below)

Goal: The purpose of this assignment is to familiarize you with the command- line Linux environment that you will be using this class. This assignment covers only the most basic usages of Linux/UNIX command- line. For more information, please read the appropriate sections of Sobell. There is an also online UNIX tutorial from the Canadian Research Coun- cil at http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/basic/node1. html that might be useful.

Learning objectives: be able to login to the CS department machines; be able to execute programs from a bash or tcsh shell; be able to use basic UNIX commands to navigate the file-system (pwd, cd, ls), manage files and directories (cp, mv, rm, mkdir, rmdir), and view the contents of text files (cat); be able to use vi to edit text files; be able to use svn to download updates from a server repository (svn update), and to record your additions/modifications in the server repository (svn update, svn status, svn add, svn commit); be able to use gcc to compile single source file C programs.

Other objectives: setup account password; prepare to receive email on your CS account; checkout the svn repository used in this course.

Directions: Please read the entire assignment, and then follow the steps enumer- ated below. You will need to turn in the answers to the bolded questions in the text file assgn1/questions.txt. A template for questions.txt will be retrieved from sub- version as part of the section on subversion. Instructions on how to format/sub- mit the questions.txt are found in the final section. It is suggested that you simply record your answers to the bolded questions on a piece of paper until you get to the final section of this assignment. Except as noted with regard to the bonus question, you are allowed to use out- side resources and get help from your classmates and other individuals without penalty on this assignment as long as the help received is ( 1 ) completely disclosed in the README file and ( 2 ) does not involve directly copying answers without doing the prescribed activities or understanding what those answers mean.

Login information. Every registered CS student is given a Windows XP and a UNIX account. The usernames are the same,^1 and they share space (users’ UNIX home directory is available as Z: under windows). What is your username? (^1) Usually, first initial + last name, truncated to 8 characters. If there’s already an account with that name, the username will be up to 7 letters of the first name and the first letter of last name. The username’s of new users should be posted in the labs. See also: http://www.cs.utsa.edu/ ~javalab/lab/accountLogin.html

  1. Use one of the machines in the Main Lab or one of the department’s teaching laboratories on the third floor of the science building.

  2. If the machine is currently booted in Windows XP, shut it down. As you restart the computer, select Ubuntu from the Grub boot menu.

  3. Login using your department username.

(a) The UNIX and Windows XP account passwords are not synchronized, so you actually have two different passwords. It is recommended that you set them both to the same thing. See http://www.cs.utsa.edu/ ~javalab/lab/accountLogin.html (b) If you have not previously logged in to one of the department UNIX hosts (Solaris or Linux) and changed your password, your password will be the first 8 digits of your student id number.

  1. Once your have logged in, open up a terminal. In the GNOME desktop environment (which is the default graphical user interface for Ubuntu) this can be accomplished by selecting the program “Terminal” under under the Applications/Accessories drop-down menu. The terminal will contain a prompt at which you can type commands.

  2. If you have not previously set your UNIX password, enter the command passwd followed by an enter into the Terminal and follow the prompts for entering your old and new passwords.

Directory Navigation. One important concept in Linux/UNIX is the file-system hierarchy, which organizes the permanent storage of a UNIX system. (For more information, see Sobell, Chapter 4 .) There are two items in the UNIX file-system: directories (a.k.a., folders) and files. Files contain arbitrary streams of text or binary data. Files are named through entries in directories. Directories may also contain other directories. The outermost directory, known as the root director (written /) is the only directory not contained in another directory. Thus, the directory hierarchy consists of a tree rooted at /.^2

  1. The prompt you see when you first open up terminal is called the UNIX shell. There are actually a couple different shells that have slightly different features and operate in different ways. The machines in our department normally default to tcsh, a descendant of csh (the C shell); another common shell is Bourne shell (sh) and its descendant bash. Both of these are described in Sobell (Chapters 8 and 9 ). For our purposes, it shouldn’t matter which one you are using. Part of the shell’s state is the current ‘working directory’ (a.k.a., current directory), which is where many commands default to operating. Print the working directory of your shell by typing the command pwd (which stands (^2) As a side note, UNIX users do not usually need to deal with individual volumes or drive letters as in Windows. The system administrator can just mount a disk partition or network share as a replacement for some subdirectory within the single unified hierarchy.

From the directory /var/log, list the contents of /var/run using a a relative path. What command did you use?

  1. What happens if you try to pass ls a non-existent name, such as ls /bin/transmogrify?

Managing files and directories. The following commands can be used to copy, move/rename, remove, or create files and directories. Command Meaning cp old-file-name 5 new-file-name copies the contents of the file old-file-name to a new file named new-file-name cp list-of-files 6 destination-directory copies each of the files in list-of-files to the destination-directory mv old-file-name new-file-name renames the file old-file-name to new-file-name (which can be in a different directory from old-file-name mv old-directory-name new-directory-name renames the directory old-directory-name to new-directory-name mv list-of-files existing-directory moves each of the files in list-of-files to some existing-directory rm list-of-files removes (deletes)^7 each of the files in list-of- files mkdir new-directory creates a new directory called new-directory, which must be in a previously existing di- rectory, but must not previously exist rmdir empty-directory-name removes an empty directory called empty- directory-name

  1. Change to your home directory (a.k.a., ~).

  2. Copy /usr/bin/man to the current directory (which can be represented using a single dot: .)? What command did you use?

  3. Rename rename ~/man to ~/my-man without using dot ., slash /, or tilde ~. What command did you use?

  4. Remove my-man from you home directory? What command did you use?

  5. Create a subdirectory within your home directory called courses. What command did you use?

  6. In order to make sure, you classmates can’t look at your course files, please run the command: chmod g-rwx,o-rwx ~/courses (^5) The filenames/directory names used in these commands may be specified using relative or absolute paths. Most often, at least one of the arguments is in the current directory and specified just as a file or directory name without any slashes; this is just a special case of a relative path. (^6) For all of the commands that take lists of files or directories; it is permissible (and quite common) to just give a list consisting of only one file or directory. If there are more than one file or one directory in the list, they should be separated with spaces. (^7) UNIX/Linux rm does not normally implement any kind of Trash Basket or Recycle Bin like most graphical user interfaces (GUI) do. Once you remove a file with rm, there is no guaranteed way to recover it.

Viewing and Editing Text Files. Most files in UNIX are text files, which contain lines of readable characters and white-spaces characters separated by newlines '\n'. Many UNIX/Linux command operate on text files, including cat and more. vi can be used to edit text files.

  1. The command, cat file-list , can be used to display the contents of one or more files. Use cat to look at /etc/issue. What is the contents of the file /etc/issue?

  2. Some files, however, take up more than one page, and are inconvenient to view with cat. For example, try to view /etc/services with cat. This file more than fills up one screen. The UNIX utility more^8 provides a solution to this problem, by displaying only one screen-full at a time, and waiting until you press the space bar to see one “more,” screen-full. In addition, one can press enter to see one more line at a time, and ‘b’ to go up one screen-full at a time. Use more to view /etc/services and find out which service operates on port

  3. Which service is this?

  4. You can create and edit text-files using the editor vi.^9 vi is a bit of an acquired taste. At first it is quite difficult to use, because ( 1 ) it has a few distinct modes that operate in different ways, and ( 2 ) you have to memorize the commands to get it to work. Run the program vimtutor to learn how to navigate vi. You may also find it helpful to browse through the tutorial at http://rcsg-gsir.imsb-dsgi. nrc-cnrc.gc.ca/documents/basic/node166.html which has a fairly con- cise listing of some of the most important vi commands, or to read Chapter 6 in Sobell.

Email. Your UNIX account is capable of receiving email at the address username @cs.utsa.edu. An email will be sent to all students in this class on January 27. Reply to this email by January 30 , 2009. This email can be read through a web mail system http://mail.cs.utsa.edu; connecting your favorite email client to the department IMAP server imaps://mail.cs.utsa.edu; using a text-mode client, such as mutt; or by forwarding future email to a different email account (see the instructions for using vi to create a .forward file below). Now that you know how to use vi, you can use it to setup your CS department email to be forwarded to another account.

  1. In your home directory, use the command vi .forward to launch the vi editor with a blank document buffer that you will save to .forward.

(a) Insert a single line that contains the email address of the account you normally use for email (e.g., [email protected]). (^8) The utility less, which is available on Linux systems, also does about the same things, but let’s you go backwards in some situations more does not. (^9) Actually, our department machines default to an enhanced variant of vi called vim.

  • The TA/instructor will use subversion to access the most recently commit- ted version of each student’s assignment as of 5 pm on the due date for that assignment. This version will be graded. (Students can continue to com- mit additional changes after the deadline, but these will be ignored during grading.)

Subversion works mostly the same as an older system called Concurrent Ver- sions System (CVS), so most of what Sobell says about CVS on page 420 ff. applies to Subversion (just replace cvs with svn in the commands. There’s also an online book at http://svnbook.red-bean.com/. Here we will cover just the minimum needed to use svn to submit your assignments in this class. Further reading in svnbook or Sobell is recommended. The repositories that will be used in this class are stored on the machine nougat.cs.utsa.edu and accessed using your department UNIX account names and passwords. The URL for each student’s repository is: https://nougat.cs.utsa. edu/cs2213/cs2213-username

  1. The first step in using subversion is to cd into your ~/courses directory and check out a working copy of your repository using the command: svn co https://nougat.cs.utsa.edu/cs2213/cs2213-username This will download the current version and setup a working copy of your repository ~/courses/cs2213-username.

  2. Use ls to see what files are in your working copy.

  3. Now, make some modifications in the working directory as follows:

(a) Change into ~/courses/cs2213-username/assgn1 directory. (b) Use vi to edit the file questions.txt and put your name at the top. (c) Use vi to create a new text file called README containing your name.

  1. Now, that there have been changes made to the local copy it is necessary to commit them before they are uploaded to the repository and become visible to other users (i.e., the instructor and TA). It is recommended, that you do this before you leave the computer each time you’ve been working on your assignment. Until one becomes an svn expert, the following procedure is recommended. Use it to commit your changes to questions.txt and README.

(a) Use svn status to get a list of any files or directories that you’ve cre- ated or modified, since the last time you did a commit. Files that are the same in the working directory and the server repository will not be listed. (b) If svn status puts a? before a file, that file is ‘unknown’ to subversion and needs to be ‘added’ before it can be committed to the repository. This can be accomplished with the command: svn add list-of-files

(c) Use the command svn update to synchronize your local copy with the latest changes in the repository. (That is, download any new updates from the server and merge them into your local copy.) Normally, this should ‘just work,’ but if svn reports a conflict, please see http://svnbook.red-bean.com/en/1.5/svn.tour.cycle. html#svn.tour.cycle.resolve. (d) Use svn commit to actually commit your current working version to the server repository.

  • As part of the commit process, an editor will be opened and you will be given an opportunity to write a sentence or two about what changes you are committing. You can later view these as a log of what changes you’ve made at what times.

Compiling with gcc. GCC can be used to compile a C source code into an executable program using the following command: gcc -o program-name list-of-source-files. The program can then be executed with the command ./program-name.

  1. A program hello.c has been provided in the assgn 1 directory of your svn repository.

(a) cat it. (b) Compile it to produce an executable hello in assgn1. (c) Run the compiled program with the command ./hello. What does the program output? (d) Use vi to modify the program, so that the output is customized with your name, so that, for example, if your name were John Smith, you’d modify the program to output: Hello, John Smith! (e) Recompile and rerun the program. If there are errors, fix them, and repeat as necessary. (f) Commit your modified hello.c to the subversion repository for grading using the steps described above. The program file hello does not need to be added to svn. In general, it is desirable to have svn manage only those files created or edited by humans (i.e., source files and documen- tation), but not the files produced by the compiler or automatically by other tools from those files. Your svn repositories should already be set up to ignore the files assgn1/hello and assgn1/fix-me.

  1. Another program fix-me.c has been provided in the assgn 1 directory of your subversion repository.

(a) Use gcc to try to compile fix-me.c into the executable program fix-me. What happens? What error message does gcc report? There are two errors: one on line 8 and one on line 11. How can you tell this from gcc’s output? (b) Fix the first error by deleting the third + in line 8.