2440:145 Operating Systems Study Guide #3 The University of Akron Summit College Dept of Business Technology Computer Information Systems The Program Development Cycle Program – a set of instructions that guide the computer to perform basic arithmetic and logical operations Program development cycle (Programming) – the process of writing instructions (programs) for a computer to solve a problem. The steps necessary include: 1. Creating program specifications – the requirements the application must meet 2. Designing the application – create file formats, screen layouts, and algorithms Algorithm – a sequence of procedures, programming code, or commands that are used as part of a program or result in a program Tools used in designing the sequential and logical structure of a program include: o Flowchart – uses symbols for program design o Pseudocode – uses natural English for program design 3. 4. 5. Writing source code – the code must be written, tested and debugged Source code (file) – the file with the actual code which is written with a programming language of choice in a text editor such as the vi editor Debugging – the process of going through program code to locate errors and fixing them. o Syntax errors – grammatical mistakes in program language use o Logical errors – logical mistakes in a program Compiling/Interpreting programs – the source code is translated in its entirety at one time (compiled) or a single line at a time (interpreted) from high-level program instructions into machine language, and executed if no errors are found (run). Compiler – a system software program that translates an entire high-level program (source code) at one time into machine language (object code grouped together in files called library files) that the computer can interpret and execute. It gives no feedback until the entire program has been compiled. Interpreter – a system software program that translates a single line at a time in a program. An immediate feedback is given if the code contains an error. Executing (running) program – the compiled or interpreted code is executed if no errors are found. Executable code – a linker or link editor resolves object code references to other programs by creating an executable code. Programming Languages Programming languages have evolved in generations and are improved over the years. The generations of programming languages include: o Low-level languages – include: Machine language (1st Generation) – instructions are coded as a series of 0s and 1s and are unique to a particular computer Assembler language (2nd Generation) – instructions are coded using some recognized symbols called mnemonics (memory aids) and are also unique to a particular computer. Example, MUL is used to represent a multiply instruction. An assembler translates the mnemonics into 0s and 1s. o Document1 High-level languages – programs instructions are often written in English-like expressions in languages such as: 3rd Generation languages – instructions are coded at the programmer’s convenience but cannot be executed in their original form (source code). A compiler or interpreter is needed to translate the language syntax into low-level machine language to be executed. Examples include: FORTRAN (FORmula TRANslator)-1955, COBOL (Common Business Oriented Language)-1959, BASIC (Beginners All-purpose Symbolic Instruction Code)-1964, Pascal-1968, C-1972, C++-1980s, Java1990 4th Generation languages – Has a graphical user interface (GUI) that combines a code editor, compiler, debugger, linker, and executor into a single Inter-Development Environment (IDE). Examples include: Visual Basic, Delphi, PowerBuilder, Visual.NET, etc Enoch E. Damson Page 1 of 6 2440:145 Operating Systems Study Guide #3 The University of Akron Summit College Dept of Business Technology Computer Information Systems Linux Programming Utilities Some useful UNIX/Linux programming utilities include: o The make Utility – useful when a program consists of more than one file Keeps track of the source files that are changed and need recompilation, and relinks the programs if required It gets its information from a control file, which contains rules that specify source files’ dependencies and other information. o The SCCS Utility – a collection of programs that help to maintain and manage the development of programs Helps to create different versions of a program easily. Keeps track of all the changes among different versions The Programming Shell Before creating a script, the shell in which to run the script must be chosen. The different UNIX/Linux shells include: o bash – offers strong scripting and programming language features by combining the best features of the Bourne and Korn shells o sh/csh/tcsh – use operators similar to those found in the C programming language o ksh/zsh - similar to bash shell in many respects, but has syntax similar to that of C programming Writing a UNIX/Linux Shell Script Programming experience is not needed to write simple shell scripts. Linux shell utilities are used to specify elements of shell scripts. o E.g. #! /bin/bash # Displaying a message to the screen echo “Hello…” # Displaying”Hello…” to the screen Executing a UNIX/Linux Shell Script Two ways of executing shell scripts include: 1. Using the sh, bash, ksh utility (depending on the shell being used) E.g. bash hello (where hello is the name of the file) 2. Making the shell script file an executable file E.g. chmod ugo+x hello (using mnemonics to make a file executable for everyone) chmod 777 hello (using numeric values to make a file executable for everyone) ./hello (executing the hello file) Elements of Programming Languages Every programming language has the following elements: o Syntax – rules of a programming language o Comments – non executing programming language statements o Constant data – raw data used in programming such as numbers, string values, etc o Data output – displaying data on a screen or sending data to a file o Data input – receiving data from the keyboard or file o Variables – named memory locations for data storage o Data types – specifies the types of data native to the programming language o Keywords – words with a special meaning in the programming language o Operators – symbols or words that perform operations on one or more operands o Selections (Condition testing) o Repetitions (Looping structures) o Arrays o File handling o Data Structures o Algorithms Document1 Enoch E. Damson Page 2 of 6 2440:145 Operating Systems Study Guide #3 The University of Akron Summit College Dept of Business Technology Computer Information Systems UNIX/Linux Shell Scripting Features Some scripting and programming features of the UNIX/Linux shell include the following: o Comments – non executing program statements o Data output – displaying data to the screen or to files o Data input – reading data from the keyboard or from a file o Shell variables – symbolic names that temporarily store values in memory for use by shell scripts o Shell script operators – symbols used for assigning contents of a shell variable, evaluating information, performing mathematical operations, and piping or redirecting input/output o Shell Logic/control structures – include: Sequential logic – for performing a series of commands Decision logic – for branching from one point in a script to a different point Looping logic – for repeating a command several times Case logic – for choosing an action from several possible alternatives o Other special commands – for formatting screen input and positioning the cursor, commenting non-executable code, etc UNIX/Linux Built-in Programming Commands Depending on the shell being used, there are several built-in shell commands used for programming scripts including: o echo – displays the specified arguments on the output device o read – reads input from the input device and stores the input string in one or more variables. o expr – evaluates arithmetic, relational and string operations o let – stores the results of arithmetic operations in a variable o test – compares values and validates file existence o if – o case – o while – used to execute a set of commands as long as a condition is true o for – used to execute a set of commands a specified number of times o until – used to execute a set of commands as long as a condition is false o tput – formats screen text. Some options include: tput cup – moves the screen cursor to a specified row and column tput smso – enables boldfaced output tput rmso – disables boldfaced output o exit – logs out of current session or terminates a script. Below are some of the built-in commands with their respective shell environments: Command Bourne Shell Bourne Again Shell Korn Shell sh bash ksh exit sh bash ksh for sh bash ksh if bash ksh let sh bash ksh read sh bash ksh test sh bash ksh until sh bash ksh while Comments Comments – Linux comments are preceded with the pound (#) symbol o E.g. #! /bin/bash # This is a program comment Document1 Enoch E. Damson Page 3 of 6 2440:145 Operating Systems Study Guide #3 The University of Akron Summit College Dept of Business Technology Computer Information Systems Data Output The echo utility is used to send outputs to the screen. Some options include: o –n (Eliminating new line after the display. E.g. echo –n “What is your name?”) o –e (Enabling escape sequences. E.g. echo –e “What is your name? \t”) o E.g., echo “Hello…” #! /bin/bash # Displaying data to the screen echo “Hello” # Displaying data Data Input The read utility is used to send take inputs from the keyboard. Some options include: o –p prompt (Prompting users E.g. read –p “What is your name?”) o –s (Suppressing input so it does not show on the screen. E.g. read –s) o –t timeout (Limiting input within a timeframe. E.g. read –t 5) o E.g., read name #! /bin/bash # Reading data from the keyboard into and displayi data to the screen echo “What is your name?” # Displaying data read name # Reading data and storing into a variable echo “Hello $name” # Displaying data stored in a variable Variables The three types of Linux variables are: o Configuration variables – used to store information about the setup of the operating system, and after they are set up, you typically do not change them. The printenv or set commands can be used to print a list of the configuration and environment variables. o Environment variables – have initial values which can be changed as needed. E.g., PS1 o Shell local variables – created at the command line or in a shell script and useful for temporarily storing information. Some basic guidelines for handling shell variables are as follows: Omit spaces before and after the assignment operator (=) when assigning a value to a variable. E.g. x=5 (instead of x = 5) To assign a value with spaces to a variable, enclose the value in single or double quotes. E.g., fname=”John Doe” (instead of fname=John Doe) To reference a variable, use a dollar sign ($) in front of it. E.g., echo $fname To store the output of a command into a variable, enclose the command in a pair of grave accent marks (`). E.g., cls=`clear` #! /bin/bash # Assigning values to variables and displaying values from variables to the screen fname=”John Doe” # Assigning a string value to a variable age=45 # Assigning a numeric value to a variable cls=`clear` # Storing the output of the clear command in a variable $cls # Displaying value stored in a variable echo “$fname is $age years old” # Displaying values stored in the variables Shell Operators Defining Operators – used to assign a value to a variable. The equal sign (=) is one of the most common operators used to define a variable. The variable names or values that appear to the left and right of an operator are its operands. E.g., fname=Betty Evaluating Operators – used to evaluate the contents of a variable. The dollar sign ($) or the curly brackets ({}) are used as evaluating operators. E.g., echo $fname Arithmetic Operators – used to in conjunction with the expr and let commands to perform basic arithmetic. Consists of the plus sign (+) for addition, minus sign (-) for subtraction, asterisk sign (\*) for multiplication, and forward slash (\ /) for division. The use of white spaces in shell scripting is important. o E.g., let x=2+5 x=`expr 2 \* 3` Document1 Enoch E. Damson Page 4 of 6 2440:145 Operating Systems Study Guide #3 The University of Akron Summit College Dept of Business Technology Computer Information Systems #! /bin/bash # Per forming arithmetic operations x=5 # Assigning a numeric value to a variable y=10 # Assigning a numeric value to a variable let add=$x+$y # Performing an arithmetic operation (addition)…spaces/non spaces are important subtract=`expr $x - $y` # Performing an arithmetic operation (subtraction) echo “Addition: $add” # Displaying result of arithmetic operation echo “Subtraction: $subtract” # Displaying result of arithmetic operation Shell Logic Structures Sequential Logic – works so that commands are executed in the order in which they appear in the script Decision Logic – enables your script to execute statement(s) based on condition(s). o The if, if/else, if/else/if and case statements are the primary decision-making logic structures in this type of logic. o The conditions are expressed using conditional (relational) operators and enclosed in square brackets [ ], double parenthesis (( )), double square brackets [[ ]], the test command or the expr command. E.g., test $num –eq 3 It is very important to leave white spaces before and after conditions when using the above-mentioned symbols for conditional operations. E.g. [ $num –eq 3 ] o Conditional operators – used to test different categories of things including: numeric values, string values and files: Numeric values – equal to (-eq), Not equal to (-ne), greater than (-gt), greater than or equal to (-ge), less than (-lt), less than or equal to (-le). E.g. test $num –eq 3 String values – equal to (=), not equal to (!=), etc. E.g., [ $fname != “Betty” ] Files – file exists and is readable (-r), file exists and is writable (-w), etc. E.g., test –r myfile o Logical operators – used to combine multiple expressions by performing the three basic Boolean operations (AND, OR, NOT). Logical AND (-a) – combines two expressions, and tests a logical AND relationship between them. E.g., test $num –gt 10 –a $num –lt 15 Logical OR (-o) – combines two expressions, and tests a logical OR relationship between them. E.g., [ $num –gt 10 –o $fname = “Bill” ] Logical NOT (!) – negates the value of an expression. E.g., [ !$num –gt 10 ] #! /bin/bash # Program implements the if logic structure echo “Enter years in school” read years if [ $years –eq 1] then echo “Freshman” elif [ $years –eq 2 ] then echo “Sophomore” elif test $years –eq 3 then echo “Junior” elif test $years –eq 4 then echo “Senior” else echo “Invalid Choice”;; fi Document1 Enoch E. Damson Page 5 of 6 2440:145 Operating Systems Study Guide #3 The University of Akron Summit College Dept of Business Technology Computer Information Systems Case Logic – simplifies the selection of a match when there are lists of options. A common application is in creating menu selections. It simplifies some if/else/if scenarios. #! /bin/bash # Program implements the case logic structure echo “Enter years in school” read years case $years in 1) echo “Freshman”;; 2) echo “Sophomore”;; 3) echo “Junior”;; 4) echo “Senior”;; *) echo “Invalid Choice”;; esac Looping Logic – used to repeat commands or statements until specific condition(s) exist(s). o The three basic elements of a looping structure are: Initialization Condition testing Iteration (increment/decrement) o The three basic types of looping structures are: while loop – a pre-condition loop that continues to loop and execute commands or statements as long as the given condition(s) is or are true. #! /bin/bash # Program directive to run script using the bash shell # Program implements the case logic structure count=1 while [ $count –lt 5 ] do echo “Count = “ $count let count=count+1 done for loop – a pre-condition loop used to loop through a range of values. E.g., for count in 1 2 3 4 do echo “Count = “ $count done until loop – a post-condition loop that continues to loop and execute commands or statements as long as the given condition(s) is or are false. E.g., count=1 until [ $count –eq 5 ] do echo “Count = “ $count let count=count+1 done Command Line Parameters – uses positional variables $1 through $9 for the first through ninth command line parameters. Other variables include: $0 (name of script), $# (number of command line parameters), $@ or $* (all command line parameters $1 through $9), $? (exit status of last command), $$ (process ID of the executing process). Document1 Enoch E. Damson Page 6 of 6