User Tools

Site Tools


opus:spring2012:rhensen:part2

Part 2

Entries

Entry 5: March 2, 2012

Today’s lab introduced the concept of shell scripting in Unix. Shell scripts are executable text files that contain a series of commands that are to be performed when the file is executed. This concept seemed to me to be very similar to batch files in DOS. Before executing these files, there are two factors to take into account. The first is that the permissions of the file must be set to allow the file to be executed. Secondly in order to issue the command to execute the file, the path to the script file must be specified unless it is located in a directory that $PATH will search in.

This lab introduced many concepts that are useful for writing scripts. The read and let commands can be used to set and manipulate variables. Shell scripts can also contain if statements, which allow for the script to evaluate a condition and then perform different actions based on the results. The concept of iteration can also be used in scripts to allow the same action to be performed repeatedly in a loop. This can be achieved by setting a variable as the counter and incrementing the counter each time the action is performed until the limit for the counter is reached. The counter for the loop does not have to be numeric, it is also possible to perform an action repeatedly using a list of items. This lab was useful for learning concepts that are needed when creating scripts in the Unix environment.

Entry 6: March 9, 2012

This week’s lab involved the concept of multitasking and how to work with different running processes on the system. The ps command can be issued to show a list of the running processes on the system, which each correspond to a program that is running. The lab also explains that the & character can be added to the end of a command line to make the program run in the background. Running in the background means that the program will run invisibly to the user so that it will be performed but the user will be able to do other things as it is carried out. This is useful because it allows the user to do other things on the system when there are programs that take a long time to run, or in the case of programs that need to constantly be running.

Once processes are running, they can also be suspended by using the SUSPEND character (CTRL-Z). Suspending a process will temporarily pause the process. When processes are suspended, they can be brought back to the foreground with the fg command. The kill command can be used to completely end a running or suspended process by sending a signal to the operating system. The combination of these concepts is useful for understanding how to work with and manipulate the different processes that are running on the system.

The case study associated with this lab deals with the topic of scheduling tasks, which is setting certain processes to run at specific times. This is achieved by using a utility in Unix called cron. The cron utility is the scheduler and it makes use of the crontab, which is a list of scheduled processes and the times that they should be run. The at utility is similar in function to cron, but it is designed to be used for tasks that are only meant to be scheduled once.

Entry 7: March 16, 2012

This lab dealt with the programming environment in Unix, which deals with how programs are written, compiled, and executed. The source code of a program is simply a text file that contains the code written in a certain programming language and it is therefore not executable. In order to make the program an executable binary file, it must be compiled. The first part of the lab demonstrates how to use compilers on source code files written in C, C++, and assembly. Although these languages are all different, the compilers all work mostly the same. Compilers are run by issuing their command and they accept the name of the source code file and the name of the output file as arguments. Multiple source code files can be compiled into one executable file using the Makefile utility. This is useful for complex programs that require multiple components in order to work.

The case study also had to do with the programming environment, specifically the data types that it uses. Data types have to do with how many bits are allocated to different types of data and the range of data that is able to be expressed. The values of the multiple data types on the system are stored in a file called “limits.h,” which is a plain text file for C. The way in which the different data types work is demonstrated by using a C program that can be used to display the ranges of the various data types.

Entry 8: March 30, 2012

The topic of this lab was regular expressions, which can be used to match patterns in various Unix utilities that support them. The pattern matching of regular expressions is similar to the use of wildcards, however they are more complex and offer greater control over how patterns can be defined. Once utility that regular expressions are useful for is grep. The grep utility can search for strings of text that match certain patters, which are defined with regular expressions. The lab mostly consists of exercises where I must search files for text that matches a set of criteria. This was challenging at first and required some time to become familiar with how the different symbols are properly used. After some time spent with it I realized that regular expressions are very robust and allow patterns to be defined in multiple ways. The vi editor also makes use of regular expressions and has a substitution feature that allows certain patterns of text to be replaced with other ones. This is a useful feature when dealing with length files that contain the same patterns multiple times.

The case study was also related to regular expressions and how they can be used with different versions of the grep utility. The regular grep utility is limited in that it only accepts basic regular expressions. The egrep utility expands on the functionality of grep by allowing the use of extended regular expressions metacharacters. The fgrep utility is designed to only search for literal strings, which makes it less powerful than the other versions, but quicker and easier in situations when patterns are not necessary.

Keywords

unix Keywords

Source Code

Definition

A text file containing a program that is written out in a particular programming language. This file itself is not executable.

Demonstration

This is the source code of a simple program in C.

#include <stdio.h>
 
int main()
{
        puts("Hello, World!\n");
        return(0);
}

Compiler

Definition

A compiler is able to create executable code from a source code file.

Demonstration

This shows how to compile a source code file called “helloC.c” into an executable file called “helloC”.

lab46:~/devel$ gcc -o helloC helloC.c

Pattern Matching

Definition

Certain Unix utilities support regular expressions, which can be used to define a pattern of characters. Utilities that support this will then be able to find strings of characters that adhere to this pattern.

Demonstration

This shows how pattern matching can be used to find lines of text that match a defined pattern.

lab46:~$ cat pattern
1. dog
2. cow
3. horse
4. cat
lab46:~$ grep c.. pattern
2. cow
4. cat

Regular Expression

Definition

A regular expression is a means of defining a formula that can be used as a stand in for a string of text. These are used to specify a pattern so that lines of text following that pattern can be found.

Demonstration

This shows how lines of text can be matched to a regular expression. The regular expression is defined as a string that begins with the letters c or d followed by two more characters.

lab46:~$ cat pattern
1. dog
2. cow
3. horse
4. cat
lab46:~$ grep [c-d].. pattern
1. dog
2. cow
4. cat

Job Control

Definition

Job control allows the user to view and manage the multiple processes that are running on the system.

Demonstration

The ps command can be used to show running processes on the system.

lab46:~$ ps
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
rhensen   1236  0.0  0.0  13592     8 pts/0    SNs  Jan25   0:00 /bin/bash
rhensen   1257  0.0  0.1  42608  2332 pts/0    SN+  Jan25   9:21 irssi
rhensen  20632  0.0  0.1  13640  2012 pts/27   SNs  01:30   0:00 -bash
rhensen  21082  0.0  0.0   8588  1000 pts/27   RN+  02:04   0:00 ps u

Suspend

Definition

Suspending a process allows that process to be paused, but not stopped entirely. This is done by issuing the SUSPEND character to the program (CTRL-Z).

Demonstration

The jobs command can be used to show programs that are running. Stopped jobs have been suspended, which means they are still in memory but are not currently running.

lab46:~$ jobs
[1]+  Stopped                 man man

Text Processing

Definition

Text processing involves the creation and manipulation of the ASCII data within text files.

Demonstration

There are various programs available that can be used to create or edit text. In the example below, three text editors are shown. If the text file called “text” already exists it will be opened for manipulation in the program. If the file does not exist it will be created.

lab46:~$vi text
lab46:~$vim text
lab46:~$pico text

The VI Editor

Definition

VI is a text editing utility created for Unix. It is a modal text editor, which means that there are two modes that it can operate in. The command mode interprets input as commands and the insert mode interprets input as text that should be typed into the file.

Demonstration

This is what a VI screen generally looks like. The lines of text are displayed at the top of the screen and commands that are issued are displayed at the bottom.

Hello. This is text.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"text" [New File]                                             0,0-1         All

unix Objective

unix Objective

To become more familiar with how the Unix shell operates.

Definition

Since I have been working with Unix in a command line environment, learning to use the features and understanding what the system is doing is less transparent that it is when working in a GUI. I want to obtain a better understanding of how Unix works and what happens when commands are issued.

Method

I will feel that I have gained an understanding in this area when I have a firm understanding of how the Unix system operates apart from its utilities.

Measurement

I would like to become more familiar with various aspects of the Unix environment, such as how to manage processes, how to find files, and make use of the device files.

Analysis

The lab that involved job management was particularly useful in this area. I learned a lot about how to see what processes are running on the system and the different ways to manage them. The differences between killing, suspending, and interrupting a process for example are important concepts for working with the operating system. I also found it interesting that Unix allows communication between users by writing to a device file that the user is connected through. I feel that I have also become more comfortable with using utilities like the find command and grep. I believe that these concepts have enhanced my overall understanding of what makes the operating system work when it has been broken down.

Experiments

Experiment 4

Question

What is the question you'd like to pose for experimentation? State it here.

Resources

Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment.

Hypothesis

Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment.

State your rationale.

Experiment

How are you going to test your hypothesis? What is the structure of your experiment?

Data

Perform your experiment, and collect/document the results here.

Analysis

Based on the data collected:

  • Was your hypothesis correct?
  • Was your hypothesis not applicable?
  • Is there more going on than you originally thought? (shortcomings in hypothesis)
  • What shortcomings might there be in your experiment?
  • What shortcomings might there be in your data?

Conclusions

What can you ascertain based on the experiment performed and data collected? Document your findings here; make a statement as to any discoveries you've made.

Experiment 5

Question

What is the question you'd like to pose for experimentation? State it here.

Resources

Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment.

Hypothesis

Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment.

State your rationale.

Experiment

How are you going to test your hypothesis? What is the structure of your experiment?

Data

Perform your experiment, and collect/document the results here.

Analysis

Based on the data collected:

  • Was your hypothesis correct?
  • Was your hypothesis not applicable?
  • Is there more going on than you originally thought? (shortcomings in hypothesis)
  • What shortcomings might there be in your experiment?
  • What shortcomings might there be in your data?

Conclusions

What can you ascertain based on the experiment performed and data collected? Document your findings here; make a statement as to any discoveries you've made.

Retest 2

State Experiment

The experiment that I am retesting is Mason Faucett's second experiment shown here:

http://lab46.corning-cc.edu/opus/spring2012/mfaucet2/start#experiment_2

The question posed in this experiment is whether or not it is possible to remove a directory while there are still files in it.

Resources

A resource that I would like to add to this experiment is the Wikipedia articles for the rmdir and rm commands.

http://en.wikipedia.org/wiki/Rmdir

http://en.wikipedia.org/wiki/Rm_(Unix)

These articles are useful for explaining how these two commands work and how they can be used to achieve the results that the original experiment was looking for.

Hypothesis

The original experiment's hypothesis is that it is possible to remove a directory that contains files, since in a GUI it is possible to remove a folder that contains other files. I believe that the original experiment was correct in that it is only possible to remove an empty using the rmdir command (the command's description provided in the manual page reads “remove empty directories”). However, I have found in an option for the rm command that I believe may achieve this effect.

Experiment

I was able to perform the original experiment again to show that the rmdir command cannot be used to remove a non-empty directory. What I would like to add to the experiment is attempting to remove a directory with the rm command and the -r option. The manual page for rm says that the -r option can be used to “remove directories and their contents recursively.”

Data

The results of attempting to remove a directory called “direct” with the rm -r command are shown here.

lab46:~$ rm -r direct
rm: descend into directory `direct'? y
rm: remove regular empty file `direct/file1'? y
rm: remove regular empty file `direct/file2'? y
rm: remove directory `direct'? y

Analysis

While the -r option works as described and the directory and its contents were removed, there was a prompt to confirm that each file should be removed. This seems to be a problem in the case of removing directories with a large number of files. I decided to try to streamline this process by adding the -f option as well, which will make the rm command never prompt. The results of removing the same directory called “direct” are below.

lab46:~$ rm -rf direct

By simply issuing the command, the directory and all of it's contents are removed without prompting the user or displaying any messages.

Conclusions

The rm command will actually remove a directory by first removing the contents and then the directory itself, which means that once again only an empty directory can be removed. In this sense, the original experiment was correct. However, this invocation of the rm command seems to be consistent with the results that the original experiment was trying to achieve since it removes a directory and its contents with a single command.

opus/spring2012/rhensen/part2.txt · Last modified: 2012/04/03 16:06 by rhensen