Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

3.2.5 Conditional Constructs

if
The syntax of the if command is:
if test-commands; then
  consequent-commands;
[elif more-test-commands; then
  more-consequents;]
[else alternate-consequents;]
fi
The test-commands list is executed, and if its return status is zero, the consequent-commands list is executed. If test-commands returns a non-zero status, each elif list is executed in turn, and if its exit status is zero, the corresponding more-consequents is executed and the command completes. If 'else alternate-consequents' is present, and the final command in the final if or elif clause has a non-zero exit status, then alternate-consequents is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.
case
The syntax of the case command is:
case word in 
  [ [(] pattern [| pattern]...) command-list ;;]
  ... 
esac
case will selectively execute the command-list corresponding to the first pattern that matches word. The '|' is used to separate multiple patterns, and the ')' operator terminates a pattern list. A list of patterns and an associated command-list is known as a clause. Each clause must be terminated with ';;'. The word undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before matching is attempted. Each pattern undergoes tilde expansion, parameter expansion, command substitution, and arithmetic expansion. There may be an arbitrary number of case clauses, each terminated by a ';;'. The first pattern that matches determines the command-list that is executed. Here is an example using case in a script that could be used to describe one interesting feature of an animal:
echo -n "Enter the name of an animal: "
read ANIMAL
echo -n "The $ANIMAL has "
case $ANIMAL in
  horse | dog | cat) echo -n "four";;
  man | kangaroo ) echo -n "two";;
  *) echo -n "an unknown number of";;
esac
echo " legs."
The return status is zero if no pattern is matched. Otherwise, the return status is the exit status of the command-list executed.
select
The select construct allows the easy generation of menus. It has almost the same syntax as the for command:
select name [in words ...]; do 
  commands; 
done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error output stream, each preceded by a number. If the 'in words' is omitted, the positional parameters are printed, as if 'in "$@"' had been specified. The PS3 prompt is then displayed and a line is read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the select command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The commands are executed after each selection until a break command is executed, at which point the select command completes. Here is an example that allows the user to pick a filename from the current directory, and displays the name and index of the file selected.
select fname in *;
do
	echo you picked $fname \($REPLY\)
	break;
done
((...))
(( expression ))
The arithmetic expression is evaluated according to the rules described below (see section 6.5 Shell Arithmetic). If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to
let "expression"
See section 4.2 Bash Builtin Commands, for a full description of the let builtin.
[[...]]
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below in section 6.4 Bash Conditional Expressions. Word splitting and filename expansion are not performed on the words between the '[[' and ']]'; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. When the '==' and '!=' operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below in section 3.5.8.1 Pattern Matching. The return value is 0 if the string matches or does not match the pattern, respectively, and 1 otherwise. Any part of the pattern may be quoted to force it to be matched as a string. Expressions may be combined using the following operators, listed in decreasing order of precedence:
( expression )
Returns the value of expression. This may be used to override the normal precedence of operators.
! expression
True if expression is false.
expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2
True if either expression1 or expression2 is true.
The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.

 
 
  Published under the terms of the GNU General Public License Design by Interspire