Difference between revisions of "Tutorial12: Shell Scripting - Part 2"

From CDOT Wiki
Jump to: navigation, search
(INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS)
 
(68 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
===Main Objectives of this Practice Tutorial===
 
===Main Objectives of this Practice Tutorial===
  
:* Use the '''if-else''' and '''if-elif-else''' control flow statements.
+
:* Explain how to construct '''if-else''' and '''if-elif-else''' control flow statements.
  
:* Use alternative methods with the '''for''' loop control flow statement.
+
:* Explain the purpose of '''command substitution'''
  
:* Use '''command substitution''' with control-flow statements
+
:* Explain how to issue the '''for''' loop control flow statement using a list with '''command substitution'''.
  
:* Configure and use the '''.bashrc''' start-up file.
+
:* Explain how to configure and use the '''.bashrc''' start-up file.
<br><br>
+
<br>
  
 
===Tutorial Reference Material===
 
===Tutorial Reference Material===
Line 29: Line 29:
  
  
|  style="padding-left:15px;" |Additional Control Flow Statements
+
|  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-else]
 
* [https://www.tutorialspoint.com/unix/if-else-statement.htm if-elif-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://www.cyberciti.biz/faq/bash-for-loop/#:~:text=A%20'for%20loop'%20is%20a,files%20using%20a%20for%20loop. for Loop]
 +
* [https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html Using Command Substitution]
 
* [https://bash.cyberciti.biz/guide/While_loop while Loop]<br><br>
 
* [https://bash.cyberciti.biz/guide/While_loop while Loop]<br><br>
  
Line 47: Line 48:
 
===Additional Logic Statements===
 
===Additional Logic Statements===
 
<br>
 
<br>
[[Image:if-else.png|thumb|right|300px|Example of how an '''if-else''' statement works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc]]'''if-else Statements'''
+
'''if-else statement:'''
 +
<br>
 +
[[Image:if-else.png|thumb|right|300px|Example of how an '''if-else''' statement works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
  
Unlike using only an if statement, an if-else statement take '''two different sets of actions'''<br>based on the results of the test condition.<br><br>''How it Works:''<br>When the test condition returns a '''TRUE''' value, then the Linux Commands between<br>'''then''' and '''else''' statements are executed.<br>If the test returns a '''FALSE''' value, then the the Linux Commands between<br>the '''else''' and '''fi''' statements are executed.<br><br>
+
Unlike using only an ''if'' statement, an '''if-else''' statement take '''two different sets of actions'''<br>based on the results of the test condition.<br><br>''How it Works:''<br>When the test condition returns a '''TRUE''' value, then the Linux Commands between<br>'''then''' and '''else''' statements are executed.<br>If the test returns a '''FALSE''' value, then the the Linux Commands between<br>the '''else''' and '''fi''' statements are executed.<br><br>
  
 
''Example:''
 
''Example:''
Line 55: Line 58:
 
<span style="font-family:courier;font-weight:bold;">num1=5<br>num2=10<br>if test $num1 –lt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Less Than”<br>else<br>echo  &nbsp;&nbsp;&nbsp;“Greater Than or Equal to”<br>fi</span><br><br>
 
<span style="font-family:courier;font-weight:bold;">num1=5<br>num2=10<br>if test $num1 –lt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Less Than”<br>else<br>echo  &nbsp;&nbsp;&nbsp;“Greater Than or Equal to”<br>fi</span><br><br>
  
[[Image:if-elif-else.png|thumb|right|300px|Example of how an '''if-elif-else''' statement works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc]]'''if-elif-else Statements'''
+
'''if-elif-else statement:'''
 +
[[Image:if-elif-else.png|thumb|right|300px|Example of how an '''if-elif-else''' statement works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
  
 
The '''elif''' statement can be used to perform additional conditional tests of the previous test condition tests '''FALSE'''. This statement is used to make your logic control-flow statement to be more adaptable.<br><br>''How it Works:''<br>If the test condition returns a '''TRUE''' value, then the Linux Commands between<br>'''then''' and '''else''' statements are executed.<br><br>If the test returns a '''FALSE''' value, then '''a new condition is tested''',<br>and action is taken if the result is '''TRUE''', then the Linux Commands between<br>'''then''' and '''else''' statements are executed. '''Additional elif statements''' can be used if additional conditional testing is required . Eventually, an action will be taken<br>when the final test condition is '''FALSE'''.<br><br>
 
The '''elif''' statement can be used to perform additional conditional tests of the previous test condition tests '''FALSE'''. This statement is used to make your logic control-flow statement to be more adaptable.<br><br>''How it Works:''<br>If the test condition returns a '''TRUE''' value, then the Linux Commands between<br>'''then''' and '''else''' statements are executed.<br><br>If the test returns a '''FALSE''' value, then '''a new condition is tested''',<br>and action is taken if the result is '''TRUE''', then the Linux Commands between<br>'''then''' and '''else''' statements are executed. '''Additional elif statements''' can be used if additional conditional testing is required . Eventually, an action will be taken<br>when the final test condition is '''FALSE'''.<br><br>
Line 61: Line 65:
 
''Example:''
 
''Example:''
  
<span style="font-family:courier;font-weight:bold;">num1=5<br>num2=10<br>if test $num1 –lt $num2<br>then &nbsp;&nbsp;&nbsp;echo “Less Than”<br>elif test $num1 –gt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Greater Than”<br>else &nbsp;&nbsp;&nbsp;echo “Equal to”<br>fi</span><br><br>
+
<span style="font-family:courier;font-weight:bold;">num1=5<br>num2=10<br>if test $num1 –lt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Less Than”<br>elif test $num1 –gt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Greater Than”<br>else &nbsp;&nbsp;&nbsp;echo “Equal to”<br>fi</span><br><br>
  
 
===Additional Loop Statements===
 
===Additional Loop Statements===
 +
 +
'''Command Substitution:'''
  
 
[[Image:for-command-substitution.png|thumb|right|300px|Example of how a '''for loop with command substitution''' works.]]
 
[[Image:for-command-substitution.png|thumb|right|300px|Example of how a '''for loop with command substitution''' works.]]
Line 76: Line 82:
 
<span style="font-family:courier;font-weight:bold">file $(ls)<br>mail -s "message" $(cat email-list.txt) < message.txt<br><br>
 
<span style="font-family:courier;font-weight:bold">file $(ls)<br>mail -s "message" $(cat email-list.txt) < message.txt<br><br>
  
'''Using for Loop with Command Substitution'''
+
'''Using the for Loop with Command Substitution'''
  
 
Let’s issue the for loop with a list using command substitution.<br>In the example below, we will use command substitution to issue the ls command and<br>have that output (filenames) become arguments for the for loop.<br><br>
 
Let’s issue the for loop with a list using command substitution.<br>In the example below, we will use command substitution to issue the ls command and<br>have that output (filenames) become arguments for the for loop.<br><br>
Line 83: Line 89:
  
 
<span style="font-family:courier;font-weight:bold;">for x in $(ls)<br>do<br> &nbsp;&nbsp;&nbsp;echo “The item is: $x”<br>done</span><br><br>
 
<span style="font-family:courier;font-weight:bold;">for x in $(ls)<br>do<br> &nbsp;&nbsp;&nbsp;echo “The item is: $x”<br>done</span><br><br>
 
+
'''While Loops:'''
[[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]]'''Using the while Loop Statement'''
+
[[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 '''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.''
 
''The '''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.''
Line 92: Line 98:
  
 
<span style="font-family:courier;font-weight:bold">answer=10<br>read –p “pick a number between 1 and 10: “ guess<br>while test $guess –eq 10<br>do &nbsp;&nbsp;&nbsp;read –p “Try again: “ guess<br>done<br>echo “You are correct”</span><br><br>
 
<span style="font-family:courier;font-weight:bold">answer=10<br>read –p “pick a number between 1 and 10: “ guess<br>while test $guess –eq 10<br>do &nbsp;&nbsp;&nbsp;read –p “Try again: “ guess<br>done<br>echo “You are correct”</span><br><br>
 +
 +
 +
'''Mathematical Operations:'''
 +
 +
Since you did not have to declare a data-type of a variable, user-defined variables store values as only text.
 +
This creates a problem if you want to perform math operations. In order to convert values stored as text in the shell to binary numbers, you need to use a command in order to accomplish that task.
 +
 +
The older method is to use the '''expr''' command.
 +
 +
''Example:''
 +
 +
<span style="font-weight:bold;font-family:courier;">
 +
num1=5;num2=10<br>
 +
result=$(expr $num1 + $num2)<br>
 +
echo "$num1 + $num2 = $result"<br>
 +
5 + 10 = 15
 +
</span>
 +
 +
A short-cut to convert text values to binary numbers is to contain within a double set of round brackets: '''(( ))'''
 +
 +
''Example:''
 +
 +
<span style="font-weight:bold;font-family:courier;">
 +
num1=5;num2=10<br>
 +
((result $num1 - $num2))<br>
 +
echo "$num1 - $num2 = $result"<br>
 +
5 - 10 = -5
 +
</span>
 +
 +
Alternative Example:
 +
 +
<span style="font-weight:bold;font-family:courier;">
 +
num1=5;num2=10<br>
 +
echo "$num1 x $num2 = $((num1 * num2))"<br>
 +
5 x 10 = 50
 +
</span>
 +
 +
''Example using a Loop:''
 +
 +
<span style="font-weight:bold;font-family:courier;">
 +
value=1<br>
 +
while [ $value -le 5 ]<br>
 +
do<br>
 +
&nbsp;&nbsp;echo "$value"<br>
 +
&nbsp;&nbsp;((value=value+1)) # could also use ((value++))<br>
 +
done<br>
 +
1<br>
 +
2<br>
 +
3<br>
 +
4<br>
 +
5
 +
</span>
  
 
===Using Startup Files===
 
===Using Startup Files===
Line 144: Line 202:
 
# 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>
 
# 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>
 
# 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 be used with a for loop. To simplify things, let's create another shell script that uses '''command substitution''' and a '''for''' loop.<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 be used with a for loop. To simplify things, let's create another shell script that uses '''command substitution''' and a '''for''' loop.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-2.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>
+
# Use a text editor like vi or nano to create the text file called '''for-3.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-3.bash</span>)<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>&nbsp;&nbsp;&nbsp;echo $x<br>done<br>echo "blast-off!"</span><br><br>
+
# Enter the following lines in your shell script:<br><span style="color:blue;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>&nbsp;&nbsp;&nbsp;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 ''':wx''' followed by '''ENTER''').<br><br>
 +
# Add execute permissions for the owner of this script and run this script.<br>What do you notice? How does the result differ from the shell script called for-2.bash. Why?<br><br>Let's create another shell script to '''run a loop for each file''' that is contained in your current directory using '''command substitution'''.<br><br>
 +
# Use a text editor like vi or nano to create the text file called '''for-4.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-4.bash</span>)<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="color:blue;font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>set $(ls)<br>echo Here are files in my current directory:"<br>echo<br>for x<br>do<br>&nbsp;&nbsp;&nbsp;echo " &nbsp;&nbsp;&nbsp;$x"<br>done</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>
 +
# Add execute permissions and run this shell script<br>What do you notice?<br><br>We can reduce a line in our shell script by using '''command substitution''' in the for loop instead of using the '''set''' command.<br>Let's demonstrate this in another shell script.<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>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="color:blue;font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>echo Here are files in my current directory:"<br>echo<br>for x in $(ls)<br>do<br>&nbsp;&nbsp;&nbsp;echo " &nbsp;&nbsp;&nbsp;$x"<br>done<br></span><br>
 
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<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 for-3.bash</span><br><br>
+
# Add execute permissions for this shell script and run the shell script<br>What do you notice? Does the output for this shell script differ than '''for-4.bash'''? Why?<br><br>We now want to introduce you to the use of '''error-checking'''.<br><br>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-3.bash</span><br><br>What do you notice? How does the result differ from the shell script called for-2.bash. Why?<br><br>Let's create another shell script to '''run a loop for each file''' that is contained in your current directory using '''command substitution'''.<br><br>
+
# Use the '''more''' command to view the text file called '''for-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more for-5.bash</span>)<br><br>Take a few moments to re-familiarize yourself with this shell script<br><br>
# Use a text editor like vi or nano to create the text file called '''for-4.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-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>
+
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-5.bash </span><br><br>When prompted, enter a letter instead 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>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>set $(ls)<br>echo Here are files in my current directory:"<br>echo<br>for x<br>do<br>&nbsp;&nbsp;&nbsp;echo " &nbsp;&nbsp;&nbsp;$x"<br>done</span><br><br>
+
# Use a text editor like vi or nano to edit 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 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="color:blue;font-family:courier;font-weight:bold;"><br>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 ''':wx''' followed by '''ENTER''').<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 for-4.bash</span><br><br>
+
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./for-5.bash</span><br><br>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-4.bash</span><br><br>What do you notice?<br><br>We can reduce a line in our shell script by using '''command substitution''' in the for loop instead of using the '''set''' command.<br>Let's demonstrate this in another shell script.<br><br>
+
# When prompted, enter a letter instead 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>
# 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 the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
+
# Use a text editor like vi or nano to edit 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>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>echo Here are files in my current directory:"<br>echo<br>for x in $(ls)<br>do<br>&nbsp;&nbsp;&nbsp;echo " &nbsp;&nbsp;&nbsp;$x"<br>done<br></span>
+
# Add the following lines in your shell script <u>AFTER</u> the PREVIOUSLY ADDED error-checking code block to force the user to enter a valid number:<br><span style="color:blue;font-family:courier;font-weight:bold;"><br>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 ''':wx''' followed by '''ENTER''').<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 for-5.bash</span><br><br>
+
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./for-5.bash</span><br><br>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-5.bash</span><br><br>What do you notice? Does the output for this shell script differ than '''for-4.bash'''? Why?<br><br>The last thing in this section is to introduce you to '''error-checking'''.<br><br>
+
# When prompted, enter a letter instead 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 learn how to perform '''math operations''' in a shell script, and then incorporate math operations within a loop.<br><br>
# Use the '''more''' command to view the text file called '''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>
+
# 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>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<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 instead of a number. What happens?<br><br>Let's edit the '''if-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>
+
# Enter the following lines in your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;"><br>#!/bin/bash<br>value=1<br>while [ $value -le 5 ]<br>do<br>&nbsp;&nbsp;echo "$value"<br>&nbsp;&nbsp;value=value+1<br>done<br></span><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>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> &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 ''':wx''' followed by '''ENTER''').<br><br>
 
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' 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>
+
# 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>What do you notice? You should have noticed an '''error message'''.<br><br>
# When prompted, enter a letter instead 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>
+
# To demonstrate what went wrong, issue 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 class, we need to convert a number stored as text into a '''binary number''' for calculations (in this case, advance the value by 1 for each loop). We can accomplish this by either using the expr command, or by surrounding our math operation within a set of two round brackets.<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>
+
# To demonstrate, issue the following set of commands:<br><br><span style="color:blue;font-weight:bold;font-family:courier;">num1=5;num2=10<br>sum=$(expr $num1+$num2)<br>echo $sum<br><br>((product=$num1*$num2))<br>echo $product</span><br><br>Let's apply this technique we just learned to correct our recently created shell script.<br><br>
# Add the following lines in your shell script <u>AFTER</u> the PREVIOUSLY ADDED error-checking code block to force the user to enter a valid number:<br><span style="font-family:courier;font-weight:bold;"><br>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>
+
# 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>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
 +
# Change line 6 to the following:<br><span style="color:blue;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 ''':wx''' followed by '''ENTER''').<br><br>
 
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' 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>
+
# Add execute permissions for the owner of this script and run this script.<br>What do you notice? <br><br>
# When prompted, enter a letter instead of a number. What happens?<br>Does the shell script allow you to enter an invalid grade like 200 or -6?<br><br>
 
 
:In the next investigation, you will learn to create and test-out the '''~/.bashrc''' start-up file to customize your Bash shell.
 
:In the next investigation, you will learn to create and test-out the '''~/.bashrc''' start-up file to customize your Bash shell.
  
Line 183: Line 248:
 
# Use the '''more''' command to view the contents of the start-up file called '''/etc/profile''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more /etc/profile</span>)<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/profile''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more /etc/profile</span>)<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''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more /etc/bashrc</span>)<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 "'''.bashrc'''"<br><br>
 
# Use the '''more''' command to view the contents of the start-up file called '''/etc/bashrc''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more /etc/bashrc</span>)<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 "'''.bashrc'''"<br><br>
# Let's move your .bashrc file to prevent accidental overwrite. Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv ~/.bashrc  ~.bashrc.bk</span><br><br>If you experience an error message "''No such file or directory''", just ignore since there is no startup file to backup.<br><br>
+
# Let's move your .bashrc file to prevent accidental overwrite. Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv ~/.bashrc  ~/.bashrc.bk</span><br><br>If you experience an error message "''No such file or directory''", just ignore since there is no startup file to backup.<br><br>
 
# Use a text editor like vi or nano to create the text file called '''~/.bashrc''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi ~/.bashrc</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>
 
# Use a text editor like vi or nano to create the text file called '''~/.bashrc''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi ~/.bashrc</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>
 
# 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>
Line 208: Line 273:
 
Take care and good luck in your future endeavours,
 
Take care and good luck in your future endeavours,
  
Murray Saul
+
<span style="font-size:1.3em; font-family:cursive">Murray Saul</span>
  
 
= LINUX PRACTICE QUESTIONS =
 
= LINUX PRACTICE QUESTIONS =

Latest revision as of 07:45, 30 December 2020

ADDITIONAL SHELL SCRIPTING


Main Objectives of this Practice Tutorial

  • Explain how to construct if-else and if-elif-else control flow statements.
  • Explain the purpose of command substitution
  • Explain how to issue the for loop control flow statement using a list with command substitution.
  • Explain how to configure and use the .bashrc start-up file.


Tutorial Reference Material

Course Notes
Linux Command/Shortcut Reference
YouTube Videos
Course Notes:


Additional Control Flow Statements / techniques Startup Files


Brauer Instructional Videos:

KEY CONCEPTS

Additional Logic Statements


if-else statement:

Example of how an if-else statement works.
(Image licensed under cc)

Unlike using only an if statement, an if-else statement take two different sets of actions
based on the results of the test condition.

How it Works:
When the test condition returns a TRUE value, then the Linux Commands between
then and else statements are executed.
If the test returns a FALSE value, then the the Linux Commands between
the else and fi statements are executed.

Example:

num1=5
num2=10
if test $num1 –lt $num2
then
   echo “Less Than”
else
echo    “Greater Than or Equal to”
fi


if-elif-else statement:

Example of how an if-elif-else statement works.
(Image licensed under cc)

The elif statement can be used to perform additional conditional tests of the previous test condition tests FALSE. This statement is used to make your logic control-flow statement to be more adaptable.

How it Works:
If the test condition returns a TRUE value, then the Linux Commands between
then and else statements are executed.

If the test returns a FALSE value, then a new condition is tested,
and action is taken if the result is TRUE, then the Linux Commands between
then and else statements are executed. Additional elif statements can be used if additional conditional testing is required . Eventually, an action will be taken
when the final test condition is FALSE.

Example:

num1=5
num2=10
if test $num1 –lt $num2
then
   echo “Less Than”
elif test $num1 –gt $num2
then
   echo “Greater Than”
else    echo “Equal to”
fi


Additional Loop Statements

Command Substitution:

Example of how a for loop with command substitution works.

Command substitution is a facility that allows a command
to be run and its output to be pasted back on the command line as arguments to another command.

Reference: https://en.wikipedia.org/wiki/Command_substitution

Usage:

command1 $(command2)
or
command1 `command2`


Examples:

file $(ls)
mail -s "message" $(cat email-list.txt) < message.txt

Using the for Loop with Command Substitution

Let’s issue the for loop with a list using command substitution.
In the example below, we will use command substitution to issue the ls command and
have that output (filenames) become arguments for the for loop.

Example:

for x in $(ls)
do
   echo “The item is: $x”
done


While Loops:

Example of how a while loop works.
(Image licensed under cc)

The while loop is useful to loop based on the result from a test condition or command result.
This type of loop is very useful for error-checking.

How it Works:
The condition/expression is evaluated, and if the condition/expression is TRUE,
the code within … the block is executed.
This repeats until the condition/expression becomes FALSE.
Reference: https://en.wikipedia.org/wiki/While_loop

Example:

answer=10
read –p “pick a number between 1 and 10: “ guess
while test $guess –eq 10
do    read –p “Try again: “ guess
done
echo “You are correct”



Mathematical Operations:

Since you did not have to declare a data-type of a variable, user-defined variables store values as only text. This creates a problem if you want to perform math operations. In order to convert values stored as text in the shell to binary numbers, you need to use a command in order to accomplish that task.

The older method is to use the expr command.

Example:

num1=5;num2=10
result=$(expr $num1 + $num2)
echo "$num1 + $num2 = $result"
5 + 10 = 15

A short-cut to convert text values to binary numbers is to contain within a double set of round brackets: (( ))

Example:

num1=5;num2=10
((result $num1 - $num2))
echo "$num1 - $num2 = $result"
5 - 10 = -5

Alternative Example:

num1=5;num2=10
echo "$num1 x $num2 = $((num1 * num2))"
5 x 10 = 50

Example using a Loop:

value=1
while [ $value -le 5 ]
do
  echo "$value"
  ((value=value+1)) # could also use ((value++))
done
1
2
3
4
5

Using Startup Files

Shell configuration (start-up) files are scripts that are run when you log in, log out, or start a new shell. The start-up files can be used, for example, to set the prompt and screen display, create local variables, 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: ~/.bash_profile runs when you log in ~/.bashrc runs when you start an interactive sub-shell.

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

INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS


In this section, you will learn additional control-flow statements to allow your shell scripts to be more flexible.


Perform the Following Steps:

  1. Login to your matrix account.

  2. Issue a command to confirm you are located in your home directory.

    In a previous tutorial, you learned how to use the if control-flow statement. You will now learn to use the if-else statement
    to take two different actions based on if the condition tests either TRUE or FALSE.

  3. Use a text editor like vi or nano to create the text file called if-3.bash (eg. vi if-3.bash)

    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  4. Enter the following lines in your shell script:
    #!/bin/bash
    clear
    read -p "Enter the first number: " num1
    read -p "Enter the second number: " num2
    if [ $num1 -gt $num2 ]
    then
       echo "The first number is greater than the second number."
    else
       echo "The first number is less than or equal to the second number."
    fi


  5. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  6. Issue the following linux command to add execute permissions for your shell script:
    chmod u+x if-3.bash

  7. Run your shell script by issuing: ./if-3.bash

    What do you notice? Try running the script several times with numbers different and equal to each other to
    confirm that the shell script works correctly.

    It would be nice to have a separate result of the numbers are equal to each other.
    In order to achieve this, you can use an if-elif-else statement.

  8. Use a text editor like vi or nano to create the text file called if-4.bash (eg. vi if-4.bash)

    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  9. Enter the following lines in your shell script:
    #!/bin/bash
    clear
    read -p "Enter the first number: " num1
    read -p "Enter the second number: " num2
    if [ $num1 -gt $num2 ]
    then
       echo "The first number is greater than the second number."
    elif [ $num1 -lt $num2 ]
    then
       echo "The first number is less than the second number."
    else
       echo "The first number is equal to the second number."
    fi


  10. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  11. Issue the following linux command to add execute permissions for your shell script:
    chmod u+x if-4.bash

  12. Run your shell script by issuing: ./if-4.bash

    What do you notice? Try running the script several times with numbers different and equal to each other
    to confirm that the shell script works correctly.

    A classic shell script to demonstrate many different paths or actions to take depending of multiple testing
    using an if-elif-else statement would be a mark to letter grade converter.

  13. Use a text editor like vi or nano to create the text file called if-5.bash (eg. vi if-5.bash)

    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  14. Enter the following lines in your shell script:
    #!/bin/bash
    clear
    read -p "Enter a mark (0-100): " mark
    if [ $mark -ge 80 ]
    then
       echo "You receive an A grade."
    elif [ $mark -ge 70 ]
    then
       echo "The receive a B grade."
    elif [ $mark -ge 60 ]
    then
       echo "The receive a C grade."
    elif [ $mark -ge 50 ]
    then
       echo "The receive a D grade."
    else
       echo "You receive an F grade."
    fi


  15. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  16. Issue the following linux command to add execute permissions for your shell script:
    chmod u+x if-5.bash

  17. Run your shell script by issuing: ./if-5.bash

    What do you notice? Run several times to confirm that the shell script runs correctly for all mark (grade) categories.

In the next investigation, you will learn more about the for loop and learn how to use the while loop for error-checking.

INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS

In this section, you will learn more about the for loop and learn how to use the while loop for error-checking.


Perform the Following Steps:

  1. Use the more command to view the contents of the text file called for-1.bash (eg. more for-1.bash)

    As you should have noticed from tutorial 10 that the for loop can use a list.
    You can also use the for loop with positional parameters stored as arguments from an executed shell script.
    We will revisit this now.

    You can also use the for loop with a list using command substitution - this is an effective technique to loop within a shell script.

  2. Before creating a new shell script, let's learn to use command substitution from the Bash Shell to store arguments as positional parameters and use them for practice. Issue the following linux command to set positional parameters in your current shell:
    set apples oranges bananas pears

  3. Issue the following linux command:
    echo $#

    What do you notice?

  4. Issue the following linux command:
    echo $*

    What do you notice?

    These positional parameters could be used with a for loop. To simplify things, let's create another shell script that uses command substitution and a for loop.

  5. Use a text editor like vi or nano to create the text file called for-3.bash (eg. vi for-3.bash)
    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  6. Enter the following lines in your shell script:
    #!/bin/bash
    clear
    set 10 9 8 7 6 5 4 3 2 1
    for x
    do
       echo $x
    done
    echo "blast-off!"


  7. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  8. Add execute permissions for the owner of this script and run this script.
    What do you notice? How does the result differ from the shell script called for-2.bash. Why?

    Let's create another shell script to run a loop for each file that is contained in your current directory using command substitution.

  9. Use a text editor like vi or nano to create the text file called for-4.bash (eg. vi for-4.bash)
    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  10. Enter the following lines in your shell script:
    #!/bin/bash
    clear
    set $(ls)
    echo Here are files in my current directory:"
    echo
    for x
    do
       echo "    $x"
    done


  11. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  12. Add execute permissions and run this shell script
    What do you notice?

    We can reduce a line in our shell script by using command substitution in the for loop instead of using the set command.
    Let's demonstrate this in another shell script.

  13. Use a text editor like vi or nano to create the text file called for-5.bash (eg. vi for-5.bash)
    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  14. Enter the following lines in your shell script:
    #!/bin/bash
    clear
    echo Here are files in my current directory:"
    echo
    for x in $(ls)
    do
       echo "    $x"
    done

  15. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  16. Add execute permissions for this shell script and run the shell script
    What do you notice? Does the output for this shell script differ than for-4.bash? Why?

    We now want to introduce you to the use of error-checking.

  17. Use the more command to view the text file called for-5.bash (eg. more for-5.bash)

    Take a few moments to re-familiarize yourself with this shell script

  18. Run your shell script by issuing: ./for-5.bash

    When prompted, enter a letter instead of a number. What happens?

    Let's edit the for-5.bash shell script to perform error-checking to force the user to enter a numeric value between 0 and 100.

    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.

  19. Use a text editor like vi or nano to edit the text file called for-5.bash (eg. vi for-5.bash)

    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  20. Add the following lines in your shell script AFTER the read statement to prompt the user for a mark:

    while ! echo $mark | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null
    do
       read -p "Not a valid number. Enter a mark (0-100): " mark
    done


  21. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  22. Run your shell script by issuing:
    ./for-5.bash

  23. When prompted, enter a letter instead of a number. What happens?
    Does the shell script allow you to enter an invalid grade like 200 or -6?

    Let's add an additional error-checking loop to force the user to enter a number between 0 and 100.

  24. Use a text editor like vi or nano to edit the text file called for-5.bash (eg. vi for-5.bash)

  25. Add the following lines in your shell script AFTER the PREVIOUSLY ADDED error-checking code block to force the user to enter a valid number:

    while [ $mark -lt 0 ] || [ $mark -gt 100 ]
    do
       read -p "Invalid number range. Enter a mark (0-100): " mark
    done


  26. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  27. Run your shell script by issuing:
    ./for-5.bash

  28. When prompted, enter a letter instead of a number. What happens?
    Does the shell script allow you to enter an invalid grade like 200 or -6?

    Let's learn how to perform math operations in a shell script, and then incorporate math operations within a loop.

  29. Use a text editor like vi or nano to create the text file called for-6.bash (eg. vi for-6.bash)
    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  30. Enter the following lines in your shell script:

    #!/bin/bash
    value=1
    while [ $value -le 5 ]
    do
      echo "$value"
      value=value+1
    done

  31. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  32. Set execute permissions for this shell script and run your shell script by issuing: ./for-6.bash

    What do you notice? You should have noticed an error message.

  33. To demonstrate what went wrong, issue the following commands:

    num1=5;num2=10
    result=$num1+$num2
    echo $result

    Notice that the user-defined variable stores the text "10+5" which is NOT the expected result of adding the number 10 and 5.

    As you may recall in class, we need to convert a number stored as text into a binary number for calculations (in this case, advance the value by 1 for each loop). We can accomplish this by either using the expr command, or by surrounding our math operation within a set of two round brackets.

  34. To demonstrate, issue the following set of commands:

    num1=5;num2=10
    sum=$(expr $num1+$num2)
    echo $sum

    ((product=$num1*$num2))
    echo $product


    Let's apply this technique we just learned to correct our recently created shell script.

  35. Use a text editor like vi or nano to edit the text file called for-6.bash (eg. vi for-6.bash)
    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  36. Change line 6 to the following:
    ((value=value+1))

    Note: For those familiar with other programming languages, you can achieve the same results by using: ((value++))

  37. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  38. Add execute permissions for the owner of this script and run this script.
    What do you notice?

In the next investigation, you will learn to create and test-out the ~/.bashrc start-up file to customize your Bash shell.

INVESTIGATION 3: USING START-UP FILES

In this section, you will learn how to customize your Bash Linux shell environment by creating and testing a start-up file.


Perform the Following Steps:

  1. Use the more command to view the contents of the start-up file called /etc/profile (eg. more /etc/profile)

    This file contains the default settings when you open your Bourne shell (eg. if issuing the command sh).

  2. Use the more command to view the contents of the start-up file called /etc/bashrc (eg. more /etc/bashrc)

    This file contains the default settings when you open your Bash shell (eg. if issuing the command bash).

    Since we are using the Bash shell by default, let's create a customized Bash start-up file.
    This startup file is located in your home directory using the name ".bashrc"

  3. Let's move your .bashrc file to prevent accidental overwrite. Issue the following linux command:
    mv ~/.bashrc ~/.bashrc.bk

    If you experience an error message "No such file or directory", just ignore since there is no startup file to backup.

  4. Use a text editor like vi or nano to create the text file called ~/.bashrc (eg. vi ~/.bashrc)

    If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.

  5. Enter the following lines in your shell script (the symbol "[" is the open square bracket symbol):
    clear
    echo -e -n "\e[0;34m"
    echo "Last Time Logged in (for security):"
    echo
    lastlog -u $USER
    echo
    echo -e -n "\e[m"


  6. Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).

  7. You can test run the startup file without exiting and re-entering your Bash shell environment.
    Issue the following:
    . ~/.bashrc

    What do you notice?

  8. Exit your current Bash shell session.

  9. Login again to your matrix account.

    Did you start-up file customize your Bash shell environment with colours?

  10. Issue the following linux command to restore your previous settings for your bashrc startup file:
    mv ~/.bashrc.bk ~/.bashrc

    If you experience an error message "No such file or directory", just ignore.

  11. After you complete the Review Questions sections to get additional practice, then work on your online assignment 3,
    sections 4 to 6 labelled: More Scripting (add), Yet More Scripting (oldfiles), and sed And awk

WHERE DO I GO FROM HERE?

I hope this series of tutorials have been helpful in teaching you basic Linux OS skills.

In order to get efficient in working in the Linux environment requires practice and applying what you have learned to administering Linux operating systems including users, applications, network services and network security.

Although you are NOT required to perform Linux administration for this course, there are useful course notes and tutorials for advanced Linux server administration that have been created for the Networking / Computer Support Specialist stream:



Take care and good luck in your future endeavours,

Murray Saul

LINUX PRACTICE QUESTIONS

The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.

Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:

https://ict.senecacollege.ca/~murray.saul/uli101/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:

  1. Create 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?

  2. Add code to the retire.bash script created in the previous question to force the user to enter only an integer to provide error-checking for this shell script.

  3. What is the purpose of the /etc/profile startup file?

  4. What is the purpose of the /etc/bashrc startup file?

  5. What is the purpose of the ~/.bashrc startup file?

  6. What is the purpose of the ~/.bash_logout file?

  7. Write code for the ~/.bashrc 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.

  8. Write a command to run the recently created ~/.bashrc startup file from the previous question without exiting and re-logging into your Matrix account.