Difference between revisions of "Shell"

From Coder Merlin
Line 3: Line 3:


{{Observe|
{{Observe|
Section 1
Section 1  
# Name at least two other services that you personally use which are implemented using a Client/Server Model
# Name at least two other services that you personally use which are implemented using a Client/Server Model
}}
}}
Line 14: Line 14:


Merlin is built upon this Client/Server Model, so you won't need any particular type of hardware or software, only a device capable of running a web-browser and a terminal emulator (available in Google Chrome).
Merlin is built upon this Client/Server Model, so you won't need any particular type of hardware or software, only a device capable of running a web-browser and a terminal emulator (available in Google Chrome).
 
== First Connection ==
== First Connection ==
Coder Merlin is a cloud-based server where we’ll be doing most of our exercises.  To access Merlin, you’ll need a '''secure shell client'''.   
Coder Merlin is a cloud-based server where we’ll be doing most of our exercises.  To access Merlin, you’ll need a '''secure shell client'''.   

Revision as of 18:13, 1 July 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

The Client and the Server[edit]

Client-server-model

A client-server model is a means of distributing resources between the provider of a service and a consumer of that service, the client. You're likely very familiar with this concept though less familiar with the name. One common example of a service provided in this manner is email. You, a user, can open an email client (for example, an app on your iPhone or Outlook on a PC) which then connects to a remote server. The server is responsible for providing several services, including receiving mail on your behalf from other servers, transmitting email on your behalf to others, and storing your email for your later use.

ObserveObserveIcon.png
Observe, Ponder, and Journal:

Section 1

  1. Name at least two other services that you personally use which are implemented using a Client/Server Model

There are several advantages to this model:

  • Users don't need to personally possess the hardware required to fulfill a particular service, only a means of connecting and communicating with a server which provides the service
  • Users don't need to worry about installing and configuring the server hardware
  • Users are able to access their data from anywhere in the world (as long as they have internet access)
  • The state of a users data remains consistent, regardless of how and from where they access the server

Merlin is built upon this Client/Server Model, so you won't need any particular type of hardware or software, only a device capable of running a web-browser and a terminal emulator (available in Google Chrome).

First Connection[edit]

Coder Merlin is a cloud-based server where we’ll be doing most of our exercises. To access Merlin, you’ll need a secure shell client.

If you’re using a Mac or a Linux based device you’ll probably have a built-in client that you can access from the terminal.

If you’re using a Windows-based device, you can access a terminal by installing Google Chrome, and then within Google Chrome typing “Secure Shell Extension” and then installing the extension.

Either way, you’ll need a connection string to access the server. The connection string will begin with your first name and last name separated by a hyphen, for example: john-williams. The rest of the connection string will be @ssh.codermerlin.com

For John Williams, the entire connection string would be:

 john-williams@ssh.codermerlin.com

After you’re connected, you’ll be prompted to enter your password. After a successful logon, you'll land in the "shell".

The Shell[edit]

A shell is a user interface which provides access to an operating system’s services. There are two basic types of shells: a “command-line interface” (CLI) and a “graphical user interface” (GUI). You are likely familiar with common GUI’s from Microsoft Windows and perhaps Apple’s macOS. This document will focus on a CLI known as bash, a shell for the Unix operating system by Brian Fox. The shell is an acronym for Bourne-again shell, named for the Bourne shell that it replaces. Login to the Merlin Server as discussed previously. After some introductory text, you’ll see a prompt which will look something like this:

john-williams@codermerlin:~$ 

You’ll see your login name, an at (@) sign, the name of the server (“codermerlin”), a colon (:), a tilde (~), and a $. This will be followed by a flashing block called a cursor. The cursor indicates where new characters will appear as you type. The cursor will continue to flash patiently as it awaits input. We’ll get back to the meaning of the tilde soon.

We'll be using a server running Linux, a Unix-like operating system. Almost everything in Unix is either a file or a process. A file is either a destination or source for a stream of data. A process is a program that is being executed by the operating system. While printers and screens are considered to be “files” in Unix, a more general way of thinking of files is a collection of data that can be referred to by name. Files can be created by users directly (for example, via a text editor) or indirectly, such as the result of running a program like a compiler.

Linux File Hierarchy

All files are integrated into a single, hierarchical directory structure that appears as an inverted tree. A directory provides a logical grouping of related files. The top of this tree is called the root and is referred to by a slash (/). In the diagram, we see that the root contains five subdirectories (i.e., a directory below the parent directory): bin, etc, home, lib, and tmp. The home directory contains two subdirectories, jack-williams and john-williams. Note that this differs from a Windows file structure as Unix integrates all files into a single directory structure rather than listing different physical storage devices. Thus, the file structure is logical rather than physical. A path describes an ordered transition through one or more directories on the way to a destination (either a directory or a file). As such, note that while both jack-williams and john-williams each have a subdirectory named "projects" these are different directories. The path to the former is "/home/jack-williams/projects" while the path to the latter is "/home/john-williams/projects". Paths which begin at the root (symbolized by the initial slash) are called absolute paths. All other paths are relative paths.

ObserveObserveIcon.png
Observe, Ponder, and Journal:

Section 2

  1. It appears that both Jack Williams and John Williams can have a directory of the same name. Do you think the ability to have two different users create a directory of the same name is important? Why?

Print Working Directory[edit]

The shell uses a concept known as your working directory, which can be thought of as your current directory, i.e. where you are currently located in the file system. If you forget where you are, you can always use the command pwd to print the working directory. Try it now. Type “pwd” followed by <RETURN>. Note that you’ll need to press <RETURN> after any command before the shell will execute the command.

john-williams@codermerlin:~$  pwd

/home/john-williams 

The command will print something similar to “/home/john-williams” and then present you with a new prompt and cursor to indicate that it’s ready and awaiting your input. This directory is your home directory and it has a special symbol, the tilde (~).

ObserveObserveIcon.png
Observe, Ponder, and Journal:

Section 3

  1. Why do you think the home directory has a special symbol to represent it?

Make a New Directory[edit]

In order to keep our Merlin Missions organized, let’s place them all under a directory named “Merlin”. To make a new directory, we’ll use the command mkdir to which we’ll provide a single argument, the name of the directory that we want to create. Try it now. Type “mkdir Merlin” followed by <RETURN>.

john-williams@codermerlin:~$  mkdir Merlin

List the Contents of a Directory[edit]

How do we find out what’s contained in our current directory? We can use the ls (that’s a lowercase ‘L’ followed by a lowercase ’S’) command to list the directory contents. Try it now. Type “ls” followed by return.

john-williams@codermerlin:~$  ls

The command will print the contents of the current directory (in this case our home directory) which will include the single directory, “Merlin”, that we created earlier.

Change to a New Directory[edit]

When we want to change our current directory we use the cd command followed by the name of the directory to which we want to change. Try it now. Type “cd Merlin”.

john-williams@codermerlin:~$ cd Merlin
john-williams@codermerlin:~/Merlin$
ObserveObserveIcon.png
Observe, Ponder, and Journal:

Section 4

  1. Did the prompt change? If so, how? Why is this useful?

Note that our prompt has now changed. Before the command was executed, the prompt contained a single tilde indicating that our home directory was our current directory. After executing the command our prompt now includes a tilde, followed by a slash, followed by “Merlin”. This indicates that we are in the directory named “Merlin” below our home directory. The slash is a delimiter separating the levels of our hierarchy.

Let’s try the pwd command again.

john-williams@codermerlin:~/Merlin$ pwd

We can now see that from the root, we’re three levels down:
/ (root)
home
john-williams
Merlin

The cd command has a useful shortcut. If we type the command without an argument, it will take us back to our home directory. It’s the equivalent of "cd ~". Let’s try it now:

john-williams@codermerlin:~/Merlin$ cd
john-williams@codermerlin:~$ cd

Now, move back to the Merlin directory by issuing the appropriate command.

Tab Completion[edit]

Very often, the shell can help us to complete file and directory names when we begin to type the name of a file (or directory) and then type TAB. Try it now. Type "cd M" and then press TAB. The shell will complete the rest of the name by appending "erlin" to what you’ve already typed. Then press ENTER. Because the completion is not ambiguous (i.e. there is only one solution) the shell will complete the name. If there were multiple possibilities, the shell would complete as much as possible and then beep. At this point, we could press TAB twice in succession to see the possible completions. Let’s try this. Within the Merlin directory, create three new directories: hello, hello2, and hello3: