Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

10 bytes added, 14:10, 16 March 2021
# 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>When prompted enter , make certain that the '''first number''' 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 when .<br><br> When prompted, make certain that the '''first number''' 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>&nbsp;&nbsp;&nbsp;echo "The first number is greater than the second number."<br>else<br>&nbsp;&nbsp;&nbsp;echo "The first number is less than or equal to the second number."<br>fi</span><br><br>

Navigation menu