Operating Systems The University of Akron Dept of Business Technology Study Guide #3 Summit College Computer Information Systems



Download 56.05 Kb.
Date29.01.2017
Size56.05 Kb.
#11972

2440:145 Operating Systems The University of Akron Dept of Business Technology

Study Guide #3 Summit College 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:

  • Flowchart – uses symbols for program design

  • Pseudocode – uses natural English for program design




  1. Writing source codethe 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.

  • Syntax errors – grammatical mistakes in program language use

  • Logical errors – logical mistakes in a program

  1. 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.

  1. 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:

    • 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.




    • 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, Java-1990

      • 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

Linux Programming Utilities

  • Some useful UNIX/Linux programming utilities include:

    • 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.




    • 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:

    • bash – offers strong scripting and programming language features by combining the best features of the Bourne and Korn shells

    • sh/csh/tcsh – use operators similar to those found in the C programming language

    • 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.

    • 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)

    1. 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:

    • Syntax – rules of a programming language

    • Comments – non executing programming language statements

    • Constant data – raw data used in programming such as numbers, string values, etc

    • Data output – displaying data on a screen or sending data to a file

    • Data input – receiving data from the keyboard or file

    • Variables – named memory locations for data storage

    • Data types – specifies the types of data native to the programming language

    • Keywords – words with a special meaning in the programming language

    • Operators – symbols or words that perform operations on one or more operands

    • Selections (Condition testing)

    • Repetitions (Looping structures)

    • Arrays

    • File handling

    • Data Structures

    • Algorithms

UNIX/Linux Shell Scripting Features

  • Some scripting and programming features of the UNIX/Linux shell include the following:

    • Comments – non executing program statements

    • Data output – displaying data to the screen or to files

    • Data input – reading data from the keyboard or from a file

    • Shell variables – symbolic names that temporarily store values in memory for use by shell scripts

    • Shell script operators – symbols used for assigning contents of a shell variable, evaluating information, performing mathematical operations, and piping or redirecting input/output

    • 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

    • 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:

    • echo – displays the specified arguments on the output device

    • read – reads input from the input device and stores the input string in one or more variables.

    • expr – evaluates arithmetic, relational and string operations

    • let – stores the results of arithmetic operations in a variable

    • test – compares values and validates file existence

    • if

    • case

    • while – used to execute a set of commands as long as a condition is true

    • for – used to execute a set of commands a specified number of times

    • until – used to execute a set of commands as long as a condition is false

    • 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

    • 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

exit

sh

bash

ksh

for

sh

bash

ksh

if

sh

bash

ksh

let




bash

ksh

read

sh

bash

ksh

test

sh

bash

ksh

until

sh

bash

ksh

while

sh

bash

ksh


Comments

  • Comments – Linux comments are preceded with the pound (#) symbol

    • E.g.

#! /bin/bash

# This is a program comment


Data Output

  • The echo utility is used to send outputs to the screen. Some options include:

    • n (Eliminating new line after the display. E.g. echo –n “What is your name?”)

    • e (Enabling escape sequences. E.g. echo –e “What is your name? \t”)

    • 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:

    • p prompt (Prompting users E.g. read –p “What is your name?”)

    • s (Suppressing input so it does not show on the screen. E.g. read –s)

    • t timeout (Limiting input within a timeframe. E.g. read –t 5)

    • 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:

    • 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.

    • Environment variables – have initial values which can be changed as needed. E.g., PS1

    • 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.

    • E.g., let x=2+5

x=`expr 2 \* 3`

#! /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).

    • The if, if/else, if/else/if and case statements are the primary decision-making logic structures in this type of logic.

    • 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 ]

    • 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

    • 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


  • 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).

    • The three basic elements of a looping structure are:

      • Initialization

      • Condition testing

      • Iteration (increment/decrement)

    • 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).




Enoch E. Damson Page of


Download 56.05 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page