Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

1,652 bytes added, 20:47, 22 June 2021
LINUX PRACTICE QUESTIONS
=INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS=
<span style="color:red;">'''ATTENTION''': Depending on your ULI101 instructor, you may be required to complete this tutorial for '''marks''' in this course.<br>Please refer to your instructor's course notes and lecture notes regarding evaluation for this course.<br><br>The due date for successfully completing this tutorial (i.e. '''tutorial 12''') is by '''Friday by midnight''' next week (i.e. '''Week 12''').<br>If your instructor has NOT assigned marks for completing this tutorial, you can perform it for '''practice'''.</span><br><br> 
In this investigation, you will learn additional control-flow statements<br>to allow your shell scripts to be even '''more adaptable'''.
# 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>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./if-5.bash</span><br><br>What do you notice? Run several times to confirm that the shell script runs correctly for all mark (grade) categories.<br><br>
# Issue the following to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/myscripts~uli101/week12-check-1</span><br><br>
# If you encounter errors, make corrections and '''re-run''' the checking script until you<br>receive a congratulations message, then you can proceed.<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''' this Bash shell script again.<br><br>What do you notice this time? <br><br>
# Issue the following to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/myscripts~uli101/week12-check-2</span><br><br>
# If you encounter errors, make corrections and '''re-run''' the checking script until you<br>receive a congratulations message, then you can proceed.<br><br>
:In the next investigation, you will learn to use the '''exit''' statement to '''terminate the execution of a shell script'''<br>if not run with the properly number of arguments and use the '''break''' statement that will '''terminate a loop'''<br>but NOT terminate the running of the shell script.
# '''Add execute permissions''' for this Bash shell script.<br><br>
# Issue the following command (with arguments):<br><span style="color:blue;font-weight:bold;font-family:courier;">./break-2.bash hwd101 ipc144 uli101 apc100</span><br><br>What do you notice? How come '''uli101''' and '''apc100''' are NOT displayed<br>but a message appeared at the end of the script that the script completed?<br><br>
# Issue the following to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/myscripts~uli101/week12-check-3</span><br><br>
# If you encounter errors, make corrections and '''re-run''' the checking script until you<br>receive a congratulations message, then you can proceed.<br><br><br>
# Issue a Linux command to <u>confirm</u> you are located in the '''home''' directory.<br><br>
# Use the '''more''' command to view the contents of the '''default start-up''' file called '''/etc/profile''' <br><br>This file contains the '''default settings''' when you open your Bourne shell (eg. if issuing the command '''sh''').<br><br>
# Use the '''more''' command to view the contents of the start-up file called '''/etc/bashrc'''<br><br>This file contains the '''default settings''' when you '''open your Bash shell''' (eg. if issuing the command '''bash''').<br><br>Since we are using the '''Bash shell''' by default, let's create a '''customized Bash start-up file'''.<br>This startup file is located in your '''home''' directory using the name "'''.bashrcbash_profile'''"<br><br>Let's move your <span style="font-weight:bold;font-family:courier;">.bashrcbash_profile</span> file to prevent '''accidental overwrite'''.<br><br># Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv ~/.bashrc bash_profile ~/.bashrcbash_profile.bk</span><br><br>If you experience an error message "''No such file or directory''",<br>just ignore this command since there is no '''~/.bashrcbash_profile''' file in your home directory.<br><br># Use a text editor like vi or nano to create the text file called '''~/.bashrcbash_profile''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi ~/.bashrcbash_profile</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 (the symbol "<span style="font-family:courier;font-weight:bold;">[</span>" is the open square bracket symbol):<br><span style="font-family:courier;font-weight:bold;">clear<br>echo -e -n "\e[0;34m"<br>echo "Last Time Logged in (for security):"<br>echo<br>lastlog -u $USER<br>echo<br>echo -e -n "\e[m"</span><br><br>'''NOTE:''' You will notice there is '''NO she-bang line''' since this is a '''start-up''' file.<br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# You can test run the startup file without exiting and re-entering your Bash shell environment.<br>Issue the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">. ~/.bashrcbash_profile</span><br><br>What do you notice?<br><br>
# '''Exit''' your current Bash shell session.<br><br>
# '''Login''' again to your matrix account.<br><br>Did you start-up file customize your Bash shell environment with colours?<br><br>'''NOTE:''' This is where you can make your Linux shell environment values '''persistent'''<br>(i.e. saved regardless of exit and login to your Bash Shell such as '''aliases''', '''umask''', etc.).<br><br>
# Issue the following linux command to '''restore''' your previous settings for your bashrc startup file:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv ~/.bashrcbash_profile.bk ~/.bashrcbash_profile</span><br><br>If you experience an error message "''No such file or directory''", just ignore.<br><br>
# '''Exit''' your current Bash shell session.<br><br>
# '''Login''' again to your matrix account.<br><br>What did you notice this time?<br><br>
I hope this series of tutorials have been helpful in teaching you basic Linux OS skills.
<br><br>
In order to get efficient in working in the Linux environment requires '''practice''' and '''applying'''<br>what you have learned to administering perform Linux operating systems system administration including:<br>'''user management''', '''installing and removing applications''', '''network services''' and '''network security'''.
<br><br>
Although you are '''NOT''' required to perform '''Linux administration''' for this course,<br>there are useful '''course notes''' and '''TUTORIALS''' for advanced Linux server administration<br>that have been created for the Networking / Computer Support Specialist stream:
'''Review Questions:'''
# Create Write code for a Bash shell script called '''retire.bash''' that when run, 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 '''retire.bash''' 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 '''~/.bashrcbash_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 '''~/.bashrcbash_profile''' startup file from the previous question without exiting and re-logging into your Matrix account.<br><br>
[[Category:ULI101]]
13,420
edits

Navigation menu