Difference between revisions of "COMP 3000 Lab 1 2012"

From Soma-notes
Jump to navigation Jump to search
 
(15 intermediate revisions by 2 users not shown)
Line 3: Line 3:
For reference material, we suggest you look at this [http://linuxcommand.org/lc3_learning_the_shell.php tutorial on the shell] and the associated book [http://linuxcommand.org/tlcl.php The Linux Command Line] (available as a PDF for free and in print).  You may also use other Internet resources or other people. ''Please cite all sources that you use to answer the questions in this assignment.''
For reference material, we suggest you look at this [http://linuxcommand.org/lc3_learning_the_shell.php tutorial on the shell] and the associated book [http://linuxcommand.org/tlcl.php The Linux Command Line] (available as a PDF for free and in print).  You may also use other Internet resources or other people. ''Please cite all sources that you use to answer the questions in this assignment.''


You should turn in Lab 1 by 10 PM on Friday, September 21 via cuLearn.  Your answers should be in '''plain text''' (the true UNIX file format) or '''PDF'''.  No other formats are acceptable (and will result in a zero grade until re-submitted in the correct format).
You should turn in Lab 1 by 8 PM on Monday, September 24 via [https://www.carleton.ca/culearn/ cuLearn].  (Normally the due date is Friday, but mistakes were made and there was a power outage.) Your answers should be in '''plain text''' (the true UNIX file format) or '''PDF'''.  No other formats are acceptable (and will result in a zero grade until re-submitted in the correct format).  Note the lab has 50 points in total.


You should expect to complete Part A in tutorial.  You should submit the answers to both Part A and Part B, however, on Friday.
You should expect to complete Part A in tutorial.  You should submit the answers to both Part A and Part B, however, on Friday.
You should use this as an opportunity to become familiar with the UNIX command line.  After this lab it will be assumed that you are reasonably fluent in basic shell usage.  If you do not feel so comfortable, you should study the above mentioned references.


==Part A==
==Part A==
# [10] What is your username on this wiki?  Have a TA help you create an account.
# [8] What is your username on this wiki?  Have a TA help you create an account.
# [1] What Linux distribution are you using?  What version?  You should look at the file <tt>/etc/issue</tt> generally to find out.
# [1] What Linux distribution are you using?  What version?  You should look at the file <tt>/etc/issue</tt> generally to find out.
# [1] What shell do you get by default when you log in?  Check by running <tt>echo $SHELL</tt>.  If it is not ''bash'', then type <tt>exec bash</tt> to switch to it.
# [1] What shell do you get by default when you log in?  Check by running <tt>echo $SHELL</tt>.  If it is not ''bash'', then type <tt>exec bash</tt> to switch to it.
Line 16: Line 18:
# [2] On the machine you are on, in what directories are commands external to the shell stored?  How can you get this information on the command line?
# [2] On the machine you are on, in what directories are commands external to the shell stored?  How can you get this information on the command line?
# [2] Compare the commands <tt>ls /usr/bin | more</tt> and <tt>ls /usr/bin | less</tt>.  How is the output similar?  How are they different?
# [2] Compare the commands <tt>ls /usr/bin | more</tt> and <tt>ls /usr/bin | less</tt>.  How is the output similar?  How are they different?
# [1] How would you create an empty file with the filename <tt>empty file.txt</tt>? (Yes, that is a space in the filename.)
# [1] How would you output the contents of this file?


==Part B==
==Part B==
Line 28: Line 32:
# [2] What are the differences between shell and environment variables?  Specifically, what processes have each of them, and to what extent are they shared?  HINT: look at the <tt>execve</tt> system call.
# [2] What are the differences between shell and environment variables?  Specifically, what processes have each of them, and to what extent are they shared?  HINT: look at the <tt>execve</tt> system call.


==Processes==
== Answers ==
Answer key for lab 1:
 
# *(check against actual username list)
# should be: Ubuntu 12.04 LTS unless specifically stated reason otherwise
# /bin/bash *SCS gives csh as the default shell*
# bare metal (lambda) or virtualbox (virtualized)
# System administrators, level of access is restricted user
#
## An executable program like all those files we saw in /usr/bin. Within this category, programs can be compiled binaries such as programs written in C and C++, or programs written in scripting languages such as the shell, perl, python, ruby, etc.
## A command built into the shell itself. bash supports a number of commands internally called shell builtins. The cd command, for example, is a shell builtin.
## A shell function. These are miniature shell scripts incorporated into the environment. We will cover configuring the environment and writing shell functions in later chapters, but for now, just be aware that they exist.
## An alias. Commands that we can define ourselves, built from other commands.
# /usr/bin and /usr/sbin and other directories.  Run "echo $PATH" to see list of directories.  Also, which [external command] will tell you path for specific commands, e.g. "which ls"
# less allows backward movement within the file as well as forward movement. more is a rudimentary method for printing to crt terminals, but only allows movement through the file in a forward direction. You have to press 'q' to exit less, but more quits when it reaches EOF.
# touch "empty file.txt"
# cat empty\ file.txt or cat "empty file.txt" or cat 'empty file.txt' or > 'empty file.txt'
 
Part B
1. Some require direct access to kernel functions and therefore must be built in. Also the built in functions operate faster than non built in functions.
This is either for performance reasons -- builtins execute faster than external commands, which usually require forking off a separate process -- or because a particular builtin needs direct access to the shell internals.
 
2. drwx------
 
d = a directory flag - since my home directory is a directory, there exists a d here
rwx = read, write, execute permission for the User (owner of the directory) which is my username
--- = read write execute permissions are lacking for a Group
--- = read write execute permissions are lacking for other
 
this means my work is private. Only myself and the root user can access my directory.
 
3. cat file.txt | grep hello


Each application running on a system is assigned a unique process identifier.  The <tt>ps</tt> command shows the process identifiers for running processes.  Each process running on the system is kept separated from other processes by the operating system.  This information will be useful for subsequent questions.
the | character joins the two commands cat and grep, it enables the output from one command to be passed directly to another.


==Permissions==
cat file.txt > error.txt


Your permission to access a file in Unix is determined by who you are logged in as.  All files on the Unix file system (including directories and other special files) have three different sets of permissions. The first set of permissions denotes the allowed file operations for the owner of the file.  The second set of permissions denotes the allowed file operations for a group of users.  The third set of permissions denotes the allowed file operations for everyone else.  A
the > character redirects the standard output to the specified filename that is included in the command after it. If the file is not created it will create it, if the file exists it will overwrite the current file
file is always owned by someone and is always associated with a group.


The <tt>ls</tt> command with the <tt>-l</tt> option can be used to show both the permissions of a file as well as the owner and group associated with the file. Permissions are listed first, followed by the owner and the group.
cat file.txt > alreadyexistingerrorfile.txt


# Who is the owner of the <tt>/etc</tt> directory? (1 point)
the >> character appends to the already existing file the output of the first command.
# What group is associated with the <tt>/etc/shadow</tt> file? (1 point)
# Each user is a member of some number of groups.  You can determine what groups you are part of by using the <tt>groups</tt> command. Based on the groups listed, would you be a member of the group associated with the <tt>/etc/shadow</tt> file? (1 point)


==Environment==
cat < file.txt is equivalent to cat file.txt


The environment on both Linux and Windows contains variable - value pairs which are useful to applications running on the system.  In Linux, these environment variables can be printed on the command line by referring to the variable name prefixed with a $ sign (eg: to output the value in the HELLO environment variable, one could write <tt>echo $HELLO</tt>).
the < character redirects the standard input to come from the file instead


# On the command line, run the following two sets of commands in the <tt>bash</tt> or <tt>csh</tt> shell.  Notice that the shell command will start a new shell separate from the shell that the HELLO environment variable was set in.
the last form of redirection is only useful within shell scripts to avoid having to code a bunch of echo statements, say for a particular menu. The document created is called a here document. It is a method of specifying a string literal in command line shells. (preserves line breaks and whitespaces / indentation within the text.)


HELLO="Hi There"          or      set HELLO="Hi There"
example:
bash                              csh
echo $HELLO                        echo $HELLO
exit                              exit


export HELLO="Hi There"  or      setenv HELLO "Hi There"
# present a menu
bash                              csh
cat << alldone
echo $HELLO                        echo $HELLO
What would you like to do today?
exit                              exit
    Play with shell scripts
    Write wonderful awk scripts
    Invent powerful sed scripts
    zippity do and dah
alldone


'''Note:''' Do not put spaces on either side of the equal signs!
the above code will generate the following output:


What does the export command seem to do? (Or, what is the difference between set and setenv?) (2 points)
What would you like to do today?
    Play with shell scripts
    Write wonderful awk scripts
    Invent powerful sed scripts
    zippity do and dah


==Dynamic Libraries==
otherwise you would have had to code echo statements for each line.


Most applications on the system do not contain all the code that they need right within the executable.  Instead, dynamic libraries are loaded into the program address space when the program loads. As an example, the standard C library, which contains such functions as <tt>printf</tt> is loaded in at run-time.
4. STDERR does not have a dedicated redirection operator, instead to redirect stderr you have to refer to it's file descriptor. and call it using 2>


# Using <tt>ldd</tt>, what dynamic library dependencies does the <tt>top</tt> command have?  Note that you must specify the full path to <tt>top</tt>. (1 point)
5. the other file descriptors are 0 for standard in and 1 for standard out, the descriptor number is placed right before the redirection operator.
# In addition to the libraries listed as dependencies for the application <tt>top</tt> by <tt>ldd</tt>, there may be other libraries that the application loads dynamically at run-time.  Retrieve the process number <em>PID</em> for the <tt>csh</tt> process (using <tt>ps</tt>) and examine the map file located at <tt>/proc/</tt><em>PID</em><tt>/maps</tt>. What other dynamic libraries have been loaded into the application while it has been running? (2 points)


=Part B (take home)=
6.for variable [in words]; do
commands
done


==Processes==
variable is the name of a variable that will increment during the execution of the loop, words is an optional list of items that will be sequentially assigned to the variable, and commands are the list of commands to be executed on each iteration of the loop.


# What is the difference between the <tt>fork</tt> and <tt>exec</tt> function in a UNIX environment? ( 1 point )
for i in A B C D; do echo $i; done
# What is a zombie process? ( 1 point )
# Give an example C program which creates a zombie process.  Note that the shell by default will collect and destroy zombie processes and so you will need to avoid the shell destroying the zombie process during debugging.  This can be done by delaying the parent exit (using sleep is one good way to do this). ( 2 points )
# Perform the modifications to your program above to avoid creating a zombie process.  List the new program. ( 3 points )
==Permissions==


# Permissions on Unix are grouped into three basic file operations. What are these file operations? ( 1 point )
7. x=5
# What does it mean to have execute permission on a directory? ( 1 point )
if [ $x = 5 ]; then
# What are the 6 basic file permissions within Windows? ( 1 point )
echo "x equals 5."
# What is the difference between the write and modify file permission in Windows? ( 1 point )
else
# Because all files are stored in a directory, under UNIX permission to delete, rename, and move files is determined by the users access rights on the directory the file is contained in.  What attribute on the directory prevents those who can modify a directory from deleting files (hint: help on the <tt>chmod</tt> command may prove useful). ( 1 point )
echo "x does not equal 5."
# In Windows, a file can be associated with more than one group and each group can have different access permissions.  On Unix, each file can only belong to one group and groups cannot contain other groups (groups can only contain users).  Having said that, new groups can be created by the system administrator which are supersets of other groups (e.g., to create group C with members of A and B, just add all of A's and B's members to C).  Given this, is it possible to develop an access permission scenario which would be impossible to implement in Unix but possible to implement in Windows?  If yes, give an example.  If no, explain why. ( 1 point )
fi


==Environment==
x=5
if [ $x = 5 ]; then echo "equals 5"; else echo "does not equal 5"; fi


# What does the PATH environment variable do? PWD? ( 1 point )
if commands; then
# What environment variable tells X applications where to find the X server which it should communicate with to display the output? ( 1 point )
commands
[elif commands; then
commands...]
[else
commands]
fi
  where commands is a set of commands. Providing an exit status that indicates success or failure.


==Dynamic Libraries==
8. Background processes usually interact with the system and do not interact with the shell or the user, foreground processes must interact with the command shell. In the shell, background and foreground processes are the same except that the shell waits for foreground processes to finish before giving a new prompt.


Dynamic libraries allow one copy of executable code to be used by
9. shell variables apply only to the current shell. Environment variables apply to all possible shells running on the system. to list all env variables type printenv to list all shell variables use set
many different processes on the system, without requiring that
a shell variable is local to a particular instance of the shell (such as a shell script), while environment variables are "inherited" by any program you start, including another shell
multiple copies of the code be stored on disk in different files.
That is, the new process gets its own copy of these variables, which it can read, modify, and pass on in turn to its own children. In fact, every UNIX process (not just the shell) passes its environment variables to its child processes.
What are some problems that can arise when different programs use the
same common DLLs (hint: ``DLL Hell'')? ( 1 point )

Latest revision as of 13:21, 10 October 2012

In this lab you will be learning about the standard command shell on Linux, bash.

For reference material, we suggest you look at this tutorial on the shell and the associated book The Linux Command Line (available as a PDF for free and in print). You may also use other Internet resources or other people. Please cite all sources that you use to answer the questions in this assignment.

You should turn in Lab 1 by 8 PM on Monday, September 24 via cuLearn. (Normally the due date is Friday, but mistakes were made and there was a power outage.) Your answers should be in plain text (the true UNIX file format) or PDF. No other formats are acceptable (and will result in a zero grade until re-submitted in the correct format). Note the lab has 50 points in total.

You should expect to complete Part A in tutorial. You should submit the answers to both Part A and Part B, however, on Friday.

You should use this as an opportunity to become familiar with the UNIX command line. After this lab it will be assumed that you are reasonably fluent in basic shell usage. If you do not feel so comfortable, you should study the above mentioned references.

Part A

  1. [8] What is your username on this wiki? Have a TA help you create an account.
  2. [1] What Linux distribution are you using? What version? You should look at the file /etc/issue generally to find out.
  3. [1] What shell do you get by default when you log in? Check by running echo $SHELL. If it is not bash, then type exec bash to switch to it.
  4. [1] Is your Linux environment running virtualized or on bare metal? How do you know? (If you aren't sure, say so, but explain why.)
  5. [1] Who installed and configured your Linux environment? What level of access do you have to it?
  6. [2] A shell command can be one of four things. What are those four things? Explain very briefly.
  7. [2] On the machine you are on, in what directories are commands external to the shell stored? How can you get this information on the command line?
  8. [2] Compare the commands ls /usr/bin | more and ls /usr/bin | less. How is the output similar? How are they different?
  9. [1] How would you create an empty file with the filename empty file.txt? (Yes, that is a space in the filename.)
  10. [1] How would you output the contents of this file?

Part B

  1. [4] Some shell commands, such as pwd, are both built-in and are external. What is one reason why both versions might be present?
  2. [4] What are the permissions on your Linux home directory? With those permissions and your knowledge of the other accounts on the system, who has access to your home directory, and what sort of access do they have?
  3. [10] Give one or more command line strings that use the following operators: >, <, >>, <<, |. Explain briefly what each operator is doing in your examples, both concretely and in terms of STDIN and STDOUT.
  4. [2] Are there operators that work specifically with STDERR? Explain briefly.
  5. [2] How do you use these operators with other file descriptors? Explain briefly.
  6. [2] Give an example of a bash for loop and explain what it does.
  7. [2] Give an example of a bash if statement and explain what it does.
  8. [2] With the & you can put processes in the "background". Given that external commands are always run as separate processes from the command shell, what is the key difference between foreground and background processes?
  9. [2] What are the differences between shell and environment variables? Specifically, what processes have each of them, and to what extent are they shared? HINT: look at the execve system call.

Answers

Answer key for lab 1:

  1. *(check against actual username list)
  2. should be: Ubuntu 12.04 LTS unless specifically stated reason otherwise
  3. /bin/bash *SCS gives csh as the default shell*
  4. bare metal (lambda) or virtualbox (virtualized)
  5. System administrators, level of access is restricted user
    1. An executable program like all those files we saw in /usr/bin. Within this category, programs can be compiled binaries such as programs written in C and C++, or programs written in scripting languages such as the shell, perl, python, ruby, etc.
    2. A command built into the shell itself. bash supports a number of commands internally called shell builtins. The cd command, for example, is a shell builtin.
    3. A shell function. These are miniature shell scripts incorporated into the environment. We will cover configuring the environment and writing shell functions in later chapters, but for now, just be aware that they exist.
    4. An alias. Commands that we can define ourselves, built from other commands.
  6. /usr/bin and /usr/sbin and other directories. Run "echo $PATH" to see list of directories. Also, which [external command] will tell you path for specific commands, e.g. "which ls"
  7. less allows backward movement within the file as well as forward movement. more is a rudimentary method for printing to crt terminals, but only allows movement through the file in a forward direction. You have to press 'q' to exit less, but more quits when it reaches EOF.
  8. touch "empty file.txt"
  9. cat empty\ file.txt or cat "empty file.txt" or cat 'empty file.txt' or > 'empty file.txt'

Part B 1. Some require direct access to kernel functions and therefore must be built in. Also the built in functions operate faster than non built in functions. This is either for performance reasons -- builtins execute faster than external commands, which usually require forking off a separate process -- or because a particular builtin needs direct access to the shell internals.

2. drwx------

d = a directory flag - since my home directory is a directory, there exists a d here rwx = read, write, execute permission for the User (owner of the directory) which is my username --- = read write execute permissions are lacking for a Group --- = read write execute permissions are lacking for other

this means my work is private. Only myself and the root user can access my directory.

3. cat file.txt | grep hello

the | character joins the two commands cat and grep, it enables the output from one command to be passed directly to another.

cat file.txt > error.txt

the > character redirects the standard output to the specified filename that is included in the command after it. If the file is not created it will create it, if the file exists it will overwrite the current file

cat file.txt > alreadyexistingerrorfile.txt

the >> character appends to the already existing file the output of the first command.

cat < file.txt is equivalent to cat file.txt

the < character redirects the standard input to come from the file instead

the last form of redirection is only useful within shell scripts to avoid having to code a bunch of echo statements, say for a particular menu. The document created is called a here document. It is a method of specifying a string literal in command line shells. (preserves line breaks and whitespaces / indentation within the text.)

example:

  1. present a menu

cat << alldone What would you like to do today?

    Play with shell scripts
    Write wonderful awk scripts
    Invent powerful sed scripts
    zippity do and dah

alldone

the above code will generate the following output:

What would you like to do today?

    Play with shell scripts
    Write wonderful awk scripts
    Invent powerful sed scripts
    zippity do and dah

otherwise you would have had to code echo statements for each line.

4. STDERR does not have a dedicated redirection operator, instead to redirect stderr you have to refer to it's file descriptor. and call it using 2>

5. the other file descriptors are 0 for standard in and 1 for standard out, the descriptor number is placed right before the redirection operator.

6.for variable [in words]; do commands done

variable is the name of a variable that will increment during the execution of the loop, words is an optional list of items that will be sequentially assigned to the variable, and commands are the list of commands to be executed on each iteration of the loop.

for i in A B C D; do echo $i; done

7. x=5 if [ $x = 5 ]; then echo "x equals 5." else echo "x does not equal 5." fi

x=5 if [ $x = 5 ]; then echo "equals 5"; else echo "does not equal 5"; fi

if commands; then commands [elif commands; then commands...] [else commands] fi

where commands is a set of commands. Providing an exit status that indicates success or failure.

8. Background processes usually interact with the system and do not interact with the shell or the user, foreground processes must interact with the command shell. In the shell, background and foreground processes are the same except that the shell waits for foreground processes to finish before giving a new prompt.

9. shell variables apply only to the current shell. Environment variables apply to all possible shells running on the system. to list all env variables type printenv to list all shell variables use set a shell variable is local to a particular instance of the shell (such as a shell script), while environment variables are "inherited" by any program you start, including another shell That is, the new process gets its own copy of these variables, which it can read, modify, and pass on in turn to its own children. In fact, every UNIX process (not just the shell) passes its environment variables to its child processes.