13,420
edits
Changes
→INVESTIGATION 4: CONTROL FLOW STATEMENTS
# Issue a Linux command to display the value of '''$?'''<br><br>'''NOTE:''' You will notice that something is '''wrong'''.<br>The exit status '''$?''' shows a zero (TRUE) value, but the number 5 is definitely NOT greater than 10.<br>The problem is that the symbols '''<''' and '''>''' are interpreted as REDIRECTION symbols!<br><br>
# To prove this, issue the following Linux command :<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -l 10</span><br><br>You should notice a file called "'''10'''". The incorrectly issued '''test''' command '''used redirection'''<br>to create an '''empty''' file and assigning the exit status variable a ''TRUE'' value!<br><br>To prevent problems when issuing the '''test''' command when comparing numbers,<br>you can use the following options:<br>'''-lt''' (<), '''-le''' (<=), '''-gt''' (>), '''-ge''' (>=;), '''-eq''' (=), '''-ne''' (!=)<br><br>
# Issue the correct Linux command to '''properly ''' test both values:<br><span style="color:blue;font-weight:bold;font-family:courier;">test $number1 -gt $number2</span><br><br>
# Issue a Linux command to display the value of '''$?'''.<br><br>You should notice that the exit status value is now ''FALSE'' which is the correct result.<br><br>
# The '''test''' command can be substituted by '''square brackets''' '''[ ]''' which contains the '''test''' condition<br>within the square brackets. You need to have spaces between the brackets and the test condition;<br>otherwise, you will get a test error.<br><br>