# IT Fundamentals/2014/Commands and Scripting

A command-line interface (CLI) is a means of interacting with a computer system or program where the user issues commands in the form of successive lines of text. A scripting language is a programming language that supports scripts, programs written for an environment that can interpret (rather than compile) and automate the execution of tasks that could alternatively be executed one-by-one by a human operator. This lesson covers commands and scripting.

## Objectives and Skills

Objectives and skills for IT Fundamentals certification are covered in detail in other lessons. This lesson helps you:
* Understand operating system commands, scripts, and scripting languages
* Understand how scripting language commands a program.

## Readings

* Wikipedia: Command-line interface
* Wikipedia: Pipeline (Unix)
* Wikipedia: Redirection (computing)
* Wikipedia: Path (computing)
* Wikipedia: cmd.exe
* Wikipedia: Terminal (OS X)
* Wikipedia: Linux console
* Wikipedia: Batch file
* Wikipedia: Shell script

## Multimedia

* YouTube: Windows Command Line Tutorial Part 1
* YouTube: Windows Command Line Tutorial Part 2
* YouTube: Terminal Tutorial - Part I: Basic Commands and Concepts
* YouTube: HakTip - Linux Terminal 101 - Getting Started
* YouTube: HakTip - Linux Terminal 101 - File Manipulation
* YouTube: HakTip - Linux Terminal 101 - Wildcards, Hard Links, and Symbolic Links

## Activities

* Complete the tutorial CodeCademy: Learn the Command Line.
* Identify partitions and file systems installed on your computer system:
** Windows: Review Microsoft: A Description of the Diskpart Command-Line Utility. Open a command prompt and use diskpart and the list disk, select disk, list partition, and list volume commands to list existing partitions and file systems.
** OS X: Review OS X Daily: List All Mounted Drives and their Partitions from the Terminal. Open a Terminal window and use the diskutil list command to list existing partitions and file systems.
** Linux: Review NixCraft: HowTo: Linux List Disk Partitions Command. Open a Terminal window and use the sudo parted -l command to list existing partitions and file systems.
* Display and navigate the file system:
** Windows: Review Microsoft: Tree Command.
*** Open a command prompt and change to the root directory (cd \).
*** Use the dir and tree commands to display the directory and directory tree.
*** Pipe the output of the directory tree through the more filter using tree | more to view the tree one screen page at a time.
*** Redirect the output of the directory tree to a text file using tree > tree.txt. Open the tree.txt text file in Notepad and view the results.
** OS X: Review MacWorld: Display a tree-like structural view of any directory.
*** Open a Terminal window and change to the root directory (cd /).
*** Use the ls and ls -R | grep ":" | sed -e 's/://' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' command to display the directory and directory tree.
*** Pipe the output of the directory tree through the more filter using ls -R | grep ":" | sed -e 's/://' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' | more to view the tree one screen page at a time.
*** Redirect the output of the directory tree to a text file using ls -R | grep ":" | sed -e 's/://' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' > tree.txt. Open the tree.txt text file in TextEdit and view the results.
** Linux: Review ComputerHope: Linux and Unix Tree Command.
*** Open a Terminal window and change to the root directory (cd /).
*** Use the ls and tree commands to display the directory and directory tree.
*** Pipe the output of the directory tree through the more filter using tree | more to view the tree one screen page at a time.
*** Redirect the output of the directory tree to a text file using tree > tree.txt. Open the tree.txt text file in gedit and view the results.
* Create a folder hierarchy and organize files
** Windows: Review Colorado.edu: Essential DOS Commands.
*** Open a command prompt.
*** Use the chdir (cd) and mkdir (md) commands to create a folder hierarchy.
*** Use the move command to organize files within the folder hierarchy.
** OS X: Review Archive.org: Learn Code the Hard Way: Command Line Crash Course.
*** Open a Terminal window.
*** Use the cd and mkdir commands to create a folder hierarchy.
*** Use the mv command to organize files within the folder hierarchy.
** Linux: Review Archive.org: Learn Code the Hard Way: Command Line Crash Course.
*** Open a Terminal window.
*** Use the cd and mkdir commands to create a folder hierarchy.
*** Use the mv command to organize files within the folder hierarchy.
* Create a script to create a folder hierarchy with a single command.
** Windows: Review WikiHow: How to Write a Batch File.
*** Open Notepad.
*** Add the md commands entered above to the Notepad file.
*** Save the file as "folders.cmd".
*** Open a command prompt and navigate to the location where you saved folders.cmd.
*** Type folders.cmd to run the batch file.
** OS X: Review Apple: Shell Script Basics.
*** Open TextEdit.
*** Start the script with #!/bin/sh.
*** Add the mkdir commands entered above to the TextEdit file.
*** Save the file as folders.sh.
*** Open a terminal window and navigate to the location where you saved folders.sh.
*** Type chmod 755 folders.sh to change permissions and make the script executable.
*** Type ./folders.sh to run the shell script.
** Linux: Review LinuxCommand.org: Writing Shell Scripts.
*** Open gedit.
*** Start the script with #!/bin/sh.
*** Add the mkdir commands entered above to the gedit file.
*** Save the file as folders.sh.
*** Open a terminal window and navigate to the location where you saved folders.sh.
*** Type chmod 755 folders.sh to change permissions and make the script executable.
*** Type ./folders.sh to run the shell script.
* Review folder and file attributes and permissions.
** Windows: Review Microsoft: Managing Permissions from the Command Line.
*** Open a command prompt.
*** Use the attrib *.* and cacls *.* commands to list folder and file attributes and permissions.
** OS X: Review PeachPit: Managing Permissions Via Command Line.
*** Open a Terminal window.
*** Use the ls -l command to list folder and file attributes and permissions.
** Linux: Review LinuxCommand.org: Permissions.
*** Open a Terminal window.
*** Use the ls -l command to list folder and file attributes and permissions.

## Lesson Summary

* A command-line interface (CLI) is a means of interacting with a computer program where the user issues commands to the program in the form of successive lines of text.
* The interface is usually implemented with a command line shell, which is a program that accepts commands as text input and converts commands to appropriate operating system functions.
* Command-line interfaces to computer operating systems are less widely used by casual computer users, who favor graphical user interfaces.
* Command-line interfaces are often preferred by more advanced computer users, as they often provide a more concise and powerful means to control a program or operating system.
* Programs with command-line interfaces are generally easier to automate via scripting.
* A command prompt (or just prompt) is a sequence of (one or more) characters used in a command-line interface to indicate readiness to accept commands.
* A CLI can generally be considered as consisting of syntax and semantics.
* The syntax is the grammar that all commands must follow. These rules also dictate how a user navigates through the system of commands.
* The semantics define what sort of operations are possible, on what sort of data these operations can be performed, and how the grammar represents these operations and data—the symbolic meaning in the syntax.
* On many Unix system and derivative systems, it is common for the prompt to end in a $ or % character if the user is a normal user, but in a # character if the user is a superuser (root).
* A command-line argument or parameter is an item of information provided to a program when it is started.
* A command-line option (also known as a flag or switch) modifies the operation of a command. Options follow the command name on the command line, separated by spaces.
* Options may be indicated by one of - -- / , : ?, and may or may not be case-sensitive, depending on the command interpreter.
* It is common for a command to be able to display a brief summary of its parameters, typically when invoked with no arguments or one of ? -? -h -H /? /h /H -help --help, but entering a command without parameters in the hope that it will display help will often run the command with default settings.
* Command documentation commonly employs a small syntax to describe the valid command form where angle brackets describe required parameters, square brackets indicate optional parameters, ellipses show repeated items, and vertical bars list a choice of items.
* Although most users think of the shell as an interactive command interpreter, it is really a programming language in which each statement runs a command.
* Most command-line interpreters support scripting to some extent, and such scripts are typically referred to as batch files (Windows) or shell scripts (Unix/Linux/OS X).
* A pipeline is a set of processes chained by their standard streams, so that the output of each process feeds directly as input to the next one.
* To use the pipeline, one writes the commands in sequence, separated by the ASCII vertical bar character "|" (which, for this reason, is often called "pipe character").
* By default, the standard error streams of the processes in a pipeline are not passed on through the pipe; instead, they are merged and directed to the console.
* Redirection is a function common to most command-line interpreters that can redirect standard streams to user-specified locations.
* Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows, using < to redirect input, and > to redirect output.
* To append output to the end of the file, rather than overwriting it, use the >> operator.
* Windows paths use a \ to separate folders and filenames. Unix-like operating systems use a / to separate folders and filenames.
* In a given path, two dots ("..") point upwards in a path hierarchy to indicate the parent directory. One dot (".") represents the current directory itself.
* The command prompt for DOS and Windows operating systems prior to Windows 2000 used the 16-bit COMMAND.COM. Windows NT, Windows 2000 and later operating systems use the 32-bit cmd.exe.
* OS X and Linux use the Terminal application to implement a Bash shell interface.
* Windows batch files, with a .bat or .cmd file extension, are text files containing a series of commands to be executed by the command-line interpreter.
* OS X and Linux shell scripts are text files, typically with a .sh extension, containing a series of commands to be interpreted by the Bash shell. These scripts must begin with #!/bin/sh as the first line, and have file permissions set to allow execution.

## Key Terms
### absolute path

    A path that points to the same location on one file system regardless of the present working directory or combined paths.

### argument

    A piece of data provided as input to a program, command, or subroutine.

### attrib

    A Windows command used to change various characteristics, or attributes of a file or directory.

### Bash

    The default command shell on Linux and Mac OS X.

### cacls

    A Windows command used to display and modify the security descriptors on folders and files.

### cd (chdir)

    A Windows and Unix-like operating system command used to change the current working directory.

### chmod

    A Unix-like operating system command used to change the access permissions on files and directories.

### compiler

    Transforms source code written in a programming language into binary code to create an executable program.

### default

    A setting or a value automatically assigned to a command without user intervention.

### dir

    A Windows command used to display file and directory listings.

### diskpart

    A Windows command used to manage hard disk partitions.

### diskutil

    An OS X command used to manage hard disk partitions

### environment variable

    A dynamic named value that can affect the way running processes will behave on a computer.

### filter

    A computer program or subroutine that processes an input stream, producing another stream.

### interpreter

    Executes source code directly, without first converting the entire program into binary code.

### ls

    A Unix-like operating system command used to display file and directory listings.

### man page (manual page)

    A form of online software documentation usually found on a Unix or Unix-like operating system.

### mkdir

    A Windows and Unix-like operating system command used to make a new directory.

### more

    A Windows and Unix-like operating system command used to view the contents of a text file one screen at a time.

### move

    A Windows command used to move one or more files or directories from one place to another.

### mv

    A command on Unix-like operating systems used to move one or more files or directories from one place to another.

### parameter

    A variable that allows for text or data provided as input to a program, command, or subroutine.

### parted (partition editor)

    A Linux command used to manage hard disk partitions.

### PATH

    An environment variable specifying a set of directories where executable programs are located.

### pipeline

    a set of processes chained by their standard streams, so that the output of each process feeds directly as input to the next process.

### relative path

    A path based on the working directory of the user or application.

### script

    Programs written for a special run-time environment that can interpret (rather than compile) and automate the execution of tasks that could alternatively be executed one-by-one by a human operator.

### shell

    A user interface for access to an operating system's services, using either a command-line interface (CLI) or graphical user interface (GUI).

### syntax

    The grammar or rules that define how commands are written.

### tree

    A Windows and Unix-like operating system command used to display a recursive directory listing.

### UNC (Universal Naming Convention)

    A common syntax used by Windows to describe the location of a network resource, such as a shared file, directory, or printer using the form \\ComputerName\SharedFolder\Resource.

### Windows PowerShell

    A task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.

## Review Questions

* A command-line interface (CLI) is _____. A command-line interface (CLI) is a means of interacting with a computer program where the user issues commands to the program in the form of successive lines of text.
* Command-line interfaces to computer operating systems are less widely used by _____. Command-line interfaces to computer operating systems are less widely used by casual computer users, who favor graphical user interfaces.
* Command-line interfaces are often preferred by _____. Command-line interfaces are often preferred by more advanced computer users, as they often provide a more concise and powerful means to control a program or operating system.
* Programs with command-line interfaces are generally easier to _____. Programs with command-line interfaces are generally easier to automate via scripting.
* A command prompt is _____. A command prompt is a sequence of characters used in a command-line interface to indicate readiness to accept commands.
* A CLI can generally be considered as consisting of _____ and _____. A CLI can generally be considered as consisting of syntax and semantics.
* The syntax is _____. The syntax is the grammar that all commands must follow. These rules also dictate how a user navigates through the system of commands.
* The semantics define _____. The semantics define what sort of operations are possible, on what sort of data these operations can be performed, and how the grammar represents these operations and data—the symbolic meaning in the syntax.
* On many Unix system and derivative systems, it is common for the prompt to end in _____. On many Unix system and derivative systems, it is common for the prompt to end in a $ or % character if the user is a normal user, but in a # character if the user is a superuser (root).
* A command-line argument or parameter is _____. A command-line argument or parameter is an item of information provided to a program when it is started.
* A command-line option _____. Options follow the command name on the command line, separated by _____. A command-line option (also known as a flag or switch) modifies the operation of a command. Options follow the command name on the command line, separated by spaces.
* Options may be indicated by one of _____ and may or may not be _____, depending on the command interpreter. Options may be indicated by one of - -- / , : ?, and may or may not be case-sensitive, depending on the command interpreter.
* It is common for a command to be able to display a brief summary of its parameters, typically when invoked with _____, but _____. It is common for a command to be able to display a brief summary of its parameters, typically when invoked with no arguments or one of ? -? -h -H /? /h /H -help --help, but entering a command without parameters in the hope that it will display help will often run the command with default settings.
* Command documentation commonly employs a small syntax to describe the valid command form where angle brackets describe _____, square brackets indicate _____, ellipses show _____, and vertical bars list _____. Command documentation commonly employs a small syntax to describe the valid command form where angle brackets describe required parameters, square brackets indicate optional parameters, ellipses show repeated items, and vertical bars list a choice of items.
* Although most users think of the shell as an interactive command interpreter, it is really _____. Although most users think of the shell as an interactive command interpreter, it is really a programming language in which each statement runs a command.
* Most command-line interpreters support scripting to some extent, and such scripts are typically referred to as _____ or _____. Most command-line interpreters support scripting to some extent, and such scripts are typically referred to as batch files (Windows) or shell scripts (Unix/Linux/OS X).
* A pipeline is _____. A pipeline is a set of processes chained by their standard streams, so that the output of each process feeds directly as input to the next one.
* To use the pipeline, one writes the commands in sequence, separated by _____. To use the pipeline, one writes the commands in sequence, separated by the ASCII vertical bar character "
* By default, the standard error streams of the processes in a pipeline are not passed on through the pipe; instead, they are _____. By default, the standard error streams of the processes in a pipeline are not passed on through the pipe; instead, they are merged and directed to the console.
* Redirection is _____. Redirection is a function common to most command-line interpreters that can redirect standard streams to user-specified locations.
* Redirection is usually implemented by _____. Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows, using < to redirect input, and > to redirect output.
* To append output to the end of the file, rather than overwriting it, use the _____ operator. To append output to the end of the file, rather than overwriting it, use the >> operator.
* Windows paths use a _____ to separate folders and filenames. Unix-like operating systems use a _____ to separate folders and filenames. Windows paths use a \ to separate folders and filenames. Unix-like operating systems use a / to separate folders and filenames.
* In a given path, two dots ("..") _____. One dot (".") represents _____. In a given path, two dots ("..") point upwards in a path hierarchy to indicate the parent directory. One dot (".") represents the current directory itself.
* The command prompt for DOS and Windows operating systems prior to Windows 2000 used _____. Windows NT, Windows 2000 and later operating systems use _____. The command prompt for DOS and Windows operating systems prior to Windows 2000 used the 16-bit COMMAND.COM. Windows NT, Windows 2000 and later operating systems use the 32-bit cmd.exe.
* OS X and Linux use _____ to implement a _____ shell interface. OS X and Linux use the Terminal application to implement a Bash shell interface.
* Windows batch files, with a _____ file extension, are text files containing _____. Windows batch files, with a .bat or .cmd file extension, are text files containing a series of commands to be executed by the command-line interpreter.
* OS X and Linux shell scripts are text files, typically with a _____ extension, containing _____. These scripts must begin with _____ as the first line, and have file permissions set to _____. OS X and Linux shell scripts are text files, typically with a .sh extension, containing a series of commands to be interpreted by the Bash shell. These scripts must begin with #!/bin/sh as the first line, and have file permissions set to allow execution.

## Assessments

* Flashcards: Quizlet: IT Fundamentals - Commands and Scripting
* Quiz: Quizlet: IT Fundamentals - Commands and Scripting

## See Also

* Windows PowerShell
* LifeHacker: Top 10 Tools that are Better in the Command Line
* HowToGeek: Beginners Guide to Shel Scripting