COMP 3000 Lab 1 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
- [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 /etc/issue generally to find out.
- [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.
- [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.)
- [1] Who installed and configured your Linux environment? What level of access do you have to it?
- [2] A shell command can be one of four things. What are those four things? Explain very briefly.
- [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 ls /usr/bin | more and ls /usr/bin | less. How is the output similar? How are they different?
- [1] How would you create an empty file with the filename empty file.txt? (Yes, that is a space in the filename.)
- [1] How would you output the contents of this file?
Part B
- [4] Some shell commands, such as pwd, are both built-in and are external. What is one reason why both versions might be present?
- [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?
- [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.
- [2] Are there operators that work specifically with STDERR? Explain briefly.
- [2] How do you use these operators with other file descriptors? Explain briefly.
- [2] Give an example of a bash for loop and explain what it does.
- [2] Give an example of a bash if statement and explain what it does.
- [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?
- [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:
- *(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
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:
- 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.