1,234
edits
Changes
no edit summary
{{Admon/caution|DO NOT USE THIS VERSION OF THE LAB. This page will no longer be updated.|'''New version here:''' https://seneca-ictoer.github.io/ULI101/A-Tutorials/tutorial12<br />'''Andrew's students please go here:''' http://wiki.littlesvr.ca/wiki/OPS145_Lab_11}}
=ADDITIONAL SHELL SCRIPTING=
<br>
===Main Objectives of this Practice Tutorial===
:* Explain how to construct '''if-else''' and Use the '''if-elif-else''' control flow statementsstatement in a shell script.
:* Explain Use the purpose of '''for''' loop control using a list with '''command substitution'''.
:* Explain how to issue Use the '''forwhile''' loop control flow statement using in a list with '''command substitution'''shell script.
:* Use the '''exit''' and '''break''' statements in a shell script. :* Explain how to configure and use the a '''.bashrc''' start-up file.
<br>
|- valign="top" style="padding-left:15px;"
|colspan="2" |Course Notes'''Slides:'''<ul><li>Week 12 Lecture 1 Notes:<br>[https://ictwiki.cdot.senecacollege.ca/~murray.saululi101/uli101slides/ULI101-Week1212.1.pdf PDF] | [https://ictwiki.cdot.senecacollege.ca/~murray.saululi101/uli101slides/ULI101-Week1212.1.pptx PPTX]</li></ul>
| style="padding-left:15px;" |Additional '''Control Flow Statements / techniques* [https://www.tutorialspoint.com/unix/if-else-statement.htm if-else]'''
* [https://www.tutorialspoint.com/unix/if-else-statement.htm if-elif-else]
* [https://www.cyberciti.biz/faq/bash-for-loop/#:~:text=A%20'for%20loop'%20is%20a,files%20using%20a%20for%20loop. for Loop]
* [https://wwwbash.gnucyberciti.orgbiz/softwareguide/bashWhile_loop while Loop]<br>'''Additional Statements:'''* [https:/manual/html_nodewww.geeksforgeeks.org/Commandexit-command-in-linux-with-Substitutionexamples/#:~:text=exit%20command%20in%20linux%20is,last%20command%20that%20is%20executed.html Using Command Substitution&text=After%20pressing%20enter%2C%20the%20terminal%20will%20simply%20close. exit]* [https://bashwww.cybercitigeeksforgeeks.bizorg/guidebreak-command-in-linux-with-examples/While_loop while Loop#:~:text=break%20command%20is%20used%20to,The%20default%20number%20is%201. break]<br><br>
| style="padding-left:15px;"|'''Startup Files:'''
* [https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html Purpose]
* [http://www.linuxfromscratch.org/blfs/view/svn/postlfs/profile.html Examples]
|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>
|}
= KEY CONCEPTS =
''Example:''
<span style="font-family:courier;font-weight:bold;">num1=5<br>num2=10<br>if test $num1 –lt $num2<br>then<br> echo “Less Than”<br>elseelif test $num1 –gt $num2<br>then<br> echo “Greater Than”<br>else “Greater Than or Equal echo “Equal to”<br>fi</span><br><br>
''Example:''
<span style="font-family:courier;font-weight:bold;">num1=5for x in $(ls)<br>num2=10<br>if test $num1 –lt $num2do<br>then echo “Less Than”<br>elif test $num1 –gt “The item is: $num2<br>then<br> echo “Greater Than”<br>else echo “Equal to”x”<br>fidone</span><br><br>
===Additional Loop Statements=WHILE LOOP====[[Image:while-loop.png|thumb|right|170px|Example of how a '''while''' loop works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
''The 'Command Substitution''while''' loop is useful to loop based on the result from a test condition or command result.<br>This type of loop is very useful for '''error-checking'''.<br><br><i>How it Works:</i><br>The condition/expression is evaluated, and if the condition/expression is '''TRUE''',<br>the code within … the block is executed.''This repeats until the condition/expression becomes '''FALSE'''.<br>Reference: https://en.wikipedia.org/wiki/While_loop<br><br>
''ExamplesExample 2:''
<span style="font-familyweight:courierbold;font-weightfamily:boldcourier;">file value=1<br>while [ $(ls)value -le 5 ]<br>do<br>mail -s echo "message$value" $<br> ((value=value+1)) # could also use ((cat email-list.txtvalue++)) < message.txtbr>done<br>1<br>2<br>3<br>4<br>5</span><br><br>
====EXIT & BREAK STATEMENTS====<br>'''Using the for Loop with Command Substitution<span style="font-family:courier;font-weight:bold;">exit</span> Statement'''
''Example:''
<span style="font-family:courier;font-weight:bold;">for x in if [ $(ls)# -ne 1 ]<br>dothen<br> echo “The item is"USAGE: $x”0 [arg]"<br>done exit 1</spanbr>fi<br></span><br>'''While Loops<span style="font-family:'''[[Imagecourier;font-weight:while-loop.png|thumb|right|170px|Example of how a 'bold;">break</span> Statement''while''' loop works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
''Example:''<br><span style="font-family:courier;font-weight:bold">read -p "Enter a number: " number<br>while [ $number -ne 5 ]<br>do<br> read -p "Try again. Enter a number: " number<br> if [ $number -eq 5 ]<br> then<br> break<br> fi<br>done<br></span><br><br>
'''Shell configuration (start-up) files''' are '''scripts''' that are run when you log in, log out, or start a new shell. <br>The start-up files can be used, for example, to '''set the prompt and screen display''', '''create local variables''', <br>or create temporary Linux commands ('''aliases''')
The '''/etc/profile''' file belongs to the root user and is the first start-up file that executes when you log in, regardless of shell.
User-specific config start-up files are in the user's home directory: <br><ul><li>'''~/.bash_profile''' runs when you log in .<br></li><li>The '''~/.bashrc''' runs when you start an interactive sub-Bash shell.</li></ul><br>
'''Logout Files'''
There is a file that '''resets or restores the shell environment''' (including shut-down of programs running in the shell) when the user logs out of their shell. User-specific logout start-up files are in the user's home directory: '''~/.bash_logout'''
<br><br>
=INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS=
<span style="color:red;">'''ATTENTION''': This online tutorial will be required to be completed by '''Friday in week 13 by midnight''' to obtain a grade of '''2%''' towards this course</span><br><br> In this sectioninvestigation, you will learn additional control-flow statements <br>to allow your shell scripts to be even '''more flexibleadaptable'''.
# '''Login''' to your matrix account.<br><br>
# Issue a command to '''confirm''' you are located in your home directory.<br><br>In a previous tutorial, 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 Issue a text editor like vi or nano Linux command to create the text file a directory called '''if-3.bashadvanced''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-3.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:Issue a Linux command to <bru>change<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<bru> echo "The first number is greater than the second number."<br>else<br> echo "The first number is less than or 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 'advanced''ENTER''')directory.<br><br># Issue the following linux a 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-3.bash</span><br><br># Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./if-3.bashconfirm</span><bru><br>What do you notice? Try running are located in the script several times with numbers different and equal to each other to<br>confirm that the shell script works correctly'''advanced''' directory.<br><br>It would be nice In '''tutorial 10''', you learned how to have a separate result of use the numbers are equal to each other'''if''' control-flow statement.<br>In order You will now learn to achieve this, you can use an the '''if-elif-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> echo "The first number is greater than the second number."<br>elif [ $num1 -lt $num2 ]<br>then<br> echo "The first number is less than the second number."<br>else<br> 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 ''':wxx''' 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> echo "You receive received an A grade."<br>elif [ $mark -ge 70 ]<br>then<br> echo "The receive You received a B grade."<br>elif [ $mark -ge 60 ]<br>then<br> echo "The receive You received a C grade."<br>elif [ $mark -ge 50 ]<br>then<br> echo "The receive You received a D grade."<br>else<br> echo "You receive received an F grade."<br>fi</span><br><br># Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' 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>
# 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;">~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>
:In the next investigation, you will learn more about the '''for''' loop and learn how to use the '''while''' loop for '''error-checking'''.<br><br>
=INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS =
<br>In this sectioninvestigation, you will learn more about the '''for ''' loop <br>and learn how to use the '''while''' loop for '''error-checking'''.
'''Perform the Following Steps:'''
# Use the Issue a Linux command to <u>confirm</u> you are located in your '''moreadvanced''' directory in your Matrix account.<br><br># Issue the following Linux command to view the contents of the text file called '''<span style="font-family:courier;font-weight:bold;">./for-1.bash''' (eg. </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>We will revisit this now.<br><br>You can also use the '''for ''' loop with a list using '''command substitution''' - this .<br>Using command sustitution is an effective technique 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># Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $*</span><br><br>What do you notice?<br><br>These positional parameters could can be used with a for loop. To simplify things, <br>let's create another shell script that uses '''command substitution''' and within a '''for''' loop.<br><br># Use a text editor like vi or nano to create the text file called '''for-23.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-3.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>set 10 9 8 7 6 5 4 3 2 1<br>for x<br>do<br> echo $x<br>done<br>echo "blast-off!"</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br># Issue the following linux command to add '''Add execute permissions ''' for your the owner of this script and '''run this Bash shell script:'''.<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x for-3.bash</spanbr>What do you notice?<br><br># Run your Let's create another shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">to '''run a loop for each file''' that is contained in your current directory using '''command substitution'''./for-3.bash</span><br><br>What do you notice? How does # Use a text editor like vi or nano to create the result differ from the shell script text file called '''for-24.bash''' (eg. Why?<br><brspan style="color:blue;font-weight:bold;font-family:courier;">Let's create another vi for-4.bash</span>)<br><br># Enter the following lines in your shell script to '''run a loop for each file''' that is contained in your current directory using '''command substitution'''.<br:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br># Use a text editor like vi or nano to create the text file called '''for-4.bash''' (eg. <span style=clear<br>set $(ls)<br>echo "color:blue;font-weightHere are files in my current directory:bold;font-family:courier;"<br>vi for-4.bashecho</spanbr>)for x<br>do<br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule. echo " $x"<br>done</span><br><br># Enter Save your editing session and exit the following lines in your shell script:<br><span style="font-familytext editor (eg. with vi:courier;font-weight:bold;">#!/bin/bash<br>clearpress '''ESC''', then type ''':x''' followed by '''ENTER''').<br>set $(ls)<br>echo Here are files in my current directory:"# '''Add execute permissions''' and '''run''' this Bash shell script.<br>echo<br>for xWhat do you notice?<br>do<br> echo " $x"<br>done</span><br>We can reduce a line in our shell script by using '''command substitution''' in the for loop instead of using<br># Save your editing session and exit the text editor (eg. with vi: press '''ESCset''' command. Let''', then type ''':wx''' followed by '''ENTER''')s demonstrate this in another shell script.<br><br># Issue Use a text editor like vi or nano to create the following linux command to add execute permissions text file called '''for your shell script:<br>-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x vi for-45.bash</span>)<br><br># Run Enter the following lines in your shell script by issuing: <br><span style="colorfont-family:bluecourier;font-weight:bold;font-family:courier;">.#!/bin/for-4.bash</span><br>clear<br>What do you notice?echo "Here are files in my current directory:"<br>echo<br>We can reduce a line for x in our shell script by using '''command substitution''' in the for loop instead of using the '''set''' command.$(ls)<br>Let's demonstrate this in another shell script.do<br> echo " $x"<br>done<br><br># Use a text editor like vi or nano to create the text file called '''for-5.bash''' (eg. </span style="color:blue;font-weight:bold;font-family:courier;">vi for-5.bash</span>)<br><br>If you are using <br># Save your editing session and exit the nano text editor, refer to notes on text editing in a previous week in the course schedule(eg.<br><br># Enter the following lines in your shell scriptwith vi:<br><span style="font-familypress '''ESC''', then type ''':courier;font-weight:bold;"x''' followed by '''ENTER''').<br>#!/bin/bash<<br>clear<br>echo Here are files in my current directory:"<br>echo# '''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 x in $(ls)<br>do-4.bash'''? Why?<br><br> echo " $x"We now want to introduce you to the use of '''error-checking'''.<br>done<br></span># Save your editing session and exit Use the text editor (eg. with vi: press '''ESCmore''', then type command to view the previously-created Bash shell script ''':wx./if-5.bash''' followed by '''ENTER''')(eg. <span style="color:blue;font-weight:bold;font-family:courier;">more ./if-5.bash<br/span>)<br># Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;fontTake a few moments to re-weight:bold;font-family:courier;">chmod u+x for-5.bash</span>familiarize yourself with this shell script<br><br># Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./forif-5.bash</span><br><br>What do you notice? Does the output for this shell script differ than When prompted, enter a '''for-4.bashletter'''? Why?<bru>instead<br/u>The last thing in this section is to introduce you to of a '''error-checking'number''.What happens?<br><br># Use Let's edit the '''morefor-5.bash''' command shell script to view the text file called perform '''iferror-5.bashchecking''' (eg. to <span style="color:blue;font-weight:bold;font-family:courier;"u>more if-5.bash</spanforce</u>)<br><br>Take the user to enter a few moments to re-familiarize yourself with this shell scriptnumeric value between '''0''' and '''100'''.<br><br># Run your shell script by issuing'''NOTE: <span style="color:blue;font-weight:bold;font-family:courier;">''' The '''while''' statement can be used with the '''test''' command (or a simple linux command or a linux pipeline command) for error checking./if-5.bash </span><br><br>When promptedIn our case, enter we will use a letter instead of a numberpipeline command with extended regular expressions. What happens?<br><br>Let's edit In order to loop while the '''if-5.bash''' shell script to perform '''error-checking''' result is TRUE (not FALSE), you can use the negation symbol (!) to set the test condition to the opposite.<ubr>force</ubr> # Use a text editor like vi or nano to edit the user to enter a numeric value between 0 and 100.<br><br>text file called '''NOTE:./if-5.bash''' The '''while''' statement can be used with the '''test''' command (or a simple linux command or a linux pipeline command(eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi ./if-5.bash</span>) for error checking. In our case, we will use a pipeline command with extended regular expressions. In order to loop while <br><br># Add the result is TRUE (not FALSE), you can use following lines in your shell script <u>IMMEDIATELY AFTER</u> the negation symbol (!) read statement to set prompt the test condition to the opposite.user for a mark:<br><brspan style="font-family:courier;font-weight:bold;"># Use a text editor like vi or nano to edit the text file called '''if-5.bash''' (eg. while ! echo $mark | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null<br>do<span style="color:bluebr> font-weight:bold fontread -family:courier;p ">vi if-5Not a valid number.bashEnter a mark (0-100): " mark<br>done</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># Add the following lines in your shell script <u>AFTER</u> the read statement to prompt the user for a mark:<br><span style="font-family:courier;font-weight:bold;"><br>while ! echo $mark | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null<br>do<br> read -p "Not a valid number. Enter a mark (0-100): " mark<br>done</span><br><br># Save your editing session and exit # Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wxx''' 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 code block '''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;"><br>while [ $mark -lt 0 ] || [ $mark -gt 100 ]<br>do<br> 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 ''':wxx''' 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># Enter the following lines in your shell script:<br><span style="font-weight:bold;font-family:courier;"><br>#!/bin/bash<br>value=1<br>while [ $value -le 5 ]<br>do<br> echo "$value"<br> value=value+1<br>done<br></span><br># Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br># Set execute permissions for this shell script and run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-6.bash</span><br><br>'''You should have noticed an error message'''.<br><br># To demonstrate what went wrong, <u>issue</u> the following '''commands''':<br><br><span style="color:blue;font-weight:bold;font-family:courier;">num1=5;num2=10<br>result=$num1+$num2<br>echo $result<br><br></span>Notice that the user-defined variable stores the text "'''10+5'''" which is <u>NOT</u> the expected result of adding the number '''10''' and '''5'''.<br><br>As you may recall in '''tutorial 10''', we need to convert a number stored as text into a '''binary number'''<br>for calculations (in this case, advance the value by 1 for each loop).<br>We can accomplish this by using the math construct '''(( ))'''<br><br># To demonstrate, <u>issue</u> the following set of '''commands''':<br><br><span style="color:blue;font-weight:bold;font-family:courier;">num1=5;num2=10<br>sum=$(($num1+$num2))<br>echo $sum<br><br>((product=$num1*$num2))<br>echo $product</span><br><br>Let's correct our '''for-6.bash''' shell script to correctly use math operations.<br><br># Use a text editor like vi or nano to edit 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># Edit '''line 6''' and replace with the following:<br><span style="font-family:courier;font-weight:bold;">((value=value+1))</span><br><br>'''Note:''' For those familiar with other programming languages, you can achieve the same results by using: '''((value++))'''<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;">~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 create use the '''exit''' statement to '''terminate the execution of a shell script'''<br>if not run with the properly number of arguments and test-out use the '''~/.bashrcbreak''' statement that will '''terminate a loop''' start-up file to customize your Bash <br>but NOT terminate the running of the shellscript. =INVESTIGATION 3: <span style="font-family:courier;font-weight:bold;">exit</span> AND <span style="font-family:courier;font-weight:bold;">break</span> STATEMENTS=
'''THE EXIT STATEMENT'''
The '''exit''' statement is used to terminate a shell script.<br>This statement is very useful when combined with logic in a shell script to display an '''error message'''<br>if the command was '''improperly executed''' and '''terminate''' the running of the shell script.<br><br>The ''exit'' command can contain return a ''value'' to provide the '''exit status'''<br>of your shell script (i.e. TRUE or FALSE value).<br><br>
'''Perform the Following Steps:'''
# Use Make certain that you are logged into matrix account.<br><br># Confirm that you are currently located in the '''moreadvanced''' command directory.<br><br># Use a text editor like vi or nano to view create the contents of the start-up text file called '''/etc/profileexit.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more /etc/profilevi exit.bash</span>)<br><br>This file contains # Enter the following lines in the '''default settingsexit.bash''' when you open shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br><br>if [ $# -ne 1 ]<br>then<br> echo "USAGE: $0 [arg]" >&2<br> exit 1<br>fi<br><br>echo "The argument is: $1"</span><br><br># Save your Bourne shell editing session and exit the text editor (eg. if issuing the command with vi: press '''ESC''', then type ''':x''' followed by '''shENTER''').<br><br># Use the '''moreAdd execute permissions''' for this Bash shell script.<br><br># Issue the following command to view the contents of the start-up file called '''/etc/bashrc''' (eg. without arguments):<br><span style="color:blue;font-weight:bold;font-family:courier;">more ./etc/bashrcexit.bash</span>)<br><br>This file contains What did you notice?<br><br>Since there are no arguments, the test within the running shell script returns FALSE,<br>then an '''default settingserror message''' with feedback of how to properly issue the shell script with an argument<br>and then ''' when you terminates'''open your the Bash shellscript.<br><br>Notice that the '''$0''' (eg. if issuing positional parameter displays the command '''bashname''').of the currently running shell script<br><br>Since we are using in the USAGE message. This is useful in case you decide to '''change''' the ''name'' of the Bash shell by default, let's create script at a customized Bash start-up filelater time.<br>This startup file is located in your <br>The symbol '''>&2''' redirects '''homestandard output''' directory using from the name "USAGE message<br>to '''standard error'''making like a <u>real</u> error message.bashrc<br>This "''neat redirection trick''"will NOT be considered for evaluation for this coverage.<br><br># Let's move your .bashrc file to prevent accidental overwrite. Issue the following linux Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv ~/.bashrc ~.bashrc.bkecho $?</span><br><br>If you experience an error message "What does this '''exit status'No such file or directory''", just ignore since there is no startup file to backup.from the previously issued command indicate?<br><br># Use a text editor like vi or nano to create Issue the text file called '''~/.bashrc''' following command (eg. with an argument):<br><span style="color:blue;font-weight:bold;font-family:courier;">vi ~./exit.bashrcbash uli101</span>)<br><br>If What did you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.notice this time?<br><br># Enter Issue the following lines in your shell script (the symbol "Linux command:<br><span style="font-familycolor:courierblue;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># Save your editing session and exit the text editor (eg. with vi: press What does this '''ESCexit status''', then type ''':wx''' followed by '''ENTER''').from the previously issued command indicate?<br><br># You can test run the startup file without exiting and re-entering your Bash shell environment.<br>Issue the followingcommand (with two arguments and redirecting stderr to a file):<br><span style="color:blue;font-weight:bold;font-family:courier;">. ~/exit.bash uli101 Linux 2> error.bashrctxt</span><br><br>What do did you noticethis time?<br><br># Exit your current Bash shell session.Issue the following Linux command:<br><brspan style="color:blue;font-weight:bold;font-family:courier;"># '''Login''' again to your matrix account.echo $?<br/span><br>Did you start-up file customize your Bash shell environment with colours?<br><br># Issue the following linux Linux command to restore your previous settings for your bashrc startup confirm that stderr was redirected to a file:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv ~/.bashrc.bk ~/cat error.bashrctxt</span><br><br>If you experience an error message "''No such file or directory''", just ignore.<br><br># After you complete the Review Questions sections to get additional practice, then work on your '''online assignment 3,'''<br>'''sections 4 to 6''' labelled: '''More Scripting (add)''', '''Yet More Scripting (oldfiles)''', and '''sed And awk'''<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 perform Linux operating 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 '''tutorialsTUTORIALS''' for advanced Linux server administration <br>that have been created for the Networking / Computer Support Specialist stream:
:* [https://wiki.cdot.senecacollege.ca/wiki/OPS235 OPS235OPS245 OPS245: Basic Linux Server Administration]
:* [https://wiki.cdot.senecacollege.ca/wiki/OPS335 OPS335: Advanced Linux Server Administration]
Take care and good luck in your future endeavours, <span style="font-family:courier;font-weight:bold;">:)</span>
<span style="font-size:1.5 em3em; font-family:cursive">Murray Saul</span>
= LINUX PRACTICE QUESTIONS =
simulate a quiz:
https://ictwiki.cdot.senecacollege.ca/~murray.saululi101/uli101files/uli101_week12_practice.docx
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:'''
# 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> _________________________________________________________________________________ Author: Murray Saul License: LGPL version 3Link: https://www.gnu.org/licenses/lgpl.html _________________________________________________________________________________
[[Category:ULI101]]