Lab 02


Exercise 0.

Check out
this tutorial on the chmod command. Set up a directory where you will keep lab exercises and homework files for this class, and set its permissions so that you and ONLY you have read-write-execute permission. Keep it this way for the entire semester.

Exercise 1.

Today we will create a Python hello world file. Python is an interpreted language. We will be using this language in later labs to solve some of our physics problems. For now we will create a simple python program. We will use the basic linux commands learned in this lab to manipulate this file.

Create a file containing the following:
#! /usr/plocal/bin/python3
print('Hello, world!')
Be sure to get all of the punctuation right. The first line tells the shell that the file you have created is to be handed to the Python interpreter. This file is your script. execute the program by typing
on the command line.

Now, set the permissions of this file to be executable such that it can be run by typing only
now remove the first line.. (#! /usr/plocal/bin/python3).. rerun using what happend?

Exercise 2.

Many of the unix commands have "options" that you can chose. These usually have the form "-X" where the minus sign indicates an option and the X indicates "option X". For example, try the head function, as in

then again with
head -n 1
The -n 1 means print out only the first n lines where n=1. Another example: convenient options for the ls command are -l ("minus ell") and -t:
ls -l *
ls -t *
ls -l  -t *
ls -lt

Exercise 3.

This is an intro to pipes and redirects. Try typing  > hello.txt
Notice the output sends the stream of characters "Hello, who is it?" to a text file called hello.txt: Here, the > means "redirect" the standard output (stuff which would normally go to your terminal) to the file hello.txt. Verify that the file was created using the "ls" command, and use the "cat hello.txt" command to confirm that the file has the expected string of characters.

Now try the previous command again. It will fail because your operating system does not want you to overwrite a file which already exists. You can for the overwrite by replacing > with >! -- the "!" character (aka "bang") will force the overwrite:  >! hello.txt
Confirm with cat that this worked.

Now try the previous command again. This time by adding to the file instead of overwriting it with >>  >> hello.txt
Confirm with cat that this worked.

Next revisit the command "grep" from the previous exercise. Type

grep HELLO hello.txt

grep hello hello.txt
Grep goes through the file hello.txt looking for lines containing the word HELLO/hello. This is not working because HELLO/hello is all capitol/small letters while the word in file is "Hello". If you go through the man pages of grep you will see an option -i for (ignore-case). Now try again using:
grep -i HELLO hello.txt

Lastly, another very useful tool is pipe. Here
is the symbol needed to pipe. A pipe takes the standard output of one thing and dumps it into the standard input of another. For example:
cat hello.txt | grep  Hello 
Note when grep has only one argument, it expects its input to come from standard input. The "|" symbol links the input of grep to the output of cat.

Exercise 4.

Use your web browser to save this html document as a text file called "tmp.txt". (e.g. with Firefox, select "File" then "Save As" from the pull-down menu.) Then view this file within an xterm window. Do this by typing

       cat tmp.txt
This spews out the whole document. Next try peeking at the beginning and end of the file with the "head" and "tail" commands,
head tmp.txt
tail tmp.txt

Now try the same thing using "more" or "less", commands you encountered in last week's lab exercise. To scroll down hit the space-bar. To scroll back up hit "b". To quit, hit "q".

Find the effect of typing

cat tmp.txt > tmp.txt2
cat tmp.txt | head > tmp.txt3

Exercise 5.

Now we consider pipes, along with processes. Start up an emacs window by typing

(type exactly this! no ampersands!). Note that your shell will be tied up running the emacs job. Suspend the job by typing ^Z (Ctrl-Z). The number that you see in the message that you job is suspended is the "job number" of this emacs process. Put the job in the background using "bg". Next start up an emacs window again. Kill the job using "kill" with a second argument set to "%" immediately followed by the job number. For example, use
kill %1

Next, type "ps" to find the process id (PID) of your emacs job, and use "kill" to terminate the job with the PID.

Use a "ps aux" to print out information about all processes running on your computer.

a = show processes for all users
u = display the process's user/owner
x = also show processes not attached to a terminal
Since this might be a long unreadable list, redirect the output of this command to a file called "tmp.4.tmp" and "grep" this file to get a list of your processes:

ps aux > tmp.4.tmp
grep yourname tmp.4.tmp
(assuming your username is "yourname"). Note that you can skip the creation of the tmp.4.tmp file by piping the output of "ps" to the input of "grep". Thus, try
ps aux | grep yourname

Mandatory Assignment

See Lab 01 for a review of the submit system.

Problem 1.

In a text file called unix_files.txt, answer the following question:

a) A text file, file.txt ( download (e.g. by right-clicking on the links)) , has 200 lines of text. How do you display (that is, print to standard output) the last 6--and only 6--lines of the file?

Problem 2.

Describe briefly the effect of typing the following statements on a unix machine. Hint: you may wish to experiment by typing only the first command, then adding the command after the first pipe, etc.

a) cat file.txt | awk '{print $2}' | sort -rn

b) cat file2.txt file3.txt | tee file4.txt | grep baboon

Download (e.g. by right-clicking on the links) sample text files file2.txt and file3.txt so you can try this out.

Submit your answers in a textfile called unix_pipes.txt.

Problem 3.

You have a 16 column file in the directory file5.txt . This is an ASCII text files containing 16 columns describing each event with the following format:

  Event name 
  date - YYMMDD
  number of good SDs
  energy, EeV , E1 
  energy, EeV,  E2 
  x core, 
  y core 
  zenith angle, [Degree]
  azimuthal angle, [Degree]
  geometry fit chi2
  geometry fit number of degrees of freedom
  alternative fit chi2
  alternative fit number of degrees of freedom 
Describe breifly what does this command do.

a) awk '{p=$1; k=$2;  print p, k }' file5.txt
b) what is the smallest and the largest (E1) energy value? submit your command line?

Submit your answers in a textfile called awk_ex.txt

Back to main Physics 2235 page