Day 02 Pre Class Assignment: Command Line Interface and Scripting#

Description of image

Student Identification#

Please do not modify the structure or format of this cell. Follow these steps:

  1. Look for the “YOUR SUBMISSION” section below

  2. Replace [Your Name Here] with your name

  3. If you worked with groupmates, include their names after your name, separated by commas

  4. Do not modify any other part of this cell

Examples:

Single student:  John Smith
Group work:      John Smith, Jane Doe, Alex Johnson

YOUR SUBMISSION (edit this line only):

✅ [Your Name Here]

Note:

  • Keep the “✅” symbol at the start of your submission line

  • Use commas to separate multiple names

  • Spell names exactly as they appear in the course roster

  • This cell has been tagged as “names” - do not modify or remove this tag

Learning Goals#

In this pre-class assignment we’ll explore how one can use the Command Line Interface (CLI) to interact with a computer and run Python scripts. At the end of this pre-class you should be able to:

  • Describe and use a Command Line Interface (CLI)

  • Run a python script from the command line.

Assignment details#

This assignment is due by 4:59 p.m. the day before class, and should be uploaded into the “Pre-class assignments” submission folder for Day 14. If you run into issues with your code, make sure to use Slack to help each other out and receive some assistance from the instructors. Submission instructions can be found at the end of the notebook.


Part 1: What is a Command Line Interface (CLI)?#

First off, in order to work through this assignment, you will need to log into the JupyterHub interface. You should log onto the engineering Jupyterhub account using the following link:

http://jupyterhub.egr.msu.edu

Once you’ve done so, upload this notebook along with the fib.py file provided on D2L and complete the rest of the assignment from within JupyterHub. If you’re already using JupyterHub, make sure you still upload the fib.py file.

At the end of the assignment, you’ll need to download the notebook from JupyterHub before you can upload it to D2L.

With this notebook opened from within JupyterHub, watch the following video for an introduction to the Engineering Jupyterhub Terminal program which is one possible Command Line Interface (CLI) that can be used for work done in this class. I highly recommend that you follow along and execute the same commands as the video. If you are watching/listening to the video on-the-go, a summary of the commands presented in the video are included below as a reference for when you get back to your computer.

# the command below actually displays a specific YouTube video,  
# with a given width and height.  You can watch the video in full-screen (much higher
# resolution) mode by clicking the little box in the bottom-right corner of the video.
from IPython.display import YouTubeVideo
YouTubeVideo("dhuZdzSblD4",width=640,height=360)

# Note: if the video doesn't work in the notebook, you can also watch it on YouTube here: https://www.youtube.com/watch?v=dhuZdzSblD4

🗒️ Task:

Answer the following questions in the next markdown cell.

  1. What is the name of the Shell that is being used by your Engineering Jupyterhub terminal program?

  2. Add a short description for each of the following commands. If you’re uncertain as to what they do, test them out in your JupyterHub terminal!

    • echo $SHELL

    • python

    • clear

    • pwd

    • ls

    • cd

    • mkdir

  3. What is the name of the command for showing the contents of the current working directory in the terminal program?

  4. What is the name of the command for creating a new directory in the terminal program? (Note this command was not in the video. You many need to try and use Google to figure this out, if you can not find the answer, let us know and include some of the keywords you used in Google as your answer).

✏️ Answer:

🗒️ Task: Let’s practice using the command line to create folders for upcoming course materials. This will help you organize your CMSE 802 workspace.

  1. Open VS Code and start a new terminal

  2. Navigate to your CMSE802 folder

  3. Looking at the schedule on the class website, create folders for Days 03, 04, and 05

  4. Paste the commands you used in the code cell below

For Mac/Linux Users You’ll use the cd command to navigate and mkdir to create directories.

For Windows Users (PowerShell) You’ll use similar commands, but with PowerShell syntax. Note that when creating multiple directories at once in PowerShell, you’ll need to separate them with commas.

Your Solution Copy your commands in the code cell below. Make sure to show both:

  1. The command you used to navigate to your CMSE802 folder

  2. The command(s) you used to create the new folders

Verification After running your commands:

  • Use ls (Mac/Linux) or dir (Windows) to verify your folders were created

  • You should see folders for Days 03, 04, and 05 in your CMSE802 directory

  • The folder names should match the topics from our course schedule: Day03_VersionControl, Day04_VersionControl_continued, and Day05_GettingData

### ANSWER

Part 2: Running some sample Python code#

Now that you have a bit of familiarity with the command line interface, you’re going to try running and modifying a python script.

On D2L, you should have downloaded fib.py and then uploaded it to JupyterHub in the same location where you uploaded this Jupyter notebook. If you didn’t do that earlier, do it now. Once you’ve uploaded the file, you should be able to click on it and have it open in a new tab. This new tab will serve as your text editor for modifying the file. You should now have 4 tabs open, one for the Jupyter notebook server, one for this notebook, one for the terminal, and one with the fib.py script open.

fib.py is very simple piece of code that should print out the Fibonnaci sequence, which should be: 0, 1, 1, 2, 3, 5, 8, 13, 21, …

🗒️ Task:

Try running the code on the command line by doing the following in your JupyerHub terminal:

$ python fib.py

Note: if this doesn’t work, it could be that you are not in the same directory as the Python script. If this is the case, you will need to navigate to the correct location using the command line command you just learned!

Once you get the script to run, review the results. Uh oh! It looks like there is a bug in this code. Review the file in the text editor window and try to figure out what is going wrong. Try to fix the code, save the file, and then run it again.

Did it work? Write down what the bug was in the cell below.

✏️ Answer:

What was the bug?:

Note: If you’re run into persistent issues with getting Python to run the script from the command line, contact your instructors as soon as possible. There might be a simple solution!


Assignment wrap-up#

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment! If running the cell doesn’t work in VS Code copy the link src and paste in the browser. Make sure to sign in with your MSU email.

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://forms.office.com/r/AEc6LS6xKF" 
	width="800px" 
	height="600px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, you’re done!#

Submit this assignment by uploading your notebook to the course Desire2Learn web page. Go to the “Pre-Class Assignments” folder, find the appropriate submission link, and upload everything there. Make sure your name is on it!


© 2024 Michigan State University. This material was created for the Department of Computational Mathematics, Science and Engineering (CMSE) at Michigan State University.