Mostly MS Dos with some UNIX/LINUX The Command Line Mostly MS Dos with some UNIX/LINUX Copyright © 2003-2017 Curt Hill
Introduction The command line interface is a modified time-sharing command interface Predates GUIs Still important for sophisticated professionals such as Software Engineers Microsoft has been trying to remove it from Windows since before XP Much of what you have seen hides the command line stuff Copyright © 2003-2017 Curt Hill
Brief History First time sharing systems are in 1960s Enter a command from a printing terminal Central CPU executes UNIX developed in early 1970s Command format is the name of any program followed by parameters DOS 1 adopts this approach Single user Syntax modifications Copyright © 2003-2017 Curt Hill
Contrast Current Windows believes in two different styles of executables A GUI oriented one and a console oriented one The GUI executable registers its controls and event handlers as its first order of business By default it does not have a console A console executable does have a console It immediately starts to do the work involved Copyright © 2003-2017 Curt Hill
Shells The notion of a shell dates back to early versions of UNIX or before A shell is a command interpreter It gave an interactive means for a user to use the system UNIX allowed anyone to create their own shell, while Windows was less forthcoming in this respect On most previous versions of Windows this is the DOS command prompt – now it is PowerShell Copyright © 2003-2017 Curt Hill
Shell Scripts If the shell can accept commands from a file instead of the console this is called a shell script UNIX has a command redirection feature that enabled this The > and < on the prompt A shell script is a full but simple programming language with variables, flow of control and other features we expect in programming languages Copyright © 2003-2017 Curt Hill
The Console Often called the DOS box Only interface DOS had Recently PowerShell became the default console Paradigm is that of a printing terminal or glass teletype Sequence of lines produced by program and user Minimal editing of the line Recall previous typed line and backspace Copyright © 2003-2017 Curt Hill
Command sequence Machine gives a prompt Usually a C:...> with blinking cursor You answer with Command and <CR> If the command is valid it is executed When command is done go back to prompt Copyright © 2003-2017 Curt Hill
Command format A command has this format CMD parameters and files A command must be properly spelled A parameter is some extra info to the command DOS standard starts with a slash (/) UNIX standard starts with a dash (-) Most are optional In Windows it is the program itself that determines the format of the parameter Copyright © 2003-2017 Curt Hill
Programs The DOS box is actually cmd.exe in the Windows directory PowerShell is powershell.exe UNIX/LINUX calls this thing a shell It has a variety of shells Consistent with the UNIX philosophy Whether a DOS box or UNIX shell they are all command interpreters In early versions they were only command interpreter Now the GUI part interprets some commands Copyright © 2003-2017 Curt Hill
File specification How do we specify a file? In DOS In UNIX Files have several pieces for their name In DOS File name File extension File directory In UNIX The extension is just part of the name A dot is an ordinary character Copyright © 2003-2017 Curt Hill
File name Originally 1-8 characters Name is composed of Now they may be much larger Name is composed of Letters Digits Specials Only characters that have special meaning in DOS may not be used Name should identify the particular project UNIX is a little more restrictive Copyright © 2003-2017 Curt Hill
File type or extension Originally 0-3 characters Now may be more Separated by a dot from name Used to describe the kind of stuff in the file Predefined extensions .exe - program .bmp – bitmap picture .bat - DOS batch file A batch file will be discussed later .ps1 – PowerShell script Copyright © 2003-2017 Curt Hill
File Directory Combination of the disk letter and subdirectory In Windows/DOS disk is a letter followed by a colon In UNIX disk are represented by load points that are named not lettered Directories and subdirectories are delimited by \ in DOS and / in UNIX These may be discussed in a subsequent presentation Copyright © 2003-2017 Curt Hill
Defaults By default if subdirectory is not specified the current disk and directory are used These may be shown in the prompt The form of the prompt may be set by the user Extension Certain programs require certain extensions Each program may assume an extension or not Copyright © 2003-2017 Curt Hill
Wildcards Wildcards are ways to specify a whole group of files at once because of similarity of name * - matches as many characters as possible No characters need follow it until a . ? - matches one and only one character Wildcards are often interpreted by the program so usage varies Copyright © 2003-2017 Curt Hill
The C/C++ main function The standard main function looks like this: int main(int argc, char *argv[]) The initial int is the return value Argc is a count of command line parameters Argv is an array of character strings The actual command line parameters The program needs to interpret these as it sees fit Copyright © 2003-2017 Curt Hill
Parsing the command Def: to parse is to understand the form and meaning of communication or describe the form, function and relationships of a sentence How does DOS determine what to do with the command? Lets look Copyright © 2003-2017 Curt Hill
Executable Location Where can an executable be and be found in the command prompt? You would think that executables must be the Windows directory or one of the Program Files directories This is partially, but not fully true Executables must be on the Path The Path is an environment variable Copyright © 2003-2017 Curt Hill
Environment Variables A set of name value pairs that affect how things operate Both Windows and UNIX have the concept Both systems have a TEMP variable which identifies a directory where a program may create temporary files Both also have the PATH variable Copyright © 2003-2017 Curt Hill
The Path The Path environment variable is a string of directory names When a command needs an executable the Path directories are searched If there are multiple files of the same name, the first one found is used Windows has two paths A global one for all users A local one for each user The two are merged Copyright © 2003-2017 Curt Hill
Finding the Path Like all good Windows options there are multiple ways to examine or set Control Panel / System Advanced System Settings Copyright © 2003-2017 Curt Hill
Advanced Copyright © 2003-2017 Curt Hill
Environment Variables Copyright © 2003-2017 Curt Hill
Command Interpretation Suppose that at the prompt the command: XYZ /z abc.def is entered – what happens? The first word is the command name This may be prefixed by the disk, subdirectory etc. xyz is assumed to be name of command and /z and abc.def is passed to the command for inspection Copyright © 2003-2017 Curt Hill
Search for command 1 First the internal commands are searched for this command The internal commands are in the command processor They do not have file representations They include dir, type, ren, del, copy, etc If it is found then it is executed, otherwise the search continues Copyright © 2003-2017 Curt Hill
Search 2 Next we look in the current directory There are two extensions that can be executed: .EXE .BAT They will be searched in that order: first xyz.exe and finally xyz.bat There used to be a .com format as well If they are not found there, then the path is checked The order of the directories in the path command is then the order they will be searched Copyright © 2003-2017 Curt Hill
Path searches Like in the current directory, the first directory on the path is searched for xyz.exe and then xyz.bat Only if it is not found do we go to the second and subsequent directories If all the directories are searched and the command is not found, then we declare that this is an unknown file or command Copyright © 2003-2017 Curt Hill
Advantages The OS does not distinguish between its own external commands and foreign commands Internal commands have an advantage over other commands However external commands are just files like any other If we want to redefine we can If we want to write a new kind of command that makes life easier all we have to do is put it on the path Copyright © 2003-2017 Curt Hill
Commands We next want to consider various commands that may be useful These are typically file manipulation command Any executable can be used If it generates a window it may still be started from the command line Windows distinguishes between GUI and non-GUI executables Copyright © 2003-2017 Curt Hill
DIR Short for directory Displays the files present in the local Command alone shows all files in current directory You may also use file names and wild cards DIR is an internal command UNIX used ls with different options Copyright © 2003-2017 Curt Hill
Parameters The typical parameter of a command is a file Certain commands may take wildcards The internal commands usually have options started with / The option /? will give a page showing the format and options Non MS programs may have /h or /help instead Next screen shows this for dir Copyright © 2003-2017 Curt Hill
Dir options Copyright © 2003-2017 Curt Hill
COPY Make an identical copy of a file COPY source destination COPY a.dat b.dat Make a copy of a.dat Same disk, directory, etc Retains same date as the a.dat UNIX uses cp with different options Copyright © 2003-2017 Curt Hill
Copy Again Accepts wildcards in source file and matching in destination COPY must give it a new name or copy it elsewhere, since a directory can only have 1 identically named item Concatenation of files is allowed using + between file specs for source COPY b.dat+c.dat all.dat Copyright © 2003-2017 Curt Hill
DEL (ERASE) Removes the file from the disk Accepts wildcards DEL *.bak UNIX uses rm with different options Copyright © 2003-2017 Curt Hill
RENAME Gives a new name May abbreviate to REN May use wildcards UNIX uses mv which moves rather than renames A rename is move to same location with different name Copyright © 2003-2017 Curt Hill
The Screen The DOS screen emulates the printing terminal or glass teletype It displays the lines you typed in as well as the responses that the commands provided Since it is in its own window we can use the scrollbar and see that which happened before The CLS command will clear it Copyright © 2003-2017 Curt Hill
Cursor Keys We may use the cursor keys to modify the current command The left and right keys allows moving in the current command The delete and backspace change Insert toggles whether characters are inserted or overwritten Using the up and down key moves us through the list of commands we have used Makes it easier to reuse the typing of a previous command Copyright © 2003-2017 Curt Hill
Help command DOS still retains the HELP command which will show the commands that exist Following the HELP you may also specify the command that you wish to know more about, such as: HELP COPY Copyright © 2003-2017 Curt Hill
Directory Commands Cd or chdir changes the current directory Md or mkdir creates a new directory Rd or rmdir destroys The directory name follows the command Two specials Single dot refers to the current directory Two dots refers the ancestral Copyright © 2003-2017 Curt Hill
Addendum: Long file names The original FAT file system believed only in the 8.3 name Later, long file names were allowed These also allowed blanks as a valid character There was an algorithm that would convert that long name into an 8.3 name This produced two versions of the name that co-existed The 8.3 name always had a tilde ~ in it Copyright © 2003-2017 Curt Hill
Blanks The command processor has been modified to handle the blanks in a name A long file name may be enclosed in double quotes Then even if it has blanks it can be processed Another trick that helped is for the CD command A tab will fill in one of the available directories Copyright © 2003-2017 Curt Hill
Conclusion We now have the basics Next we learn about redirection, pipes and standard files Then batch files briefly We restart with PowerShell Copyright © 2003-2017 Curt Hill