This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions if you get stuck. Aim to complete as much as possible during our meeting, but you need not hand it in. You are encouraged to work at home to complete what you do not get through today.
Learning Objectives
This exercise should help you gain familiarity with
- Unix/Linux command line interface
- a text editor and the gcc compiler
- the distributed version control tool called git.
Part 1
To begin (if you have not already done so), log into the ugrad system using your ugrad account name and password. You can do so by logging in to ugradx.cs.jhu.edu
, or any machine ugrad1.cs.jhu.edu
, ugrad2.cs.jhu.edu
, … ugrad24.cs.jhu.edu
. On a Mac or Unix/Linux machine, you will log in to ugrad from the command line using ssh
.
Using Mac terminal or Windows Powershell, you can ssh
to ugrad machines as follows:
where USERNAME is replaced by your actual cs user name. Also, you can replace 5 by x or any number between 1 and 24.
If your machine is running Windows, you will use PuTTy or ssh using Windows Powershell to connect.
You are advised to download and use PUTTY if you are a Windows user. Directly "sshing" into ugrad machines via Windows Powershell has caused an issue with keyboard layouts in the past!
Part 2
First, create a copy (clone) of the public repository for the class by taking the following steps:
-
In your ugrad account, type
cd ~
to make sure you are in your home directory. - If you have not already done so, use git to clone the class example repository by typing:
git clone https://github.com/jhu-ip/cs220-sp24-public.git
- Type:
cd cs220-sp24-public
to enter the newly created directory (that is, folder), and then type
ls
to list the files present in that directory. Review the filenames that it shows and confirm that the filesREADME
and the exercises folder are present.
If you did not successfully complete the above steps, ask of a member of the course staff and/or post on Piazza.
Part 3
Now that you have a local copy of the git repo in your ugrad account, we want to make a separate temporary directory structure in your account where you can do some work.
-
Change back to your home directory by typing either
cd ~
, or justcd
. It’s important that you’re no longer in your local copy of the repo for the next step, so typepwd
to check the name of your current directory; you should see that you’re in the folder named /home/your_username_, e.g. /home/ips22004 or similar. If you need help getting there, raise your hand (via Zoom) so we can help, or post on Piazza. -
Create a subdirectory named
temp
by typing the commandmkdir temp
.
Part 4
Now, copy a file from your local copy of the class repo into the directory you just created in part 3, then compile and test it. Here is how:
- Copy
hello_world.c
from your local copy of the class repository into your personal folder by navigating into~/temp
(the folder you just created) usingcd temp
and then typing (Hint: using tab-completion will help here!):cp ~/cs220-sp24-public/exercises/ex03b/hello_world.c .
The command above ends with a period (“dot”), and there must be a space before the dot. The dot in Unix stands for the current directory; in this command you’re expressing that the current directory is the destination for the copy you’re making.
-
Type
pwd
to confirm that you are still working in the folder named~/temp
, and then typels
to see that it now contains a copy ofhello_world.c
- Now, compile the source file named
hello_world.c
and create an executable file by typing:gcc -std=c99 -Wall -Wextra -pedantic hello_world.c
This command should create a new executable file named
a.out
. (Typels
again now to confirm that the new file has been created.) -
Execute the file you just created by typing
./a.out
and seeing what is printed. The./
to start the command above indicates to Unix that this program resides in the current directory. Unix/Linux does not look in this directory for commands by default, so without this, the command will not be found. - Now, make a copy of the
hello_world.c
file in the same folder, and name ithello_me.c
. Here is how: Type:cp hello_world.c hello_me.c
Note that the
cp
(that is, copy) command uses two arguments; the first is the source, the second is the destination. Typels
once more to confirm that you now have a new copy of the file in your folder. -
Using the emacs text editor (or vim if you prefer), modify the new file so that rather than displaying “Hello, world!” when it is executed, it instead displays “Hello, $your name$!” (e.g., “Hello, Anna!” or “Hello, Xin!”). To do this using emacs, first type
emacs hello_me.c
to open the file for editing. Once you’ve made the appropriate modifications in emacs, you can save the file usingCtrl-x Ctrl-s
, and exit emacs usingCtrl-x Ctrl-c
. - Next, compile your new file by typing
gcc -std=c99 -Wall -Wextra -pedantic hello_me.c
, then execute it by typing
./a.out
. Confirm that it behaves as you planned. If it does not, go back and re-edit the file, re-compile it, and re-run it until you get it to work.
Part 5
Wrap up the exercise by cleaning up your files, and logging out of the ugrad system, as follows:
-
While you do not want to remove or even modify the
cs220-sp24-public
folder (that is, your copy of our public class repo), you will no longer need to keep the temporary directory namedtemp
that we created today, or any of the files you put inside it. To remove thetemp
directory, first navigate to your home directory (that is, the top-level folder in your account) by typingcd ~
or simplycd
. - Once you are sure you are in the right folder, type
rm -r temp
to remove thetemp
folder and all of its contents. Note thatrm
is the remove command, and the extra-r
flag indicates that you wish the remove command to be applied recursively, meaning to all subfolders withintemp
. Be very careful when executing anyrm
command in Unix/Linux; there is no undo button if you make a mistake and delete important files! If you want to verify deletion before removing files, use the-i
option withrm
.DangerBe careful around the
rm
command from here on. Once you remove a file/folder, there will not be a (straightforward) way to recover it! - Finally, log out of the ugrad system by typing
exit
.