Difference between revisions of "Shell"

From Coder Merlin
 
(14 intermediate revisions by 7 users not shown)
Line 2: Line 2:
{{MerlinCurriculumData|{{ROOTPAGENAME}}}}
{{MerlinCurriculumData|{{ROOTPAGENAME}}}}


{{MerlinMultipageExperience}}
== Experience ==
 
{{MerlinMultipageExperienceSubpages
{{MerlinMultipageExperienceNavBar}}
|Pages=Client and Server;First Connection;The Shell;Terminating a Partial Command;Directories;Tab Completion;Recall Previous Commands;Alter Previous Commands;Files;Deleting a File;Renaming a File;Directories Revisited;Clear the Screen;Jobs;Change Your Password;Display a Manual;Exiting the Shell
 
== Clear the Screen ==
In some cases it’s useful to clear the screen. (For example, this might be useful if you sense your guide creeping up behind you when you were doing something that you weren’t supposed to be doing. 😁) We can do this with the clear command. Type '''clear''' and then press {{Key|RETURN}}.
 
{{ConsoleLine|john-williams@codermerlin:~$ |clear}}
 
== Jobs ==
A '''program''' is an executable file which contains a series of instructions which can be executed by the computer.  A '''process''' is a ''program'' which is ''being executed'' on the computer.  The shell manages processes using the concept of a '''job'''.  You can obtain a list of jobs and suspend and resume jobs.  This functionality enables you to maximize your use of time rather than wait for the prompt to return as the shell waits for the execution of a job to complete.
 
=== Identifying Jobs ===
The shell provides several different ways to identify a job:
* <code>%''n''</code> - The specific job number is preceded by a percentage sign
* <code>%+</code> - A shortcut to specify the current job
* <code>%-</code> - A shortcut to specify the previous job
 
=== Listing Jobs ===
Jobs may be listed with the '''jobs''' command.  Let's try an example by executing a few long-running jobs.  The '''sleep''' command simply waits before returning.  Let's try it:
{{ConsoleLine|john-williams@codermerlin:~$|sleep 3}}
 
Let's try it again:
{{ConsoleLine|john-williams@codermerlin:~$|sleep 5}}
 
 
{{Observe|: Section 8|
# What do you think the argument to sleep specifies?
# Are you able to prove your hypothesis?  How?
}}
 
 
These same jobs can be executed in the background (more on this below) simply by specifying an ampersand (&) after the command. 
 
 
Let's try it:
{{ConsoleLine|john-williams@codermerlin:~$|sleep 60 &}}
 
 
{{Observe|: Section 9|
# How is the behavior different from what you expected?
# What do you think it means to run a job in the background?
# What do you think the number displayed in brackets specifies?
}}
}}


Let's run several jobs in the background and then view a list of our jobs:
{{ConsoleLine|john-williams@codermerlin:~$|sleep 40 &}}
{{ConsoleLine|john-williams@codermerlin:~$|sleep 30 &}}
{{ConsoleLine|john-williams@codermerlin:~$|sleep 20 &}}
Now, let's use the '''jobs''' command:
{{ConsoleLine|john-williams@codermerlin:~$|jobs}}
Repeatedly execute the '''jobs''' command.
{{Observe|: Section 10|
# What do you observe as you execute the '''jobs''' command?
# How do you explain your observations?
}}
=== Terminating a Job ===
While a job is executing, we may decide that we want to terminate it.  Perhaps it is taking too long or we began executing it with the wrong arguments.  We can often terminate the job with a special key sequence, {{SpecialKey|CONTROL|C}}.
Let's try an example.  We'll execute the '''cat''' command without any arguments, which will cause it to wait for something to be entered on the console. 
{{ConsoleLine|jane-williams@codermerlin:~$|cat}}
The process is now waiting.  If we realize that we made a mistake (perhaps we wanted to print the contents of a file and forgot to specify the file's name) we can terminate the job.  Try it now by pressing {{SpecialKey|CONTROL|C}}.
=== Foreground Jobs ===
Generally, when we execute a program we run it in the '''foreground'''.  This means that our shell will execute the process and we won't be able to execute the next command until the currently running foreground process exits.  This is ideal for short, interactive programs.  Whenever we execute a program, running that program in the foreground is the default.  For example, the '''find''' command can search for all files and directories within our home directory.  Let's try it:
{{ConsoleLine|jane-williams@codermerlin:~/Merlin$|cd}}
{{ConsoleLine|jane-williams@codermerlin:~$|find}}
We'll likely see a long list of files and directories quickly scroll by.  But what if we want to generate a list of all of the files and directories on the entire server?  This can take a while. Let's try it, just for fun:
{{ConsoleLine|jane-williams@codermerlin:~/$|find /}}
Eventually we'll encounter a permissions error and the process will halt.  But if you don't have the patience to wait, remember that you can use the key sequence {{SpecialKey|CONTROL|C}} to terminate the process immediately.  Alternatively, we can use the key sequence {{SpecialKey|CONTROL|Z}} to ''suspend'' the process.  When we suspend a process we can resume it in the foreground with '''fg''' followed by the job number.
=== Background Jobs ===
When we have a complex process that will take a long time to execute we don't want to be blocked from executing other commands while we wait for the long-running process to finish.  In these cases, we can execute the long-running process in the '''background'''.  Running the program in the background means that the process will continue to execute but we'll be able to enter new commands immediately.  We saw an example of this above where we executed multiple '''sleep''' jobs simultaneously.  We were able to do so because the shell was ready to accept new input ''before'' the previous job completed.
We can begin a job in the background by using an ampersand (&) after typing the command.  When we suspend a process we can resume it in the background with '''bg''' followed by the job number.
== Change Your Password ==
When you first log on to the server you'll have been assigned a password.  You should change this password to something that is easy for you to remember but difficult for others to guess (or derive).  (A good summary article of how to select strong passwords may be found [https://www.fpainc.com/blog/password-guidelines-from-nist here].)
The program that you'll use to change your password is '''passwd'''.  It will prompt you three times. 
* You'll first enter your current password
* then the new password that you want to use
* and then finally, to ensure that you're entering the new password correctly, you'll be prompted a second time for the new password.
{{Hint|
Remember that when you enter the password, what you type '''will not be echoed to the terminal'''.  You'll just need to pretend that the your keypresses are being received.
}}
Change your default password to a strong password:
{{ConsoleLines|john-williams@codermerlin:~$ passwd<br/>
Changing password for john-williams.<br/>
(current) UNIX password:<br/>
Enter new UNIX password:<br/>
Retype new UNIX password:<br/>
passwd: password updated successfully
}}
== Display a Manual ==
Finally, let's try out one more very helpful command. If we want to learn more about a command we can read its ''manual''. We do this with the command '''man'''. As an example, look at the man page for the emacs command. Type '''man emacs'''.
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |man emacs}}
The screen will be filled with information about the emacs command. While a man page is displayed we can use the following keys to navigate:
* Typing {{Key|f}} will move us forward one page in the manual
* Typing {{Key|b}} will move us backward one page in the manual
* Typing {{Key|q}} will quit (exit the manual)
Exit the manual now.
== Exiting the Shell ==
In general, there's no need to exit the shell.  It's almost always sufficient to close your terminal emulator.  An advantage to this approach is that the next time you connect to the server you'll be in exactly the same place where you left off.  However, on rare occasions the terminal emulator can become "confused" and display characters in odd places.  In such a case, there's still no need to exit; instead, use the '''reset''' command.
{{SeeAlso|
{{SeeAlso|
[[Category:Linux||Bash||SSH client||SFTP client]]|
[[Category:Linux||Bash||SSH client||SFTP client]]|
Line 140: Line 24:
== Key Concepts ==
== Key Concepts ==
{{KeyConcepts|
{{KeyConcepts|
* A '''client-server model''' is a means of distributing resources between the provider of a service and a consumer of that service, the client. The advantages of this model include:
* A '''client-server model''' is a means of distributing resources between the provider of a service and a consumer of that service, the client. The advantages of this model include:
** Users don't need to personally possess the hardware and software required to implement the service
** Users don't need to personally possess the hardware and software required to implement the service
** Users don't need to worry about installation and configuration
** Users don't need to worry about installation and configuration
** Users are able to access their data from anywhere in the world  
** Users are able to access their data from anywhere in the world  
** The state of a users data remains consistent, regardless of how and from where they access the server
** The state of a users data remains consistent, regardless of how and from where they access the server
* A '''shell''' is a user interface which provides access to an operating system’s services. There are two basic types of shells:  
* A '''shell''' is a user interface that provides access to an operating system’s services. Two basic types of shells exist:  
** A '''command-line interface''' (CLI)
** A '''command-line interface''' (CLI)
** A '''graphical user interface''' (GUI)
** A '''graphical user interface''' (GUI)
* A '''prompt''' lets you know that the server is ready and waiting for your input
* A '''prompt''' lets you know that the server is ready and waiting for your input
* A '''cursor''' is a flashing block which indicates where new characters will appear as you type
* A '''cursor''' is a flashing block that indicates where new characters will appear as you type
* A '''file''' is either a destination or source for a stream of data
* 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
* A '''process''' is a program that is being executed by the operating system
Line 156: Line 40:
** The top of this tree is called the '''root''' and is referred to by a slash (/)
** The top of this tree is called the '''root''' and is referred to by a slash (/)
** A '''subdirectory''' is a directory contained within another directory, called the '''parent directory'''
** A '''subdirectory''' is a directory contained within another directory, called the '''parent directory'''
** On Unix systems, the file structure is ''logical'' rather than ''physical''
** On Unix systems, 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)
** A '''path''' describes an ordered transition through one or more directories on the way to a destination (either a directory or a file)
*** Paths which begin at the root (symbolized by the initial slash) are called '''absolute paths'''
*** Paths that begin at the root (symbolized by the initial slash) are called '''absolute paths'''
*** All other paths are '''relative paths'''
*** All other paths are '''relative paths'''
** Your '''home directory''' is indicated by a special symbol, the ''tilde'' (~)
** Your '''home directory''' is indicated by a special symbol, the ''tilde'' (~)
Line 167: Line 51:
** The command '''cd''' changes your working directory
** The command '''cd''' changes your working directory
** The command '''pushd''' changes our working directory temporarily  
** The command '''pushd''' changes our working directory temporarily  
** The command '''popd''' returns us to our previous directory after executing pushd
** The command '''popd''' returns you to your previous directory after executing pushd
** The command '''tree''' displays a directory hierarchy
** The command '''tree''' displays a directory hierarchy
** The command '''rmdir''' removes (deletes) a directory
** The command '''rmdir''' removes (deletes) a directory
* The '''echo''' command echoes the argument to the output
* The '''echo''' command echoes the argument to the output
* The '''touch''' command creates a new, empty file, if such a file does not already exist. If such a file does already exist, the access and modification times will be set to the current time.
* The '''touch''' command creates a new, empty file, if such a file does not exist. If such a file does exists, the access and modification times will be set to the current time.
* The greater-than symbol (>) can be used to ''redirect'' output to a file
* The greater-than symbol (>) can be used to '''redirect''' output to a file
* Two greater-than symbols (>>) can be used to ''append'' output to a file
* Two greater-than symbols (>>) can be used to '''append''' output to a file
* The '''cat''' command allows us to quickly view the contents of one or more files
* The '''cat''' command allows you to quickly view the contents of one or more files
* The '''rm''' command is used to remove (delete) a file
* The '''rm''' command is used to remove (delete) a file
* The '''mv''' command is used to either move or rename a file
* The '''mv''' command is used to either move or rename a file
* The '''clear''' command clears the screen
* The '''clear''' command clears the screen
* A '''program''' is an executable file which contains a series of instructions which can be executed by the computer
* A '''program''' is an executable file that contains a series of instructions that the computer can execute
* A '''process''' is a program which is ''being executed'' on the computer
* A '''process''' is a program that is ''being executed'' on the computer
* The shell manages processes using '''jobs'''
* The shell manages processes using '''jobs'''
* The '''jobs''' command will list the current jobs associated with your shell
* The '''jobs''' command lists the current jobs associated with your shell
** A job can be terminated by using the key sequence {{SpecialKey|CONTROL|C}}
** A job can be terminated by using the key sequence {{SpecialKey|CONTROL|C}}
** A job can be suspended by using the key sequence {{SpecialKey|CONTROL|Z}}
** A job can be suspended by using the key sequence {{SpecialKey|CONTROL|Z}}
Line 195: Line 79:
<hr/>
<hr/>
''After completing W1005:''
''After completing W1005:''
* {{Assignment|J1002}} Create a journal and answer all questions in this experience. Be sure to include all sections of the journal, properly formatted. 
* {{JournalAssignment|J1002}}
<hr/>
<hr/>
''After completing W1008:''
''After completing W1008:''
* {{Assignment|M1002-10}} Complete {{MMM}} Mission M1002-10.
* {{MMMAssignment|M1002-10}}
* {{Assignment|M1002-31}} Verify your journal's conformance with {{MMM}} Mission M1002-31.
* {{MMMAssignment|M1002-31}}
}}
}}
{{Experience
{{Experience
|experienceID=W1002
|experienceID=W1002
|nextExperienceID=W1003
|experienceUnit=Lab basics
|experienceUnit=Lab basics
|knowledgeAndSkills=§10.121
|knowledgeAndSkills=§10.121
|topicAreas=SSH clients;Linux;Bash
|topicAreas=Bash;Linux;SSH clients
|classroomTime=1 hour
|classroomTime=1 hour
|studyTime=3 hours
|studyTime=3 hours
|acquiredKnowledge=differentiate between a CLI and GUI;
|acquiredKnowledge=differentiate between a CLI and GUI;
understand files, directories, and their organizational hierarchy;
understand files, directories, and their organizational hierarchy;
understand of the client-server model;
understand the client-server model;
understand processes;
understand processes;
understand the purpose of a prompt and cursor
understand the purpose of a prompt and cursor
Line 219: Line 103:
ability to delete files and directories;
ability to delete files and directories;
ability to move and rename files and directories;
ability to move and rename files and directories;
ability to successfully determine location in the file hierarchy;
ability to successfully determine your location in the file hierarchy;
ability to successfully navigate the file hierarchy;
ability to successfully navigate the file hierarchy;
ability to suspend and resume processes;
ability to suspend and resume processes;
ability to terminate processes;
ability to terminate processes;
}}
}}
{{#set:Sequenced subpages=Client and Server;First Connection;Shell;Terminating a Partial Command;Directories;Tab Completion;Recall Previous Commands;Files;Deleting a File;Renaming a File;Directories Revisited;Clear the Screen;Jobs;Change Your Password;Display a Manual;Exiting the Shell}}

Latest revision as of 13:36, 12 February 2023

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

Curriculum[edit]

ExercisesIcon.png
 Coder Merlin™  Computer Science Curriculum Data

Unit: Lab basics

Experience Name: Shell (W1002)

Next Experience: Help Me! (W1003)

Knowledge and skills:

  • §10.121 Demonstrate proficiency in managing files and processes using a command line interface

Topic areas: SSH clients; Linux; Bash

Classroom time (average): 60 minutes

Study time (average): 180 minutes

Successful completion requires knowledge: differentiate between a CLI and GUI; understand the purpose of a prompt and cursor; understand files, directories, and their organizational hierarchy; understand processes; understand the client-server model

Successful completion requires skills: ability to successfully navigate the file hierarchy; ability to create new, empty files; ability to create new text files; ability to append text to an existing file; ability to move and rename files and directories; ability to delete files and directories; ability to suspend and resume processes; ability to terminate processes; ability to successfully determine your location in the file hierarchy

Experience[edit]



👀 See Also[edit]

📺 Videos[edit]

Bash Terminal Beginner's Guide
Linus Torvalds

📖 Texts[edit]

Help Me!
Shell

📚 References[edit]


Key Concepts[edit]

Key ConceptsKeyConceptsIcon.png
  • A client-server model is a means of distributing resources between the provider of a service and a consumer of that service, the client. The advantages of this model include:
    • Users don't need to personally possess the hardware and software required to implement the service
    • Users don't need to worry about installation and configuration
    • Users are able to access their data from anywhere in the world
    • The state of a users data remains consistent, regardless of how and from where they access the server
  • A shell is a user interface that provides access to an operating system’s services. Two basic types of shells exist:
    • A command-line interface (CLI)
    • A graphical user interface (GUI)
  • A prompt lets you know that the server is ready and waiting for your input
  • A cursor is a flashing block that indicates where new characters will appear as you type
  • 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
  • All files are integrated into a single, hierarchical structure called a directory
    • A directory provides a logical grouping of related files (and potentially other directories)
    • The top of this tree is called the root and is referred to by a slash (/)
    • A subdirectory is a directory contained within another directory, called the parent directory
    • On Unix systems, 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)
      • Paths that begin at the root (symbolized by the initial slash) are called absolute paths
      • All other paths are relative paths
    • Your home directory is indicated by a special symbol, the tilde (~)
    • Your working directory can be thought of as your current directory
    • The command pwd prints the working directory
    • The command mkdir creates a new directory
    • The command ls lists the contents of a directory
    • The command cd changes your working directory
    • The command pushd changes our working directory temporarily
    • The command popd returns you to your previous directory after executing pushd
    • The command tree displays a directory hierarchy
    • The command rmdir removes (deletes) a directory
  • The echo command echoes the argument to the output
  • The touch command creates a new, empty file, if such a file does not exist. If such a file does exists, the access and modification times will be set to the current time.
  • The greater-than symbol (>) can be used to redirect output to a file
  • Two greater-than symbols (>>) can be used to append output to a file
  • The cat command allows you to quickly view the contents of one or more files
  • The rm command is used to remove (delete) a file
  • The mv command is used to either move or rename a file
  • The clear command clears the screen
  • A program is an executable file that contains a series of instructions that the computer can execute
  • A process is a program that is being executed on the computer
  • The shell manages processes using jobs
  • The jobs command lists the current jobs associated with your shell
    • A job can be terminated by using the key sequence CONTROL-C
    • A job can be suspended by using the key sequence CONTROL-Z
    • A job can be resumed in the foreground with fg
    • A job can be resumed in the background with bg
  • The man command displays the contents of a manual
  • The reset command resets your terminal

Exercises[edit]

ExercisesExercisesIcon.png

Using pencil and paper, answer all questions in this experience.


After completing W1005:

  •  J1002  Create a journal and answer all questions in this experience. Be sure to:
    • edit your journal using emacs within your ~/Journals directory
    • properly name your journal as J1002.txt
    • include all sections of the journal, properly formatted
    • push your changes to GitHub
    • properly tag your journal as J1002.Final
    • push your tag to GitHub

After completing W1008:

  •  M1002-10  Complete  Merlin Mission Manager  Mission M1002-10.
  •  M1002-31  Complete  Merlin Mission Manager  Mission M1002-31.


Experience Metadata

Experience ID W1002
Next experience ID W1003
Unit Lab basics
Knowledge and skills §10.121
Topic areas Bash
Linux
SSH clients
Classroom time 1 hour60 minutes <br />
Study time 3 hours180 minutes <br />
Acquired knowledge differentiate between a CLI and GUI
understand files, directories, and their organizational hierarchy
understand the client-server model
understand processes
understand the purpose of a prompt and cursor
Acquired skill ability to append text to an existing file
ability to create new text files
ability to create new, empty files
ability to delete files and directories
ability to move and rename files and directories
ability to successfully determine your location in the file hierarchy
ability to successfully navigate the file hierarchy
ability to suspend and resume processes
ability to terminate processes
Additional categories