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. If you are getting a “permission denied” notice, waiting a couple minutes may resolve the issue.
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. Using FastX on a macOS M1 chip may generate privacy issues. To bypass this, you can (1) Control-click the FastX app and select Open on the menu, then click Open on the dialog or (2) Open System Preferences > Security + Privacy, and then click 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
We are 23 years in a future where Tim has stopped giving talks. Even so, he enjoys reminiscing about all the talks he’s given by looking through the recordings stored on his computer. On one misfortunate evening, Tim’s computer breaks, and he loses all records of his talks. Without records to refer to, Tim wonders when the last time he gave a talk was, to which you tell him it was 23 years ago. Tim 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 23 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 23 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 installlab1
and notlab01
. 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 commandcd ..
Section 2: Move Like A Snake (Python)
Problem 1
Now that you’ve finally found the python file, you can debug the code and prove that 22 years ago was before 2010.
Instructions
To view the 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 inyears_checker.py
has the correct value, 2023. - 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.