Lab 1: Terminal and Department Tools

Section 1: You and Your Department Machine

Howdy Y’all! Welcome to your first lab in CS0112! This is going to be a great semester of learning and expanding on your CS skills, and also taking a virtual trip around the globe. In order to make sure we have the fundamental tools to prepare for our online travel, today we are going to learn how to interact and use the department machines!

Before we start, you can access and use the department machines remotely through FastX. Below is a link to CS16’s Working Locally Guide, and you can find instructions on how to set up FastX on your local computer in section 7.

http://cs.brown.edu/courses/csci0160/local.html

Interacting with the department machines and UNIX/LINUX operating system is something that becomes more common as you begin to take higher level CS classes. Many classes, including ours, have scripts and directories that handle turning in files, copying source code to your personal directory, as well as providing demos for projects. Learning how to navigate the department system in order to find and interact with the provided resources is a useful skill to build early into the semester!

Background

Your good travelmate Sam has been trying to convince you that he was born in 2014 for the past week. Even though you are 90% sure that he was not born in 2014 because he is 21 years old. Sam, however, does not believe your math. He refuses to onboard your joint traveling journey unless your math has been proven by a computer!! He claims he ran a script on a department machine in the CIT that told him he was born in the 2010’s. Knowing that the script must be broken somewhere, you decide to fix the script to prove to Sam that 21 years ago was before 2010!

Instructions

When you first interact with a department machine through accessing the FastX cluster, you should see a login prompt asking you for your password. Within the CS department, your username and password are as follows:

Username: Brown Username Password: Brown Password

Be sure to clear the text blocks before trying to enter your username or password! Sometimes there is whitespace left in the blocks that will cause your login to fail!

Also, be sure to select XFCE after you type in your password. After you click OK on the right bottom corner of the window, you should land on a desktop screen that looks something like this:

Home Screen

If your desktop screen looks drastically different from this setup, or if you are confused by the FastX setup, please email a TA and ask about it!

In order to interact 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 the department machine, click the black box on the bottom tool bar of your home screen.

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 /gpf/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 lab01. This will setup your course directory for the course, as well as putting 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 age_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] 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 Sam’s python file, you can debug his code and prove that 21 years ago was before 2010.

Instructions

To view Sam’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 Atom, Sublime, and PyCharm.

  • Run atom <filename.py> or pycharm <filename.py> or subl <filename.py> in the terminal 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, 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 - Use the REPL to ensure that the current_year variable has the correct value, 2020. Hint: ```import datetime``` before running anything else. 
  • Finally, find and correct the error in the code. Check your solution by uncommenting the test function test_born_in_2010s and running the script again. Does your code pass all the tests?

Success!

Sam now wants to help you in your online travel journey!