Faculty of Science Course: Lab: Topic: CSCI 1030U: Introduction to Computer Science #1 Installing and learning the Linux/UNIX command line Installation Before we begin, we should make sure that you have Linux installed on your laptop. You have four options: 1. You could install the Linux subsystem for Windows, which installs Ubuntu in a container in Windows. This is very lightweight, so you can do this on a relatively simple computer, but it is only available in Windows 10. If you have Windows 10, this is the recommended option. Here are instructions to get it installed: https://msdn.microsoft.com/enus/commandline/wsl/install_guide 2. You could use the Terminal on another operating system, e.g. MacOS or BSD. If you have a MacBook, this is the recommended option. You won’t technically be using Linux, but you’ll be able to use the terminal, and the command line tools in this lab. The shell (command line) used by MacOS is not the same shell, but it is very similar. 3. You could run Linux in a virtual machine. A virtual machine running Xubuntu Linux is available on https://software.ontariotechu.ca/login.php, along with instructions. You can use this option if your laptop has 8GB of RAM or more. 4. You could install Linux as a separate operating system on your machine and dual boot. There is a danger that you may delete data on your hard drive if you make a mistake while partitioning, so back up all of your data before attempting this. Take extra care, and only choose this option if you know what you are doing. Part 1 Start Linux (or whichever environment from the options, above, that you chose). In this part of the lab assignment, we’ll explore Linux and the command line. Once you do, you should see a screen similar to figure 1.1 (this assumes you are running Xubuntu). Figure 1.1 – The starting screen of Xubuntu 1 Click the button at the top left. This is similar to the start menu in Windows. Choose the Terminal Emulator option. In Windows, a similar program is called Command Prompt. In both, you can type commands rather than work graphically with your operating system. Normally, it is preferred to use your operating system graphically, but sometimes a command line is necessary or more efficient. We’ll learn about some of those scenarios in this lab. Note: Your instructions may differ for other options. For example, if you installed the Linux Subsystem for Windows, run “Bash on Ubuntu on Windows,” which looks like this: Figure 1.2: How to open bash for Linux Subsystem When the Terminal Emulator (also called a shell) loads, you’ll see a prompt similar to this: yourname@computername:~$ In some command line environments, the $ symbol is the prompt symbol. It means that you can enter commands. If the symbol is not visible, you cannot type commands. Just before the $ symbol is the ~ symbol. This is a shorthand for your home directory, which each user has for their personal files. Your home directory path is similar to /home/yourname. Type pwd (print working directory) to see the path to your home directory. Directories are places where you can organize your files. Directories can store directories themselves. When you see a directory like /home/yourname, it is actually a directory called home in the root directory (/, the top level), and inside that, another directory called yourname. The / symbols are used to separate directory names and filenames. Note: Commands and filenames in Linux/UNIX are case sensitive. Type whoami to see your username. The commands pwd and whoami are programs. You execute those programs by typing their name and hitting enter. Where are these programs located? Find out using the following command (do not type the prompt, just the command that follows it): yourname@computername:~$ which pwd 2 The command which determines which program named pwd will execute if you type pwd. In this case, the pwd program is located in /bin (it might be different if you use MacOS or BSD). /bin is a directory where a bunch of core programs (sometimes called binaries) are kept. Linux finds these programs and other programs using PATH, which is an environment variable which stores a list of directories where Linux should look for programs to run. If you want to see this list of directories, type: yourname@computername:~$ echo $PATH The result is a list of directories, separated by : symbols. It is not recommended that you modify the PATH variable, until you are more experienced with Linux. When you type any command, the shell searches for a program with that name in each of the directories in PATH, in order. If they are not found, well.. let’s figure out what happens: yourname@computername:~$ fakeprogram Like with the which command above, command-line programs can take arguments (often called command-line arguments). These arguments supply additional information to commands, to help them do their job. Part 2 Since this is a programming course, we should install Python. Type the following command to install Python 3.x: yourname@computername:~$ sudo apt-get install python3 Note: You may find that Python is already installed. This is because Python is a popular language for developing applications and user interface components on Linux. The program apt-get is a program which can install/uninstall/update programs or packages. The sudo prefix allows the program to have administrator access, which apt-get needs to do its job. This could mean that the shell asks for the administrator password, which should be your password. apt-get takes two arguments, install and python3. install is the operation to be performed, and python3 is the name of the program to install. Hit enter to confirm installation, and a few minutes later you will have Python installed in Linux. To make sure it works, try the following command: yourname@computername:~$ python3 Python 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> If Python is not installed correctly, the shell will say: The program 'python3' is currently not installed. If it is installed properly, the output should be similar to that shown above. Your version may differ, but as long as your Python version is 3.6 or greater, you should have no issues with the subsequent lab assignments. Exit Python by typing the following command: >>> quit() yourname@computername:~$ Let’s make a Python program to test out our Python shell. Note: Using a graphical text editor, e.g. gedit, in the Linux Subsystem for Windows requires Gnome to be installed, which is quite large. Instead, it is recommended that you use a Windows-based text editor (e.g. VS Code), instead, for text editing in this lab assignment. In the Linux Subsystem for Windows, files are kept separate from the files accessible in Windows. This can make it awkward to use a Windows text editor, and run the program from the Linux command line. To solve this problem, create your csci1030u/labs 3 directory in your C:\Users\<your name>. Those files will be accessible from the Linux command line under the directory /mnt/c/Users/<your name>/csci1030/labs. Open the code editor of your choice. Its features are similar to many other text editors, so you should easily learn how to use it. Let’s test Python with the following program: name = 'Bob' print(f'Hello, {name}') Code listing 1.1 – A sample Python program We’ll learn more about the contents of this file later. For now, let’s just try to use the command-line compiler. Save the file as lab01.py. I strongly recommend that you create a directory to keep your files organized. On my laptop, I created a directory called /home/rfortier/csci1030/labs, and saved the file there in lab01.py. If you saved your file in a different folder than your home directory, then you’ll need to change to that directory before you can compile your program. On my system, here is how I did that: yourname@computername:~$ cd csci1030 yourname@computername:~/csci1030$ cd labs yourname@computername:~/csci1030/labs$ The command cd is short for change directory. Notice that the prompt changes to show the current directory. Make any necessary adjustments to the steps above, to match your own directory structure. Now, let’s run our program: yourname@computername:~/csci1030/labs$ python3 lab01.py Hello, Bob The command above takes the input file (lab01.py) and interprets it to figure out what to do with each line. If you have errors at this stage, check your programs for typing mistakes before proceeding. We’ve learned about the UNIX/Linux command line (shell), and have set up our system for future practice. We’ve even run our first Python program using the command line! How to Submit Upload a screenshot showing the output of your shell so that the lab instructor can confirm that you have completed this lab assignment. Your lab instructor may request that you also submit the files directly to them (e.g. in MS Teams), to make marking easier. Resources https://swcarpentry.github.io/shell-novice/ - A more detailed shell tutorial (strongly recommended for all Computer Science students!) 4