PreviousNext

Conditionalizing with if Statements

Sometimes, you will want part of your script to execute only under certain conditions. Use an if statement to detect a condition and conditionally perform some operation. The syntax for an if statement is:

if test true_body else false_body

Let's say you're writing a script that searches through a list of attributes for a particular attribute. An if statement could take particular actions depending on whether an attribute exists. The following example script fragment returns an error message if the account name does not exist in the list_of_group_entries variable.

set list_of_group_entries [group list $group -simplename]
if { [lsearch $list_of_group_entries $account_name] == -1} {
group add $group -member $account_name
} else {
error "Group \"$group\" already has an entry for \"$account_name\"."
}