13,420
edits
Changes
→INVESTIGATION 4: CONTROL FLOW STATEMENTS
# Save your editing session and exit the text editor<br>(eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br>[[Image:if-2.png|thumb|right|320px|Output of a shell script using the '''read''' command and the '''if''' control-flow statement.]]
# Issue the following Linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x if-2.bash</span><br><br>
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./if-2.bash</span><br><br>Enter When prompted enter make certain the '''first number greater''' is <u>greater than </u> the '''second number'''<br><br># Run the <span style="font-weight:bold;font-family:courier;">./if-2.bash</span> Bash shell script again and enter when prompted, make certain the '''first number''' that is '''<u>less than or equal''' to </u> the '''second number'''.<br>What happens?<br><br>Let's use an '''if-else''' statement to provide an appropriate alternative<br>if the condition is FALSE.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-3.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-3.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold">#!/bin/bash<br>read -p "Enter the first number: " num1<br>read -p "Enter the second number: " num2<br>if [ $num1 -gt $num2 ]<br>then<br> echo "The first number is greater than the second number."<br>else<br> echo "The first number is less than or equal to the second number."<br>fi</span><br><br>