Day 02: In-Class Assignment: Command line interface#

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

Table of Contents#

Learning goals#

By the end of this assignment, you should be able to:

  • Be more comfortable using the command line interface to:

    • create and move between directories on your computer

    • move, copy, and edit files

    • download files from the internet

    • run Python scripts

  • Be able to Enter the “vi” text editor, edit a file, and gracefully exit the text editor (all from starting on the command line)

  • Be able to run command line commands from inside of a Jupyter notebook


Part 1. Revisting common command line commands#

✅ Do this: Before you dive into today’s activity, take a moment to remind yourself of some of the commands that came up in the pre-class assignment. If you don’t remember what they do, check in your with your group or your instructor.

Note: there might be a few in this list that didn’t come up, so make note of them and figure out what they do!

  • clear

  • pwd

  • ls

  • cd [directory]

  • cd ..

  • cd .

  • cd ~

  • cd -

  • cd /

  • touch [new file]

  • cp

    • cp [file] [new file]

    • cp -r [folder] [new folder]

  • mv

    • mv [path to file/folder] [new path to file/folder] (note that this can be used for relocating files or renaming files)

  • rm

    • rm [file]

    • rm -r [folder]

  • less [file]

  • more [file]

✅ Do this: Remind yourself how you can interact with Python on the command line as well by revisiting these commands:

  • python

    • import random

    • random.randint(1,100)

    • exit()

  • ipython

    • import numpy as np

    • np.linspace(0,100,5)

    • ls

    • pwd

    • cd

    • exit()


Part 2. Downloading files from the command line using curl#

Not only can you create, move, copy, and delete files on the command line, there are also commands that allow you to download files from the internet.

One such command for doing this is the curl command. Since you may not be familiar with this command, one option would be to do a quick google search for “curl unix command” which would bring up a page like this one: https://www.tutorialspoint.com/unix_commands/curl.htm

Of course, most command line commands also have a built-in set of documentation that explains what the command does and how it works. You can access this using the man command, to access the “manual” for that command.

🗒️ Task: Open up a new Terminal in JupyterHub. Once you’re there, try running the following command:

man curl

This command should bring up the manual for the curl command. You can step through this file one line at a time by pressing Return or Enter on your keyboard, or if you want to page through a page at a time you can press F or B to move forward and backward respectively. To quit out of the manual page, press Q.

🗒️ Task: Now that you can access the manual for the curl command, use it or the internet link provided above to answer the following questions:

  1. What information does the curl command need to download a file from the internet?

  2. Does curl save the data to a file by default or does it just output to the terminal? If it doesn’t output it to a file by default, what flag (using - or --) do you need to use to make it do so? Is there more than one flag that can do this? To test this out, you can experiment a bit by using the README.md file from the supplemental repository for the course:

    • https://raw.githubusercontent.com/msu-cmse-courses/cmse802-supplemental/main/README.md

✏️ Answer:

🗒️ Task: Using what you’ve learned, what would be the command to download the README.md file from the course website such that it is saved using the same filename?

✏️ Answer:

Confirm that you’re right by running your command on your terminal (if you haven’t done so already). Once you’re sure it works as intended, feel free to delete that file using rm.


Part 3. Setting up a CMSE 802 directory using the command line#

If you’ve haven’t already, in this section you’re going practice setting up a directory structure for storing content related to CMSE 802 using CLI.

🗒️ Coding Task: Using the command line interface, do the following:

  • Looking at the schedule on the class website create folders for the next three days of materials (e.g. Day03_VersionControl etc.).

  • Double-check that everything worked by running the pwd and ls commands to poke around in your new directories.

Copy in the code cell below the commands you used to complete the task.

### ANSWER

✏️ Answer:


Part 4. Modifying a Python script, but this time with the vi text editor#

In the Pre-class you have practice modifying scripts using an IDE, but you might find yourself in situations in which you can’t use an IDE and need to use the command line interface. Let’s try that now.

You’re going to download a script and modify it using a command-line-based text editor that is widely available on many systems: “vi

For this part of the activity, you’re going to work with a script that is designed to calculate the \(x\)-position and \(y\)-position of a projectile as a function of time. Since you are not expected to know the equations for projectile motion off the top of your head for this class, there are provided here:

\[x(t) = v_{0}t\cos(\theta)\]
\[y(t) = v_{0}t\sin(\theta) - \frac{1}{2}gt^2\]

where \(v_0\) is the magnitude of the initial velocity in meters per second, \(\theta\) is the angle that the projectile is launched at in radians, \(g\) is the acceleration due to gravity in meters per seconds-squared, and \(t\) is the time in seconds.

You do not need to understand the physics behind projectfile motion to complete this activity, you only need to understand what the code is doing so that you can modify it a bit.

✅ Do This: Download the following script in your scripts directory using the command line:

https://raw.githubusercontent.com/msu-cmse-courses/cmse802-supplemental/main/DailyMaterial/scripts/projectile_motion.py

Once you’ve downloaded it, test it out by running it on the command line. You should find that when the script runs, it makes an image called x_vs_time.png. You can find this image by looking for it in the same folder of the script. It should look something like this:

x-vs-time

🗒️ Task: Once you have the script downloaded and have confirmed that it works as intended, make a copy of it using the command line and call this new file projectile_motion_2.py (clever name, no?). What was the command you used to make the copy of the original?

✏️ Answer:

Opening the file with vi#

Now that you’ve got a copy of the original script, you are going to open it using the command-line-based text editor, “vi”.

IMPORTANT: Before you do anything, make sure to read the following content as it can take a bit to get used to using vi and the commands will likely not feel very intuitive at first, but it can be useful to learn how to navigate it!

  • To open a file in vi on the command line, you simply need to do “vi <file_name>” which will open the file and dump you into the vi text editor

  • When you first open vi, you will not be able to simple type things to make modifications like you might be used to. Instead, you will start out in the vi “command mode” which allow you to manipulate the file using specific letter-based commands.

  • To enter the text-editing mode that you’re familiar with, you need to press i which puts you in “insert mode”. From here, you can type normally.

  • When you are done typing in insert mode, you need to press ESC, which takes you back to command mode.

  • If you’re happy with everything you’ve done, you can type :wq which will “write” (w) and then “quit” (q). If you ever want to quit without saving your changes, you can use :q!.

✅ Do This: With the above directions in mind, let’s try opening the copy of the original script and make a few minor changes. Do the following:

  • Open projectile_motion_2.py using the vi command on the command line

  • Turn on insert mode (i) and then make the following changes to the script:

    • Change the call to the compute_trajectory function in the main function so that the velocity of projectile is 750 m s\(^{-1}\) and the launch angle is 45 degrees. Do not alter the compute_trajectory function itself.

  • After you’ve made your changes, exit insert mode (ESC).

  • Save and quit the file with :wq

  • Test out your new function by running the script on the command line. If everything went at planned you should have a new plot of \(x\) as a function of time that looks like this (indicating that the projectile traveled farther):

x-vs-time

Can we do something a little more interesting?#

Looking at a straight line isn’t all that much fun. It would be more interesting to see how the project traveled vertically since it should start out going up, but eventually slow down and change direction under the force of gravity. Wouldn’t it be nice if our script produced \(y\) as a function of time?

✅ Do This: Using vi, make a new function that plots y as a function of time you should be able to use the pre-existing plot_x_vs_time as a basis for your new function. While this can be a little tedious if you’re just using the insert mode in vi, there are some additional vi commands that can make this a little eaiser. Try out the following:

  • Once you open the file in vi, move your cursor to the front of the line where def plot_x_vs_time is. Then press v and then move your cursor down until the entire function is highlighted.

  • Once the entire function is highlighted, press y for “yank” (copy) and then move your cursor down to a blank line below the plot_x_vs_time function and press p for “paste”. You should find that a duplicate version of the new function exists. You can now use insert mode to modify that function!

There’s a lot that vi can do once you start learning some of the commands and there are a lot of resources available for learning the commands (like this simple cheat sheet).

In your new version of the Python script, make sure you call your new function inside the main function and ensure that it saves a new file called y_vs_time.png. You should get something that looks like this:

y-vs-time

Much more interesting!


Part 5. Testing out command line commands from inside a Jupyter notebook#

No that you’ve started to get comfortable with navigating the command line interface and editing files. Wer’e gonna spend the last bit of this assignment visiting how some of the same commands can be run from inside the Jupyter notebook itself.

Using “!#

One way to access command line commands from inside your jupyter notebook is to use the exclamation point/bang symbol, !. This symbol actually tells the Jupyter notebook to run the command on whatever the underlying default command prompt is for the computer where the Jupyter notebook is being run. In the case of JupyterHub, this is the “Terminal” that we’ve already talked a bit about, but if you’re running the Jupyter notebook on you computer locally, it may be something different, so be aware that this behavior may change depending on what computer/system you’re using!

✅ Do This: Try running the following cell to see what happens. Do the results make sense?

!pwd
!ls

✅ Do This: Carefully try executing some of the other command line commands you’ve learned using !. As always, make sure you know what you expect the command to before you run it! Did all of the commands you tried work? Which, if any, didn’t? You may want to experiment with using more than one cell.

# Test out some "!" commands here

Using the built-in IPython command line commands#

Because Jupyter notebooks are also IPython notebooks you can run some of the same commands that are available to you when you’re using the IPython command prompt.

✅ Do This: Run the following two cells. Do the results makes sense?

pwd
ls

🗒️ Task: What happens if you combined the above to commands into one cell? Does it work? If not, why not? What is happening with how the Jupyter notebook is interpreting the information?

### ANSWER

# Test out putting both commands in the same cell here

✏️ Answer:

✅ Do This: Experiment with other commands to determine which ones work and which ones don’t. Proceed with caution!

# Experiment here and create cells as necessary

Congratulations, you’re done!#

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

NOTE: You must upload your script too.


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