Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

499 bytes added, 11:24, 27 October 2020
WHERE DO I GO FROM HERE?
===Main Objectives of this Practice Tutorial===
:* Use the Explain how to construct '''if-else''' and '''if-elif-else''' control flow statements.
:* Use alternative methods with Explain the purpose of '''forcommand substitution''' loop control flow statement.
:* Use Explain how to issue the '''for''' loop control flow statement using a list with '''command substitution''' with control-flow statements.
:* Configure Explain how to configure and use the '''.bashrc''' start-up file.<br><br>
===Tutorial Reference Material===
| 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://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>
===Additional Logic Statements===
<br>
'''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])]]'''if-else Statements'''
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:''
<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>
'''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])]]'''if-elif-else Statements'''
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>
===Additional Loop Statements===
 
'''Command Substitution:'''
[[Image:for-command-substitution.png|thumb|right|300px|Example of how a '''for loop with command substitution''' works.]]
<i>'''Command substitution''' is a facility that allows a command<br>to be run and its output to be pasted back on the command line as arguments to another command.</i><br>Reference: https://en.wikipedia.org/wiki/Command_substitution<br><br>
''Usage:''
<span style="font-family:courier;font-weight:bold">file $(ls)<br>mail -s "message" $(cat email-list.txt) < message.txt<br><br>
'''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>
<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])]]
[[Image:while-loop.png|thumb|right|170px|Example of how a '''while''' loop works.]]'''Using the while Loop Statement''' 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. ''
This repeats until the condition/expression becomes '''FALSE'''.<br>Reference: https://en.wikipedia.org/wiki/While_loop<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>
= WORKING EFFECTIVELY IN THE LINUX ENVIRONMENT 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 advanced '''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,
<span style="font-size:1.3em; font-family:cursive">Murray Saul</span>
= LINUX PRACTICE QUESTIONS =
13,420
edits

Navigation menu