command line interpreters: talking to the os

15
Talking to The OS, Not One’s Self Daniel Vance Dr. Martin CS 4100 Spring 2013

Upload: daniel-vance

Post on 24-Oct-2015

10 views

Category:

Documents


1 download

DESCRIPTION

A basic evaluation of the CLI's for ms-dos and bash interpreters. Includes a brief history of ms-dos and BASH and their implementation. Command Line Interpreters are the text based form of communicating with the kernel to control the OS and run programs and utilities.

TRANSCRIPT

Talking to The OS, Not One’s Self

Daniel Vance

Dr. Martin

CS 4100

Spring 2013

Interacting with computers was a complicated and laborious feat of focus and understanding.

Thanks to the work done by the pioneers in the computer science field, we now have a foundation of

programming languages that range from specific uses to general purposes. Programming languages allow

users to interact with computers and allow them to manipulate them to do their bidding, with some

languages being provided with graphical user interfaces and sophisticated development environments

that make programming almost as easy as typing into a word processor. With all this sophistication

comes a lot of overhead. Most personal computers have the resources necessary to easily handle this

overhead, but what about servers? Servers handling intense workloads and network traffic need to

focus their resources on the end user, not the administrator of the server. That is where the machine’s

OS’s command line interpreter (CLI) comes into play. The CLI is the default way to interact with the

OS on Unix/Linux machines and was the original way to interact with Microsoft (MS) and IBM

machines in the early 90’s. The command line Microsoft implemented, MSDOS, was based on the unix

OS but never came close to comparing to Unix in terms of performance or features2. The CLI

implemented in Unix/Linux OS’s is known as the shell. There are many different shells, but they all

follow the same general design thread. The CLI also offers much more flexibility in the utilities that can

be used, including control structures, variables, redirecting input and output, piping, and passing

variables and arguments to utilities; all things that are either impossible or laborious, with a GUI. Finally,

CLI’s allow for script files to be ran, giving even more function to the basic OS in a style that is in line

with traditional programming languages, though not as efficient. With the increase in machine power and

resources, the CLI has taken a backseat role to the GUI, but offers a treasure trove of utilities to those

willing to look for them.

MS­DOSHistory:

The Microsoft Disk Operating System (MS­DOS) was developed by Seattle Computer

Products and purchased by MS in 1981. It was originally called the QDOS, the Quick and Dirty

Operating System, but MS changed the name to MS­DOS v1.0 when it was purchased. It was a 16­bit

OS designed to run on the IBM PC. It only supported a single user and single task, but was very

successful2. The design of MS­DOS was based on the requirement that any program designed

translated to meet Intel’s rules for running on the 8086 platform, then they would run on a machine

running MS­DOS, meaning that the hardware would no longer restrict the whether a program would run

on the machine as long as the machine was running MS­DOS. MS­DOS was also developed for speed

and efficiency. This was accomplished by keeping the OS lean, with little waste and overhead. Speed

was accomplished by “minimizing the number of disk transfers, making the needed disk transfers happen

as quickly as possible, and reducing the ... compute time and overhead of application programs3.”

Lastly, MS­DOS was required to be written in Assembly. This aided in the other design requirements,

but also was a necessity, since it was the only 8086 developing environment available to Seattle

Computer3.

Structural Organization:

MS­DOS, as an operating system, is mainly a file system. The CLI portion of MS­DOS is

found in the COMMAND.COM application program. COMMAND.COM runs the command line,

takes user interactions, and interprets them into the correct calls to complete the users command.

COMMAND.COM supports error trapping and was designed to be in two pieces, the “resident” and

“transient” sections3.

Source 3

By doing this, an application program that needs more memory can use COMMAND.COM’s

footprint. COMMAND.COM originally had a built in set of eleven commands2, but now supports many

more along with control structures. The Application programs COMMANd.COM could accept are

8086 binary “.COM” programs, “.EXE” files, and the scripting text files “.BAT.” .BAT are batch files

that use the COMMAND syntax to complete a series of COMMAND commands, with the ability to

use variables, and control structures like many other scripting languages3.

Syntactic Structure:

The MS­DOS CLI has three main parts. The first part is the prompt, which tells the user the

area in the file system the user is running commands in. The user can execute commands found in that

directory or located in the system path. The path is a sequence of directories managed by

COMMAND.COM that contain applications programs that the user often needs to execute. The

second part is the command. There are two types of commands, internal and external. Internal

commands are the ones built into COMMAND.COM by MS. External commands are the application

programs that were not built into COMMAND.COM

Internal Command External Command

C:\> DIRLists the contents of a directory

C:\> CHDSKProgram written by MS to check the harddrive for disk errors.

The last part of a command are the parameters, arguments and switches/flags that modify how a

command executes. Flags are denoted by the “/n”, where “n” has to be a predefined switch letter. .

Examples of the prompt, the command and parameters are:

C:\> DIR Lists the contents of the current directory

C:\> DIR *.txt Lists all files ending in “.txt” in the currentdirectory

C:\> DIR /s Lists the contents of the current directory andall sub directories.

C:\> DIR *.txt /s Lists all files ending in “.txt” in the currentdirectory and all sub directories.

COMMANDS.COM also has the ability to run multiple commands on one line by using the “&”

character. This does not create any sort of multitasking, but instead, just one command is executed after

the other.

C:\> cd USERS & DIR *.txt Changes the directory to C:\Users then listsall files ending in “.txt” in that directory.

MS­DOS also supports output redirection and appending by using “>” and “>>” and input redirection

using “<.” Redirection allows the user to choose to send the output somewhere other than the screen.

C:\> DIR /s *.txt > textlist.txt Displays all “.txt” files in the directory andsubdirectories and creates a file calledtextlist.txt where the output will be sent,creating a file of all the .txt file names.

C:\> SORT < textlist.txt Sorts the list of .txt files in textlist.txt

Piping is also supported by MS­DOS. Pipling allows DOS to use the output of one command as the

input of another command. The difference between piping and redirection is that redirection must go to

a destination, either a file, the screen, etc, but cannot go to a command. Piping allows the output to go

to a command3.

C:\> DIR /s *.txt | SORT | MORE Displayes all .txt files in the current andsubdirectories, then sends that output to theSORT command, which passes its results tothe MORE to display the alphabetical list of.txt files one page at a time.

Variables are defined using the SET command. SET with no arguments will show all currently defined

variables. From the prompt, defined variables cannot be addressed, except to clearing them, but are

addressable from scripting files.

C:\> SET Displays all defined variables

SET TMP=C:\TMP Sets the variable TMP to the value “C:\TMP”

C:\> SET TMP= Clears the value of TMP

Script Structure:

Script files end in the .BAT extension. They are text files, but must follow a specific structure.

Script files can accept arguments and flags. All DOS scripting files, called batch files, start off with the

@ECHO OFF line. The script can be run without this line, but the resulting script will print the output of

every command ran to the screen, just as we will see BASH does. The @ symbol tells DOS not to

display the following command at the prompt and the ECHO OFF command prevents DOS from

displaying every line it interprets.

Batch File OUTPUT

::CDUSER::Change Directory to USERcd c:/USER

C:\> CDUSERcd c:/USERC:\USER>

::CDUSER::Change Directory to USER@ECHO OFFcd C:/USER

C:\> CDUSERC:\USER>

The “::” is used for comments. where as the “:” is used for GOTO labels. The COMMAND.COM

interpreter ignores all lines beginning with a “:” except when it encounters a GOTO command, and then

it ignores all lines not begging with a “:”. By using “::” we ensure that it is not a proper GOTO label,

unless the programmer makes a label using the “:”. Batch file parameters and are accessed by using %n,

where n is the number of the argument in a list starting at %1. To access variables, they are surrounded

by %. %PATH% or %TMP% will return the value stored in the PATH and TMP variable when in a

script file12.

Control Structure:

GOTO: Batch scripting relies heavily on GOTO and labels as the “primary means of navigating

command sequences12.” GOTOs work exactly like they do in other programming languages. Every

GOTO is required to have a matching label. The label is defined by a colon then the label, but the only

not textual character that DOS will interpret is the % symbol and the label must be less than 9

characters, violating the Zero, One, or Infinity Principle.

GOTO label Line Label used Line Label DOS interprets

GOTO copyfile :copyfile :copyfile

GOTO copyfilenow :copyfilenow :copyfile

GOTO copy%file :copy%file :copy%fil

Soruce: 12

IF: DOS does support the IF command for comparing a condition. The syntax for IF is very

basic. The “Batfiles ­ The DOS batch file programming handbook12” puts the syntax for the IF

command is:

IF [NOT] EXIST FileName Command

IF [NOT] EXIST DirName\nul Command

IF [NOT] string1==string2 Command

IF [NOT] ERRORLEVEL number Command12

When comparing strings or variables, the “==” must be used. The comparison is based on value.

DOS gained wide use thanks to its availability on a wide variety of hardware, and was

considered one of the most successful operating systems to date2, but its scripting files are more than

just a long list of commands with limited control structures, it never became as powerful or as useful and

the multitasking, multiuser abilities of UNIX/LINUX.

BASH Shell:History:

AT&T’s Bell Laboratories began development of UNIX in 1969. It was rewritten in C in 1973

allowing it to be portable. Initially, UNIX was an open system, free to download the now portable

code, but as students graduating college began to become decision makers in companies, they wanted

to continue working with UNIX. Many variations of UNIX were being developed for different business

applications. In 1984 Bell Labs left AT&T and began to sell UNIX, leading to licensing fees associated

with OS’s based off of UNIX.1 After the licensing began, the GNU Project began working on free

software for UNIX and it became Brian Fox’s role to make a “clone of the Bourne Shell.”14 The shell is

the CLI for the UNIX and now Linux, which was developed in 1991 by Linus Tolvald, in his fascination

with operating systems and being fed up having to license a UNIX spinoff.13 Many of the features that

MS­DOS has were to mimic UNIX’s shell OS.2 Because of this the share many of the same

fundamentals.

Structural Organization:

The Shell of a Linux (we’ll refer to UNIX/LINUX as just Linux and will not if there is an

occasion where there is not parity in the way they function) OS is what the user interacts with when

they are using the CLI. The shell then interprets the commands or scripts and passes them to the kernel

for processing. The shell is ran in memory like the commands and applications it can run, with the kernel

managing the memory. Multiple users can interact with the kernel, with each user running one or more

shells at a time. The kernel uses process priority to determine which users interpreted commands will

run first and for how long. Depending on the users permissions, the user can use the ps command with

the -e flag to see all current processes by ID number. Since Linux does support multiple user it also

supports permissions and file ownership, which MS­DOS did not15.

The way the shell operates is a seven step process described by the GNU Bash Manual:1. Reads its input from a file, from a string supplied as an argument to the ­c invocation

option, or from the user’s terminal.

2. Breaks the input into words and operators, obeying the quoting rules. These tokens

are separated by metacharacters. Alias expansion is performed by this step.

3.Parses the tokens into simple and compound commands.

4. Performs the various shell expansions, breaking the expanded tokens into lists of

filenames and commands and arguments.

5.Performs any necessary redirections and removes the redirection operators and their

operands from the argument list.

6. Executes the command.

7. Optionally waits for the command to complete and collects its exit status. 15

Once the the command is complete, the shell returns to step one. These steps cover all possible ways

for a user and program to interact with the shell interpreter.

Both piping and redirection function the similiarly in Linux as they do in MS­DOS, using the

same symbols ( < , > , >> , | ). One major difference, though, is that the output redirector, >, can

redirect successful output to one destination, and errors to another15.

ls ­al /sbin > sbinlist.txt 2> errordump.txt ls may get an error if the user does not havepermission to /sbin.

Syntactic Structure:

Though there are many different UNIX shells, their syntax is similar, as MS­DOS has similar

syntax to UNIX. The prompt in BASH is much more useful and customizable. It can identify the user

and the relative path of the present directory like MS­DOS does, but it can also be customized to

display other information, acting almost as a variable15. The first input into the prompt must be a

command, followed by either an argument parameter, flag, or redirect.

[prompt] $ [command] [parameter/flag] [redirect]

user1~$ ls Displays the contents of the current directory.

user1~$ ls -al Displays the contents (including hidden files)and attributes of all the files in the presentdirectory.

user1~$ ls -al >> filelist.txt Redirects the output of ls ­al to the file“filelist.txt” and appends the new data to theend of the file.

The BASH shell uses the “­” to denote flags unlike the MS­DOS convention of using the “/”. Due to

this, typing in the path to a file or directory in LINUX uses the “/” character, where MS­DOS uses the

“\”. When quoting, the BASH shell uses the “\” for escape characters, where MS­DOS uses the “%”.

Also different is that LINUX is case sensitive, with commands being lower case, where MS­DOS

interprets everything as upper case.

LINUX MS­DOS

cd /user/user1 CD C:\USER\USER1

ls -l dir /l

Both versions of the commands result in the same output.

BASH also allows for variables to be defined and accessed from the command line, where

MS­DOS does not support this function. Variables are accessed by the preprocessor “$.” Variables

are assigned in the form of name = [value]. If the value is left blank, then null is assigned. BASH also

supports commands to modify values as arguments. According to the GNU Bash manual, using the

“alias, declare, typeset, export, readonly, and local builtin commands” will modify how the variable

behaves. Of note are the export command, that allows the variable to be used by other shells, than the

one in which it was set and the declare command that creates the array data structure.15

echo $SHELL The shell takes the echo command with the$SHELL variable as an argument, it calls the

echo command, which prints the value ofSHELL to the screen

PATH = $PATH:./ Sets the $PATH variable to append thepresent directory to the existing PATHvariable.

The arrays Bash supports are “one­dimensional indexed and associative” arrays.15 Bash indexing uses

integers and starts at zero. The syntax to create an indexed array is declare -a name or

name[subscript] = value15.

Script Structure:

Most all functions of the LINUX shell are available at the command line, but there are some

reserved specially for script files. Script files generally end in the .sh extension, though not necessary.

BASH script files begin the the “bash bang” or the “#!” followed by the shell the script is meant to run

in. The most common are “#!/bin/bash” or “#!/bin/sh.” In linux, the “#” symbol is used for comments

and is not read by the interpreter.

Bash supports functions in its scripts and at the command line. Functions are declared using the

“name () compound-command [ redirections ] or function name compound-command [

redirections ].15 Compound commands are control structure commands like looping and if statements

(See LINUX: Control Structure). Functions can have variables using the local command. BASH does

support tracing using the trace command, but for the trace to work inside a function, the command

must be called in the scope of the function.

Bash scripts can also be used by other users that have permission to execute the file. With the

capabilities of functions and control structures, the BASH shell interpreter is a formidable tool.

Control Structures:

The BASH version of the LINUX shell offers several methods for controlling the script and

commands. There are many more tools than MS­DOS’ GOTO and IF control mechanisms.

Looping is accomplished in BASH using until, while, for, break, and continue.15

All three loop structures are based off of the do loop structure, but each one provides a different utility.

Mechanism Syntax

until until test-commandsdo commandsdone

while while test-commandsdo commandsdone

for for name [ [in [words ...] ]] do commands done

Source15

An example of the for loop from “Bash For Loop Examples”16

#!/bin/bash

for i in 1 2 3 4 5

do

echo “Welcome $i times”

done

This script outputs:

Welcome 1 times

Welcome 2 times

Welcome 3 times

Welcome 4 times

Welcome 5 times

Bash also supports conditional commands that include if and case commands. The

conditional commands begin with the calling of the command and terminate with the calling of the

command spelled backwards. The GNU BASH manual defines the syntax and example of case as:

Mechanism Syntax

if if test-commandsthen commands[elif more-test-commands then more-commands][else alternate-commands

]fi#the elif and else in brackets areoptional

case case word in [ [ (] pattern [ |patter] ... ) command-list ;; ] esac

#!/bin/bash

echo -n "Enter the name of an animal: "

read ANIMAL

echo -n "The $ANIMAL has "

case $ANIMAL in

horse | dog | cat) echo -n "four";;

man | kangaroo ) echo -n "two";;

*) echo -n "an unknown number of";;

esac

echo " legs."15

The case command takes the value read in from the user and then matches it to one of the words it has

to compare with, then evaluates the whether there is any equality and returns the matching string15.

Conclusion:Before the days of graphical interfaces and touch screens, text based operating systems based

on command line interpreters were the state of the art technology that allowed users to push computing

to the edge of the hardware’s capabilities. Though MS­DOS has stopped development, LINUX and

the BASH shell are continually being updated with an entire community of developers and coders

working to improve the LINUX kernel that has worked its way onto nearly every type of hardware

imaginable, including Android using a modified version of it. Because of the low overhead and immense

power of the command line and BASH scripting, it is worth the time and energy to learn the nuances it

has to offer. The interpreter will never perform as fast as a program compiled in C++, but for tasks that

need to be done in a hurry or do not need to be performed a million times a day, using a quick and dirty

shell script can be the most cost effective and relatively efficient tool when looking at the cost of

development. Its power and relatively small footprint also mean that it will continue to be an invaluable

tool to network administrators to keep their systems performing properly.

Works Cited:1. Ritchie, Dennis, and Ken Thompson. "History and Timeline." The UNIX System. The Open Group,

2012. Web. 23 May 2013

2. “MS­DOS: A Brief Introduction." MS­DOS History. Linux Information Project, 30 Sept. 2006.

Web. 23 May 2013.

3. Paterson, Tiim. "An Inside Look at MS­DOS." An Inside Look at MS­DOS. Paterson Technology,

n.d. Web. 23 May 2013.

4. "MS­DOS and Command Line Overview." Basic Overview of MS­DOS Commands. Computer

Hope, 2013. Web. 23 May 2013.

5. Josephn. "MS­DOS Batch Files." Josephn.net. N.p., 31 Dec. 2010. Web. 23 May 2013

7. William, Joy. "MAN Page: Csh." Mac Developer LIbrary. Apple, 10 July 2009. Web. 23 May

2013.

12. Soucy, Laurence. "Batfiles: The DOS Batch File Programming Handbook & Tutorial."Batfiles:

The DOS Batch File Programming Handbook & Tutorial. Batfiles, 2013. Web. 23 May

2013.

13. Admin, Linux.org. "Linux Shells." ­ Linux.org. Linux.org, 3 May 2012. Web. 23 May 2013.

14. Hamilton, Naolmi. “The A­Z of Programming Languages: BASH/Bourne­Again Shell.”

ComputerWorld. 30 May 2008. Web. 23 May 2013

15. "The GNU Operating System." Bourne­Again SHell Manual. Free Software Foundation, 28 Dec.

2010. Web. 23 May 2013.

16. NIXCRAFT. "Bash For Loop Examples." FAQs About Linux/UNIX. NixCraft, 31 Oct. 2008.

Web. 23 May 2013