Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

38 bytes added, 09:06, 28 July 2020
INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS
# 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;">#!/bin/bash<br>clear<br>read -p "Enter the first numbera mark (0-100): " num1mark<br>read if [ $mark -p ge 80 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "Enter the second number: You receive an A grade." num2<br>if elif [ $num1 mark -gt ge 70 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The receive a B grade."<br>elif [ $num2 mark -ge 60 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The first number is greater than the second numberreceive a C grade."<br>elif [ $num1 mark -gt $num2 ge 50 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The first number is less than the second numberreceive a D grade."<br>else<br>&nbsp;&nbsp;&nbsp;echo "The first number is equal to the second numberYou 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>
# 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-5.bash</span><br><br>
13,420
edits

Navigation menu