Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

43 bytes added, 12:09, 3 September 2020
INVESTIGATION 3: USING CONTROL FLOW STATEMENTS IN SHELL SCRIPTS
: Before learning about logic and loop control-flow statements, you need to first learn about issuing test conditions using the '''test''' command.<br><br>
# Issue the following linux commands at the Bash shell prompt to assign values to several variables:<br><span style="color:blue;font-weight:bold;font-family:courier;">course="ULI101"<br>number1=5<br>number2=10</span><br><br># Issue the following linux command to test a condition:<br><span style="color:blue;font-weight:bold;font-family:courier;">test $course = "ULI101"</span><br><br>The '''$?''' variable is used to store an exit status of the previously command issued (including the test command). If the status is '''zero''', then it indicates a TRUE value and if the status is '''non-zero''', then it indicates a FALSE value.<br><br>
# Issue the following linux command to view the status of the previously-issued '''test''' command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $?</span><br><br>Based on its value, is the result TRUE or FALSE?<br><br>
# Issue the following linux command to test another condition:<br><span style="color:blue;font-weight:bold;font-family:courier;">test $course = "uli101"</span><br><br>
# Issue the following linux command to test a condition involving numbers:<br><span style="color:blue;font-weight:bold;font-family:courier;">test $number1 > $number2</span><br><br>
# Issue a linux command to display the value of '''$?'''. '''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 '''&lt;''' and '''&gt;''' 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 10</span><br><br>You should notice a file called "'''10'''". The incorrectly issued '''test''' command used redirect to create an empty file instead,<br> which indeed succeeded just giving a TRUE value!<br><br>To prevent these incorrectly issued testing for number comparisonproblems when issuing the '''test''' command when comparing numbers, you can use the following options instead:<br>'''-lt''' (&lt;), '''-le''' (&lt;&#61;), '''-gt''' (&gt;), '''-ge''' (&gt;&#61;;), '''-eq''' (&#61;), '''-ne''' (!&#61;)<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>You should notice that the exit status value is now FALSE which is the correct result.<br><br># The '''test''' command can be abbreviated by the square brackets '''&#91; &#93; ''' which contain the test condition within the square brackets. You need to have spaces between the brackets and the test condition; otherwise, you will get a test error.<br><br># To generate a '''test error''', issue the improper use of copy and paste the following '''test''' command:<br><span style="color:blue;font-weight:bold;font-family:courier;">&#91;$number1 -gt $number2&#93;</span><br><br>You should notice an test error message.<br><br># Issue Copy and paste the following (correct use of the ) '''test''' command:<br><span style="color:blue;font-weight:bold;font-family:courier;">&#91; $number1 -gt $number2 &#93;</span><br><br>Issue a command to view the value of the exit status of the previously issued '''test''' command. You should notice that is works properly.<br><br>'''Let's now learn about control-flow statements:'''<br><br>'''Logic statements''' are used to create different paths or directions that the script can execute based in on the result of testing conditions. In this tutorial, we will only focus on the '''if''' logic statement.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-1.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-1.bash</span>)<br><br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;">#!/bin/bash<br>clear<br>num1=5<br>num2=10<br>if [ $num1 -gt $num2 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "Greater Than"<br>fi</span><br><br>
13,420
edits

Navigation menu