Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

240 bytes removed, 08:37, 18 March 2021
INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS
# Issue a Linux command to <u>change</u> to the '''advanced''' directory.<br><br>
# Issue a Linux command to <u>confirm</u> you are located in the '''advanced''' directory.<br><br>In '''tutorial 10''', you learned how to use the '''if''' control-flow statement. You will now learn to use the '''if-else''' statement<br>to take two different actions based on if the condition tests either TRUE or FALSE.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-4.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-4.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;font-weight:bold">#!/bin/bash<br>clear<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>elif [ $num1 -lt $num2 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The first number is less than the second number."<br>else<br>&nbsp;&nbsp;&nbsp;echo "The first number is equal to the second number."<br>fi</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br>
# 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-4.bash</span><br><br>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./if-4.bash</span><br><br>What do you notice? Try running the script several times with numbers different and equal to each other<br>to confirm that the shell script works correctly.<br><br>A <u>classic</u> shell script to demonstrate many different paths or actions to take depending of multiple testing<br>using an '''if-elif-else''' statement would be a mark to letter grade converter.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-5.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;font-weight:bold;">#!/bin/bash<br>clear<br>read -p "Enter a mark (0-100): " mark<br>if [ $mark -ge 80 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "You receive an A grade."<br>elif [ $mark -ge 70 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The receive a B grade."<br>elif [ $mark -ge 60 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The receive a C grade."<br>elif [ $mark -ge 50 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The receive a D grade."<br>else<br>&nbsp;&nbsp;&nbsp;echo "You receive an F grade."<br>fi</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br>
13,420
edits

Navigation menu