Changes

Jump to: navigation, search

Tutorial 12 - Shell Scripting - Part 2

2,698 bytes added, 22:51, 4 August 2023
m
no edit summary
Content under development
=ADDITIONAL SHELL SCRIPTING=
===Main Objectives of this Practice Tutorial===
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Linux Command/Shortcut Reference<br>
 
|colspan="1" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|YouTube Videos<br>
|- valign="top" style="padding-left:15px;"
|colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=XVTwbINXnk4&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=6 Bash Shell Scripting - Part 2]</li></ul>
|}
= INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS =
<span style="color:red;">'''ATTENTION''': The due date for successfully completing this tutorial (i.e. tutorial 12) is by Friday, December 15 April 21 @ 11:59 PM (Week 14).</span><br>
In this investigation, you will learn additional control-flow statements<br>to allow your shell scripts to be even '''more adaptable'''.
# Issue a Linux command to <u>confirm</u> you are located in your '''advanced''' directory in your Matrix account.<br><br>
# Issue the following Linux command to view the <span style="font-family:courier;font-weight:bold;">~./for-1.bash</span> file:<br><span style="color:blue;font-weight:bold;font-family:courier;">more ~./for-1.bash</span>)<br><br>As you should have noticed from '''tutorial 10''' that the '''for''' loop can use a '''list'''.<br>You can also use the for loop with positional parameters stored as '''arguments'''<br>from an executed shell script.<br><br>You can also use the '''for''' loop with a list using '''command substitution'''.<br>Using command sustitution is an effective method to loop within a shell script.<br><br>Before creating a new shell script, let's learn to use command substitution from the Bash Shell<br>to store arguments as positional parameters and use them for practice.<br><br>
# Issue the following linux command to set positional parameters in your current shell:<br><span style="color:blue;font-weight:bold;font-family:courier;">set apples oranges bananas pears</span><br><br>
# Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $#</span><br><br>What do you notice? What does this value represent?<br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# '''Add execute permissions''' for this shell script and '''run Bash shell script'''<br>What do you notice? Does the output for this shell script differ from '''for-4.bash'''? Why?<br><br>We now want to introduce you to the use of '''error-checking'''.<br><br>
# Use the '''more''' command to view the previously-created Bash shell script '''~./if-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more ~./if-5.bash</span>)<br><br>Take a few moments to re-familiarize yourself with this shell script<br><br># Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">~./if-5.bash </span><br><br>When prompted, enter a '''letter''' <u>instead</u> of a ''number''. What happens?<br><br>Let's edit the '''for-5.bash''' shell script to perform '''error-checking''' to <u>force</u> the user to enter a numeric value between '''0''' and '''100'''.<br><br>'''NOTE:''' The '''while''' statement can be used with the '''test''' command (or a simple linux command or a linux pipeline command) for error checking. In our case, we will use a pipeline command with extended regular expressions. In order to loop while the result is TRUE (not FALSE), you can use the negation symbol (!) to set the test condition to the opposite.<br><br># Use a text editor like vi or nano to edit 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>
# Add the following lines in your shell script <u>IMMEDIATELY AFTER</u> the read statement to prompt the user for a mark:<br><span style="font-family:courier;font-weight:bold;">while ! echo $mark | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null<br>do<br> &nbsp;&nbsp;&nbsp;read -p "Not a valid number. Enter a mark (0-100): " mark<br>done</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./if-5.bash</span><br><br>
# When prompted, enter a '''letter''' <u>instead</u> of a ''number''. What happens?<br>Does the shell script allow you to enter an invalid grade like '''200''' or '''-6'''?<br><br>Let's add an '''additional error-checking loop''' to force the user to enter a number between '''0''' and '''100'''.<br><br>Compound operators like '''&&''' and '''||''' can be used with the '''test''' command.<br>Let's use the '''||''' compound criteria to to NOT accept numbers '''outside''' of the range '''0''' to '''100'''.<br><br>
# Use a text editor like vi or nano to edit 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>
# Add the following lines in your shell script <u>IMMEDIATELY AFTER</u> the PREVIOUSLY ADDED<br>error-checking '''while''' loop statement to '''force''' the user to enter a valid number (between 1 and 100):<br><span style="font-family:courier;font-weight:bold;">while [ $mark -lt 0 ] || [ $mark -gt 100 ]<br>do<br> &nbsp;&nbsp;&nbsp;read -p "Invalid number range. Enter a mark (0-100): " mark<br>done</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">~./if-5.bash</span><br><br>
# When prompted, enter a '''letter''' <u>instead</u> of a ''number''. What happens?<br>Does the shell script allow you to enter an '''invalid grade''' like '''200''' or '''-6'''?<br><br>Let's reinforce '''math operations''' in a shell script (that you created in '''tutorial 10''') and then incorporate math operations within a loop.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-6.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-6.bash</span>)<br><br>
= LINUX PRACTICE QUESTIONS =
The purpose of this section is to obtain '''extra practice''' to help with '''quizzes''' and your '''midterm''.
 
Here is a link to the [https://matrix.senecacollege.ca/~osl640/questions/osl640_week12_practice.docx MS Word Document of ALL of the questions] displayed below but with extra room to answer on the document to
simulate a quiz:
 
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
 
 
'''Review Questions:'''
 
# Write code for a Bash shell script that clears the screen, and then prompts the user for their age. If the age entered is less than 65, then display a message that the person is NOT eligible to retire. If the age is equal to 65, then display a message that the person just turned 65 and can retire. If the age is greater than 65, then display the message that the user is over 65 and why have they not have already retired already?<br><br>
# Add code to the script created in the <u>previous</u> question to force the user to enter only an '''integer''' to provide error-checking for this shell script.<br><br>
#Write code for a Bash shell script that will prompt the user for a '''valid POSTAL CODE'''.<br>A valid postal code consists of the following format: '''x#x #x#'''<br>where '''x''' represents an upper or lowercase letter<br>and '''#''' represents a number from 0-9<br><br>Also VALID postal codes can consist of no spaces or one or more spaces in the format shown above.<br><br>If the user enters an '''INVALID postal code''', indicate an error and allow the user to enter the VALID postal code. When the user enters a VALID postal code, then clear the screen and display the VALID postal code.<br><br>
# Write code that works similar to the previous question, but have it read an input file called '''unchecked-postalcodes.txt''' and only save VALID postal codes to a file called:<br>'''valid-postalcodes.txt'''<br><br>Design your Bash Shell script to only run if the user enters TWO ARGUMENTS:<br>'''unchecked-postalcodes.txt''' and '''valid-postalcodes.txt'''<br><br>Otherwise, display an error message and immediately exit your Bash Shell script with a false exit value.<br><br>
# What is the purpose of the '''/etc/profile''' startup file?<br><br>
# What is the purpose of the '''/etc/bashrc''' startup file?<br><br>
# What is the purpose of the '''~/.bashrc''' startup file?<br><br>
# What is the purpose of the '''~/.bash_profile''' file?<br><br>
# What is the purpose of the '''~/.bash_logout''' file?<br><br>
# Write <u>code</u> for the '''~/.bash_profile''' file below to clear the screen, welcome the user by their username, and display a list of all users currently logged into your Matrix server. Insert blank lines between each of those elements.<br><br>
# Write a command to <u>run</u> the recently created '''~/.bash_profile''' startup file from the previous question without exiting and re-logging into your Matrix account.<br><br>
 
 
 
[[Category:OSL640]]

Navigation menu