Open main menu

CDOT Wiki β

Changes

Tutorial10: Shell Scripting - Part 1

144 bytes removed, 09:57, 10 March 2021
Using Control Flow Statements in Shell Scripts
<table align="right"><tr valign="top"><td>[[Image:test-1.png|thumb|right|140px|Examples of simple comparisons using the test command.]]</td><td>[[Image:test-2.png|thumb|right|140px|Examples of using additional comparisons using the test command.]]</td></table>
'''Control Flow Statements''' are used to make your shell scripts<br>more '''flexible''' and can '''adapt''' to changing situations.<br><br>In order to use control flow statements, you need to test a condition to get a TRUE or FALSE value. This can be done two ways:<ul><li>Run a command to get the exit status (<span style="font-family:courier;font-weight:bold;">$?</span>)</li><li>Use the '''test''' command</li></ul><br><br>Refer to the diagrams on the right to see how to use the test command.<br><br>
The special parameter '''$?''' Is used to determine the '''exit status''' of the previously issued Linux command.The exit status will either display a '''zero''' (representing TRUE) or a '''non-zero''' number (representing FALSE). The test command can be used with control flow statements to control the sequence of a shell script. You CANNOT use the '''<span style="font-family:courier;font-weight:bold;"></span>''' or '''<''' span style="font-family:courier;font-weight:bold;"></span> symbols when using the test command since these are redirection symbols. Instead, you need to use options when performing numerical comparisons.
Refer to the table below for test options and their purposes.
13,420
edits