Changes

Jump to: navigation, search

Tutorial 12 - Shell Scripting - Part 2

4,961 bytes added, 14:05, 14 November 2021
no edit summary
|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 =
 
====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 <u>new</u> condition is tested again''',<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>
 
''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>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>
 
====FOR LOOP USING 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>
 
''Example:''
 
<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 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 '''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>
 
''Example 1:''
 
<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>
 
 
''Example 2:''
 
<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>
<br><br>
 
====EXIT &amp; BREAK STATEMENTS====
<br>
'''<span style="font-family:courier;font-weight:bold;">exit</span> 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.<br>
The exit command can contain an argument to provide the exit status of your shell script.
 
''Example:''
 
<span style="font-family:courier;font-weight:bold">if [ $# -ne 1 ]<br>then<br>  echo "USAGE: $0 [arg]"<br>  exit 1<br>fi<br></span>
<br>
'''<span style="font-family:courier;font-weight:bold;">break</span> Statement'''
 
The '''break''' statement is used to '''terminate a loop'''.<br>
Although the loop terminates, the shell script will continue running.
 
''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>
 
====START-UP FILES====
'''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 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'''
 
= INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS =

Navigation menu