Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

6 bytes added, 10:10, 21 March 2021
INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' 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>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>
# 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>
13,420
edits

Navigation menu