Lab 1: Terminal and Department Tools

Section 1: You and Your Department Machine

Hello and welcome to the first lab of CS0112! Today, we will prepare for the semester by discussing how to interact with and use the department machines.

Setting Up

Within the CS department, your username and password are as follows:

Username: Brown/Banner Username

Password: Brown/Banner Password

In Person Set-Up

If you are doing this lab in person, you can use the department machines directly. When you first interact with a CIT computer, there should be a login screen. Note there sometimes is whitespace left in the username or password blocks which can cause your login to fail. Enter your username and password to log into your computer and begin the lab! Even if your lab is in person, it may be useful to read the instructions for remote students so you can use ssh later if needed.

Remote Set-Up

If you are doing this lab remotely, you will need to set up ssh in order to access a department machine from your local computer. Ssh (Secure Shell) is a program used for logging onto a remote machine or for executing commands on a remote machine. In order to use ssh, you will need to use a terminal. We will cover what a terminal is and does later in this lab. On Mac, use command + space to open Spotlight, then type in "Terminal" and open the application which looks like a black square with white characters on the top left. On Windows, the ssh setup guide will have you install PuTTY, which you will use as a terminal.

The CS department has online ssh setup guides available here - follow this link, then choose the guide for your operating system, and follow the instructions to set up and use ssh.

Note that ssh will only work in a terminal. (For this lab, ssh will work well and is expected.) In the future, to remotely access a GUI for a department machine, use this guide to set up FastX. Although FastX is powerful, it can be unreliable at times, so if you ever need to use a department machine, we recommend using ssh. If you use FastX, be sure to click XFCE to get the full GUI.

Background

You used to be the best bean chef in the world, but you've retired, so you haven't been on the cover of Beans Weekly since 2010. Your arch-rival Bee Nice, the only chef who could ever compete with your beans, claims that he is the better bean chef because his last Beans Weekly cover photo was more recent than yours. You tell him this isn't true - you know for a fact that his last time on the cover was 21 years ago. Bee doesn't trust your math - he only trusts computations done by computers, and he says that he ran a script in the CIT which told him that 21 years ago was in the 2010s. Using a department machine, you set out to fix the script to show him that 2010 was less than 21 years ago!

Instructions

In order to interact with and run scripts that are found in the department file system, we will have to use the terminal. Terminal is a lower level, text input based version of a file system such as finder on Mac or file explorer on windows. Much like those applications, with a terminal you can navigate a file system using built-in commands provided by the terminal. A few commands that are important for navigating the system are:

ls: lists contents of the current directory you are in. cd: change directory, used to navigate from one directory to another. pwd: present working directory, prints the current directory you are in. touch: creates a file in the current directory you are in.

In order to open the terminal on a department machine, click the black box on the bottom tool bar of your home screen. If you are working remotely, open your terminal on Mac/Linux or PuTTY on Windows and execute ssh <your-login>@ssh.cs.brown.edu to open a department machine terminal.

Now that you have the terminal open, the window should look like this:

  • To start interacting with the terminal, use the ls command to check the contents of the current directory you're in. You should see names such as Public, Desktop, and Documents in the list of directories in /gpfs/main/home/[your-login].
  • For this lab, we will need to install the lab files into your course directory. In order to do this, run the command cs0112_install lab1. Make sure you install lab1 and not lab01. This will setup your course directory for the course and put the files for this lab into your course directory.
  • using the ls command along with the cd command, navigate through the new course folder and find the python file called years_checker.py!

    TIP: in order to travel into a folder that is in the current directory you are in, use the command cd as follows: cd [directory you want to go in]. For example: cd course
    TIP: To go back up a directory in the file system, use the command cd ..

Section 2: Move Like A Snake (Python)

Problem 1

Now that you've finally found Bee's python file, you can debug his code and prove that 21 years ago was before 2010.

Instructions

To view Bee's code, you will use a text editor. Text editors are programs that allow you to view and modify code. The decision to use one text editor versus another is up to personal preference. Some common ones are VSCode, Sublime, and Atom, which all are applications. Other text editors, like Nano and Vim, run directly in your terminal. If you are doing lab in person, run atom <filename.py> or nano <filename.py> or subl <filename.py> in the terminal to examine the python file. If you are using ssh, run nano <filename.py to examine the python file.

Do you notice anything wrong?

  • Check the ouput of the code by running the following command in your terminal: python3 <filename.py>. Side-note: To successfully run a python script, you must be in the same directory as the file. For example, if you have a python file saved on the Desktop, you would not be able to run it from the cs112 directory or the Documents folder.

If you would like to run Python code outside of a text editor, you can use a REPL, a Read-Eval-Print Loop. The Python REPL is basically a terminal within the terminal that interprets Python commands rather than UNIX commands. The REPL can be helpful when writing code and debugging because you can see the output of a particular command. To use the REPL, type python3 in the terminal. Try running these simple commands:

 >>> 2+2
 >>> l = [1,2,3]
 >>> l
 >>> x = 5
 >>> y = 4
 >>> x+y
  • In the REPL, run import datetime to access the datetime library, which allows you to conveniently code with dates and times.
  • Use the REPL to ensure that the current_year variable in years_checker.py has the correct value, 2021.

  • Finally, find and correct the error in the code. Check your solution by uncommenting the test function test_year_in_2010s and running the script again. Does your code pass all the tests?

Success!

Congratulations on completing the first lab of cs0112! Please call over a TA to get checked off.