CSc 250: Lab 2

This lab was written and designed to be completed during lab-time for cs250. This lab is not graded and does not need to be turned in. However, you should work through all of the problems, as it will help you on assignments and exams. If you cannot complete the lab in the alloted time, feel free to continue at home.

Problem 1

Write a script named logged-in.sh. This script will take as input a single file (which will be the first positional argument). This file is a list of usernames, one per line of the file. The script will use a for loop to iterate through this list of names, and for each it will check if a user has an open shell session with this username.

You can use the who command to get a list of users/usernames with open shell sessions:

$ who
bddicken console  Jan 18 19:52 
bddicken ttys000  Jan 18 19:52 
bddicken ttys001  Jan 18 19:52 
...

Say that usernames.txt has the following contents:

bddicken
asd123 
sholmes

If we run logged-in.sh with this file as input, we should see something like:

$ ./logged-in.sh usernames.txt 
bddicken has one or more active shell sessions.
asd123 has no active shell sessions.
sholmes has no active shell sessions.

Click here for the solution to problem 1

Problem 2

Go through the interactive tutorial and practice problems for regular expressions at RegexOne. You probably wont be able to finish the entire thing in the alloted lab time. While in lab, only do lessons 1-10. If you finish all of the lab questions and have extra time, you can come back to this and pick up where you left off at lesson 10. I highly recommend you complete the full tutorial and all practice problems on your own. You will need to have a good understanding of regular expressions for future homeworks (both bash and python ones) and for exams.

Problem 3

In this problem, we will write several individual grep commands that match and do not match particular strings. A general pattern for writing a grep command that matches a particular regex is:

$ grep -o -E "PUT_YOUR_REGEX_HERE" input-file.txt

Below are the contents of several text files. For each one, you would write a grep command that matches all of the strings in the first section, and does not match all of the strings in the second section.

-------
BLUE
SUNSET
BATMAN
-------
batman
BatMAn
blue
sunSET
-------
-------
abc123xyz
a7777b
beautiful1sunsets
-------
123abc456
A8888W
batman
LARGE1000NUMBER
-------
-------
speech
speeeech
speeeeeeeeeeeeeeeeeeeeeeech
need
neeeeeeeeeeeeeed
impeeeeeeeeeeeeeede
-------
impede
break
book
boooooooooooook
-------