Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 21 Basic Shell Programming

Conditional Statements

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

POSIX and Korn Shell provide constructs that allow a script to execute a designated set of command lines only if a special condition is met. These are called conditional statements. Discussed in this section are the following conditional statements: test, if, case, select, for, and while.

Using the test Command

This command evaluates expr; if expr evaluates true, test returns a zero exit status. If expr evaluates false, test returns a nonzero exit status.

Syntax is:

test expr

or

[ expr ]

As shown, the test command can be replaced by appropriately spaced brackets ([ ]).

An extensive list of exprs are covered in the HP-UX Reference on the test(1) manual page. Four exprs limited to the POSIX and Korn Shell are:

-L file

Returns true if file is a symbolic link.

file1 -nt file2

Returns true if file1 is newer than file2.

file1 -ot file2

Returns true if file1 is older than file2.

file1 -ef file2

Returns true if file1 has the same device and i-node number as file2, meaning that both refer to the same physical file.

One expr unique to the POSIX Shell is:

-e file

Returns true if file exists.

The conditional command [[ test_expression ]] may also be used, where test_expression is a combination of the above conditional primitives combined with the and operator, &&, the or operator, ||, and the negation operator, !.

Using the if Statement

The if statement allows you to execute one or several commands if a certain condition exists. The syntax is:

if command-line
then conditional_cmd_line1
[else conditional_cmd_line2 ]
fi

if checks for command_line true. (true means command_line returns 0.) If true, conditional_cmd_line1 executes; if not, conditional_cmd_line2 executes.

The following if statement checks whether x equals hello. If so, Welcome is printed; if not, Goodbye is printed.

$ x=hello
$ if [ $x = hello ]
> then echo Welcome
> else echo Goodbye
> fi
Welcome

In the following example, the files in the current directory are tested using brackets around the expression. Using -x tests for an executable file. If the test returns true, the executable file's name is printed.

$ for file in `ls`
> do
> if [ -x $file ]
> then echo $file is executable
> fi
> done
$

Using the case Statement

The case statement allows you to easily check conditions and then process a command line if that condition evaluates to true. The syntax is:

case string in
pattern1 [| pattern2]...) command-list1 ;;
[pattern3 [| pattern4]...) command-list2 ;; ]...
esac

The first line receives a string which is checked against each of the patterns to see if it matches. If the pattern matches, the command-list directly following is executed. For example:

$ case $i in
> -d | -r ) rmdir $dir1; echo "directory removed" ;;
> -o ) echo "option -o" ;;
> -* ) echo "not a valid option" ;;
> esac

The case statement first checks $i against each option for a match. If it matches -d or -r, the directory is removed (the | specifies logical OR). If it matches -o or -* (all others), an appropriate response is printed. If the string does not begin with -, no action is taken.

Using the select Statement

This is a command unique to the POSIX and Korn Shell. It prints on the screen a set of words each preceded by a number. It then prints the PS3 prompt and reads into the REPLY variable the line typed by the user. If the contents of that line is the number of one of the listed words, the value of parameter is set to the corresponding word. (If the line begins with anything else, parameter is set to the null string.)

Then, regardless of whether the user's input matches one of the words, the command_lines execute. (Within command_lines, conditionals can trap the nonmatches.) If the user presses Return but has input nothing, the command reprompts for input. The loop continues until it encounters a break.

The syntax is:

select parameter in words
do
command_lines
done

In the following example all the colors in words are printed out with a number in front. The default PS3 prompt, #?, is printed and the shell waits for a number. After Return is pressed, it echos that the number's corresponding color is an RGB color, and then prompts for the next entry.

It continues prompting until Break is pressed, or it receives an interrupt. If the input is 4, or anything that is not set to a color, it returns is an RGB color without a preceding color name. If the user presses Return , but has input nothing, the command reprompts for input.

$ select color in red green blue
> do
> echo $color is an RGB color.
> done
1) red
2) green
3) blue
#? 1
green is an RGB color.
#? 4
is an RBG color.
#? Break

Using the for Loop

The for loop allows you to execute a command_line once for every new value assigned to a parameter in a specified list. Syntax is:

for parameter [in list]
do
command-line
done

In the following example, the first time through the loop, the for statement sets file to x and prints it out. The second time through the loop, y is printed out and the last time, z is printed out. When the list is completely finished, the loop is exited.

$ for file in x y z
> do
> echo The file name is $file
> done

Using the while/until Loops

This loop continues executing command_line and processing through the list as long as the item in list continues to evaluate true. Once an item evaluates false, the loop is exited. The syntax is:

while list
do
list2
done

NOTE: The until loop is similar to the while loop and has the same basic syntax. However, it executes until a nonzero status is returned; the while command executes until a zero status is returned. Also, the until loop always executes at least once.

The loop in the following example initializes the variable x, and then increments and prints out the value until it equals 5 and you exit the loop.

$ x=0
$ while [ $x != 5 ]
> do
> let x=x+1
> echo $x
> done
1
2
3
4
5

Using the break Statement

This command exits loops created by the keywords for, while, until, or select.

The syntax is:

break [n]

If n is specified, it breaks out of n nested loops.

The following script checks the list of files x, y, z, none for executable files and prints the first executable file it encounters. If none is executable, file is left set to none but it is not printed.

$ for file in x y z none
> do
> if [ -x $file ]
> then echo $file
> break
> fi
> done
$

Using the continue Statement

This command skips any lines following it in a for, while, until, or select loop until the next iteration of the loop.

Syntax is:

continue [n]

If n is specified, then resume execution starting at the nth enclosing loop.

This next script checks for all executable files. If the file is executable the continue statement skips both following echo statements and starts another loop. If the file is not executable, the script prints that it is not executable. If the file is executable, nothing is printed.

$ for file in x y z
> do
> if [ -x $file ]
> then continue
> echo $file is executable
> fi
> echo $file is not executable
> done
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.