Control Structures CIS 118 Intro to LINUX
Basic Control Structures
TEST • The test utility, has many formats for evaluating expressions. For example, when given three arguments, will return the value true if the first and third argument hold the relation represented by the second argument, but it also has many options to test other properties of files, strings and integers. • The test utility can be used by stating the keyword test which is then followed by one of its many expression formats, or the test utility can simply be called by putting the expression in square brackets. • There are also compound expressions allowed in the test expression. These are built by using the -a for " and“ and the -o for "or". You can also use parentheses to set the order of precedence and the exclamation point “!” for negation. • integer1 -lt,-gt, -eq, -ne, -le, -ge integer2 • string1 = string2 • string1 != string2 • -z string (string is zero length) • -s string (string is not zero length) • -f filename (file exists) – d dirname (directory exists) • -r, -w, -x filename (tests for perimissions on a file)
IF Statements • IF-THEN • The sequence of statements is executed only if the condition is true. If the condition is false or null, the IF statement does nothing. In either case, control passes to the next statement. • The simplest form of IF statement associates a condition with a sequence of statements enclosed by the keywords THEN and FI as follows: if [ condition ] then sequence_of_statements fi
IF Statements • IF-THEN-ELSE • The sequence of statements in the ELSE clause is executed only if the condition is false or null. Thus, the ELSE clause ensures that a sequence of statements is executed. In the following example, the first UPDATE statement is executed when the condition is true, but the second UPDATE statement is executed when the condition is false or null: if [ condition ] then sequence_of_statements1 else sequence_of_statements2 fi
IF Statements • IF-THEN-ELSIF • If the first condition is false or null, the ELSIF clause tests another condition. An IF statement can have any number of ELSIF clauses; the final ELSE clause is optional. Conditions are evaluated one by one from top to bottom. If any condition is true, its associated sequence of statements is executed and control passes to the next statement. If all conditions are false or null, the sequence in the ELSE clause is executed. Consider the following example: if [ condition1] then sequence_of_statements1 elif [ condition2 ] then sequence_of_statements2 else sequence_of_statements3 fi
CASE Statement • CASE • A special version of the if-then-else where you test specific values. Each value precedes a right parenthese “)” and ends with double semi colon. “*” is the default case : • case <variable> in A) sequence_of_statements1 ;; B) sequence_of_statements2 ;; *) sequence_of_statements3 ;; esac
LOOP Statements • WHILE-LOOP • Before each iteration of the loop, the condition is evaluated. If the condition is true, the sequence of statements is executed, then control resumes at the top of the loop. If the condition is false or null, the loop is bypassed and control passes to the next statement. An example follows: • while [ condition ] do sequence_of_statements done
LOOP Statements • UNTIL-LOOP • Before each iteration of the loop, the condition is evaluated. If the condition is false or null, the sequence of statements is executed, then control resumes at the top of the loop. If the condition is true, the loop is bypassed and control passes to the next statement. An example follows: • until [ condition ] do sequence_of_statements done
LOOP Statements • FOR-LOOP • Whereas the number of iterations through a WHILE or UNTIL loop is unknown until the loop completes, the number of iterations through a FOR loop is known before the loop is entered. FOR loops iterate over a specified range of values. The syntax follows: • for <variable> in “list” do sequence_of_statements done
Recommend
More recommend